Data Localization Library

A while ago I have written about the Localization Concern, and one of the topics that I have detailed there was Data Localization.

By Data Localization we mean the cases when we need to store some text properties of some business entities in different languages. For example in an e-commerce app you may need to store the name and the description in more languages for the same product. The name of the mouse product will be Mouse in English, Souris d’ordinateur in French and maybe Computermaus in German.

In that post, I have emphasized why it is wise to separate this translation concern from the rest of the query logic and to build a generic mechanism that based on some conventions does the translation for all the queries. I've also given some code snippets that give the direction on how this can be implemented as part of a generic Repository or an UnitOfWork implementation.

Recently my colleague Catalin Pop has published on our iQuarc Github account a library that does just that. To quote him:

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

The library is very easy to use and it can be plugged in any Linq based data access framework. You install it via NuGet and you just add the .Localize() extension method after the .Select(), and boom: your data is translated.

var products = dbContext.Products  
                 .Select(p => new ProductData
                         Name = p.Name,
                         Description = p.Description,
                         Quantity = p.Quantity,

Of course, you need to have the translations tables in the database. Under the hood, the query is rewriten to join with them. You can do this call in your repository implementation as I was suggesting in my older blog post, or you can just call .Localize() from your controllers or services depending on your context.

To make it more usable, the library provides the TranslationForAttribute which can be added on the classes that are mapped through the ORM to the translation tables. In my example this would go on the ProductTrans class that is mapped to Products_Trans table. This means that you don't have to use name based conventions on your tables, because you can use this attribute to tell the library which is the table it needs to join with.

To see more usage examples and how to get started with this you can go to the file on the Github repository, where Catalin has written more details. Also, if you have feedback or questions you can use the Issues on Github and I'm sure Catalin is going to answer or to help you to get stared.

More about implementing localization is addresses in my Code Design Training
Featured image credit: eyematrix via 123RF Stock Photo

Florin Coros

Read more posts by this author.