M# Tutorials

Learn to build .NET applications with M#. Our step-by-step tutorials will get you up-to-speed rapidly.
If you already know ASP.NET and C#, you can master M# in a week or two.

Configuring M# for Multi-languages

In the previous tutorial we learnt about interfaces defined in M# framework required to support multilingual contents. In this tutorial we will look at the configuration required to fully enable the multi-language support for an application using M#.

Interface Implementation

Before performing any configuration in M#, we must have ILanguage and IPhraseTranslation interfaces implemented in the model. For this purpose we have created two entities in the model and have implemented both interfaces, as shown below:

The highlighted code above shows that we have implemented "IPhraseTranslation.Language" property explicitly even though we have already created a property Language in entity type PhraseTranslation. We created Language property to persist the Language type reference in the database with the phrase translation so that we can use it later to return it form "IPhraseTranslation.Language" which doesn’t support assignment call.

Note: Please remember that there must be one Language set as "Default" in the database (In our case, it is English).

The screenshot shown below is of database table "dbo.Language", which was generated by M# based on Language entity type we created in M#.

In order to enable translation, you must have all the static phrases e.g. headings, label texts, menu texts etc. in the "dbo.PhraseTranslation" database table with the translation and language reference. The database table shown below is based on PhraseTranslation entity type. The database table has 3 phrases at the moment with translation and translation language. We will use a list module on Employee entity type to translate the phrases into "Spanish" translation.

Configuring M#

Multi-lingual support for an application is enabled by setting “Is Multi Lingual” attribute available in projects setting section in M#, as shown below :

When a project is marked as multilingual by setting the above shown attribute, M# replaces all the string phrases using "Translate()" implemented in M# framework. All the static or dynamic strings are replaced using this method with appropriate translation. The screenshot below shows sections of a login form module which are now being replaced using "Translate()" method:

M# framework also translates text of collection type form control e.g. dropdown lists, checkbox lists etc.

M# framework matches the phrases of a page in the "dbo.PhraseTranslation" database table and then displays the relevant translation based on the selected language. M# framework also supports "Google Translation" which can be enabled by setting the app settings shown below in the "Web.Config" file.

Enable.Google.Translate is a Boolean type setting which determines whether Google translation should be used.

Google.Translate.Key is a string type setting, which must be supplied when the aforementioned setting is set to true otherwise M# framework throws an "InvalidOperationException" exception.

Note: M# framework always looks for the phrase translation in the database first and if doesn’t find the phrase match then goes to Google translation if enabled.

At this point, we have successfully setup the multi-lingual configuration and if you access any page the contents should be shown in "English" language without any error. Now we need to display the available languages on the UI so that users of our website can select the language of their interest. We have two languages available in our database English (as default language) and Spanish. We will simply display two buttons for this purpose but, you can also add a "File" type property in "lanuage" entity type to store image for each language and then can display those images, which will be more interactive for changing the language of the website (Please read tutorial File and Image File for more details on adding file type properties).

M# framework looks for an HTTP cookie of type ILanguage to determine the language for translation and uses default language if no cookie for ILanguage type is available. The question arises, how to store a HTTP cookie based on an entity type?

M# framework provides "CookieProperty" class, which exposes methods to set, get and remove a HTTP cookie for an entity type. You should always use this class to perform any cookie related task because it provides easy and standardised access to HTTP cookie data.

We have written a simple static method "SetWebLanguage" in our "Context" static class (Also Explained in Role tutorial) available in "App_Code" folder of our website project, as shown below:

For the purpose of this tutorial, we can add two buttons to the footer module to display languages, fireing all the methods outlined above in a custom action added in the button for each language, as shown below:

Note: In real applications you should utilize dynamicity options provided for buttons in M# to display available languages.

At this point, globalization is set up completely and we should be able to display multi-lingual contents. The screenshot below shows a list module on entity type Employee on the page with the available language buttons in the footer.

If you remember, we added three phrases in the beginning of this tutorial in "dbo.PhraseTranslation" database table. The above shown screenshot also displays those three phrases on the page and if you select "Spanish", M# framework translates those phrases into available Spanish translation throughout the website.