Localize Dynamic Resources using an Aspect
CodeFluent Entities handles static localization with the message concept. Static resources refer to all those UI messages that will never change throughout your application’s life-time: labels, error messages, information messages, titles, button texts, tool tips, etc.
CodeFluent Entities (Personal and Ultimate Edition) also handles dynamic localization out of the box. Dynamic resources refer to localized data created at runtime. For instance a web shop could have an international catalog in several languages, and this catalog should be displayed in a specific language depending on the users’ language (i.e. culture in .Net). Administrators add, remove or modify items from this catalog during the application’s life-time. Thus, administrators of the catalog will need to create catalog items with labels in several languages in order to support internationalization.
Let’s see how to use dynamic localization with CodeFluent Entities.
First, create an entity:
Then add the localization aspect:
Now we have to indicate the Description property is localizable:
That’s all. Yes you read that right! As a bonus you can define localized instances in the model:
Let’s generate the code and see what is generated:
The database contains two tables
The product table contains non-localized columns and the default value of the localized columns. The default value is used when no translation exists for one culture while loading row.
The table “ProductLocalized” contains translations:
The BOM is edited automatically but you can use it the same way as if there is no dynamic localization. Save and Load method use Thread.CurrentUICulture to know which description to load or save.
// First we create a product with an English description Thread.CurrentThread.CurrentUICulture = new CultureInfo(1033); Product product = new Product(); product.Name = "Sample product"; product.Description = "A description in English"; product.Save(); // We create a French description. Thread.CurrentThread.CurrentUICulture = new CultureInfo(1036); product.Description = "Une description en français"; product.Save(); // We can also add localization by using the static method SaveLocalizedValues Product.SaveLocalizedValues(product, 1033, isDefault: true, description: "A description in English"); // Load products with an English description Thread.CurrentThread.CurrentUICulture = new CultureInfo(1033); var productsEn = ProductCollection.LoadAll(); // Load products with a French description Thread.CurrentThread.CurrentUICulture = new CultureInfo(1036); var productsFr = ProductCollection.LoadAll(); // Load products with a German description => There is no German description so the default value is used Thread.CurrentThread.CurrentUICulture = new CultureInfo(1031); var productsDe = ProductCollection.LoadAll(); Localizing Dynamic Resources is very easy with CodeFluent Entities and the <a href="http://www.softfluent.com/documentation/BOM_LOC_DynamicResources.html">localization aspect</a>. Good news, this aspect is available out of the box!
The R&D Team