Leer este artículo en castellano aqui
Last thursday I could attend a debate organized by MSCoders Madrid user group, related with localization of apps in a web enfironment.
After the event I got interested in frontend localization with Javascript. As Karmacracy for Windows is a Javascript app, I decided to do some experimentation on localizing the user interface in the context of a Windows Store app, to be able to distribute it for other markets.
Detecting the system language
For a Windows Store app the first thing to do is to detect the sustem language. This can be archieved with the following piece of code:
var culture = Windows.System.UserProfile.GlobalizationPreferences.languages[0]; var language = culture.substr(0, 2);
The culture is the language that is currently being used for the UI, in culture format. For example for Spanish of Spain would be es-ES, for US English would be en-US, and for British English would be en-GB. To be able to change this order and deploy our app, we must go to the control panel:
In this example we only need the language, not the culture, so we are going to get a substring of the culture getting en, es, de, etc.
Adding angular-translate
Angular-translate is a module that allows us to define key-value pairs for each language and perform the translation according to the configuration. For installing it, if we don’t use a package manager such as bower, we must download the last package from github and add it to our project.
Once installed, we must add it to the configuration of our app, in the section we define external modules (in this case the module is pascalprechtr.translate).
var karmacracyApp = angular.module('karmacracyApp', [ 'ngRoute', 'karmacracyControllers', 'pascalprecht.translate' ]);
Finally, we configure the different languages by using the code we defined previously for getting the OS display language:
karmacracyApp.config(['$translateProvider', function ($translateProvider) { $translateProvider.translations('en', { 'LOGIN': 'Log in', 'LOGIN_USERNAME': 'User name', 'LOGIN_PASSWORD': 'Password' }); $translateProvider.translations('es', { 'LOGIN': 'Inicia sesión', 'LOGIN_USERNAME': 'Nombre de usuario', 'LOGIN_PASSWORD': 'Contraseña' }); var culture = Windows.System.UserProfile.GlobalizationPreferences.languages[0]; var language = culture.substr(0, 2); $translateProvider.preferredLanguage(language).fallbackLanguage('en'); }]);
Also, in case the current language is not available, we set English by default
#Usage
For accessing our keys we don’t need any aditional configuration on our controllers, we just need to modify our views so they can access the localized information
<span class="loginMessage">{{ 'LOGIN' | translate }}</span>
If the language is set to English or other language not controlled, the result will be the following:
If the language is set to spanish, the result will be the following:
As it shows, is an easy way to localize our javascript apps for the Windows Store.
Recap and next steps
Angular-translate provides a simple way of localizing texts in our views and Windows provides the information we need for setting the current language of an app.
As an aditional step, we may try to localize winjs controls such as buttons, which work in a very different way. Angular-translate also supports loading the translations from a json files, an option that may be useful if we want to distribute those files for translation by 3rd parties.
Links
- Angular translate
- Get Language of WinRT app the samples are in C# but porting it is as easy as respect java conventions
Un comentario en “Adding multiple languages to a Windows Store app with Javascript and Angular”