iQuarc.DataLocalization 1.0.4

dotnet add package iQuarc.DataLocalization --version 1.0.4
NuGet\Install-Package iQuarc.DataLocalization -Version 1.0.4
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="iQuarc.DataLocalization" Version="1.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add iQuarc.DataLocalization --version 1.0.4
#r "nuget: iQuarc.DataLocalization, 1.0.4"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install iQuarc.DataLocalization as a Cake Addin
#addin nuget:?package=iQuarc.DataLocalization&version=1.0.4

// Install iQuarc.DataLocalization as a Cake Tool
#tool nuget:?package=iQuarc.DataLocalization&version=1.0.4

iQuarc.DataLocalization

Data Localization is a helper library for Linq based data access frameworks (Such as Entity Framework 6, Entity Framework Core, Linq2SQL, nHibernate, and others) that allows easy writing queries for retrieving localized data.

Overview

The library provides a set of helper methods for querying localized data split in multiple tables. The library works by rewriting Linq expression trees that perform projections on the main table, to join and retrieve data from localization tables when available.

Usage Patterns

For example let's consider the following database which contains a Categories table and a CategoryLocalization table with localized data for categories:

CREATE TABLE [Languages]
(
	[ID] int  NOT NULL IDENTITY(1,1) PRIMARY KEY,
	[Name] nvarchar(255) NOT NULL,
	[IsoCode] nchar(2) NOT NULL
)

CREATE TABLE [Categories]
(
	[ID] int NOT NULL IDENTITY(1,1) PRIMARY KEY,
	[Name] nvarchar(512) NOT NULL
)

CREATE TABLE [CategoryTranslations]
(
	[ID] int NOT NULL IDENTITY(1,1) PRIMARY KEY,
	[LanguageID] int NOT NULL FOREIGN KEY REFERENCES [Languages]([ID]),
	[CategoryID] int NOT NULL FOREIGN KEY REFERENCES [Categories]([ID]),
	[Name] nvarchar(512) NOT NULL
)

The Linq query to a list of categories without localized data would look like:

var categories = ctx.Categories
		.Select(c => new
		{
			ID = c.ID,
			Name = c.Name
		}).ToList();

However to retrieve localized category names, for example the French version the query would need to look something like:

var categories = ctx.Categories
		.Select(c => new 
		{ 
			ID = c.ID, 
			Name = c.CategoryTranslations.Where(ct => ct.Language.IsoCode == "fr").Select(ct => ct.Name).FirstOrDefault() ?? c.Name
		}).ToList();

This pattern of writing queries becomes very tedious and error prone in time. This is where iQuarc.DataLocalization comes in, a simple set of helper methods that enable simple Linq queries to be localized automatically.

Using iQuarc.DataLocalization

var categories = this.Categories
		.Select(c => new
		{
			ID = c.ID,
			Name = c.Name
		})
		.Localize(new CultureInfo("fr-Fr")
		.ToList();

In the above query the .Localize(...) method call will automatically replace Name = c.Name in the projection with Name = c.CategoryTranslations.Where(ct => ct.Language.IsoCode == "fr").Select(ct => ct.Name).FirstOrDefault() ?? c.Name as if the localized query was written manually.

Getting Started

Step 1. Install from Nuget: https://www.nuget.org/packages/iQuarc.DataLocalization/

Or from Command Line: PM> Install-Package iQuarc.DataLocalization

Step 2. First step is to register a localization entity, typically this is done on the DbContext static constructor

static AppDbContext()
{
	iQuarc.DataLocalization.LocalizationConfig.RegisterLocalizationEntity<Language>(l => l.IsoCode);
}

Step 3. Mark all entities that contain localized data with [TranslationFor] attribute

[TranslationFor(typeof(Category))]
public class CategoryTranslation
{
	public int ID {get;set;}
	public int LanguageID {get;set;}
	public int CategoryID {get;set;}
	public string Name {get;set;}
	...
}

Step 4. Use the .Localize() extension method to localize queries containing projections (contain .Select() calls that [project data to new entities)

! Important !

The translation of Linq queries is only performed if there actually is a linq projection, otherwise the query is unaffected.

iQuarc.AppBoot iQuarc.DataAccess

Changing the localization entity identifying key

In order to change the key used to identify the localization entity the following configurations can be used:

        LocalizationConfig.RegisterLocalizationEntity<Language>(l => l.LCID);
        LocalizationConfig.RegisterCultureMapper(c => c.LCID);

The example sets up the LCID to be the language idetifier, which is a numeric code part of [RFC5646] See Also, Microsoft LCID structure:

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 is compatible.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net451 is compatible.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 2.0

    • No dependencies.
  • .NETFramework 4.5.1

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.4 14,446 3/15/2018
1.0.1 1,342 10/4/2017
1.0.0 1,609 9/22/2016

- Added .NetCore2.0 and .Netstadard2.0 targets
- Fixed nested translation bug preventing translation of nested collections