Localization

This guide explains how to implement multilanguage support in your Smartface applications.

Adding a New Language to the App

Smartface supports the i18n localization system. “i18n.js” is available in the i18n folder. Some language files added by default. You can also add your own language with the following steps:

  • Create a copy of one of the language .js files in the i18n folder.

  • Rename and edit the .js file accordingly. (example.js in this case)

  • In the next step, you need to "require" that file in the i18n.js file.

You can add two-letter ISO 639-1 standard language codes after adding the ‘case’ command in the Language section.

Changing the App Language

If you want to change the app language, go to “i18n.js" script file and edit the "SMF.i18n.switchLanguage();" function. For example; add “fi” for Finnish. To use the device locale as default, you can leave the string as “Device.language”.

Change Language In Runtime

"global.SMF.i18n.changeLanguageTo()" is used to change language in runtime then Application.restart() must be triggered as following function.

  • global.SMF.i18n.currentLang() returns the current language code of the app is being run.

  • global.SMF.i18n.defaultLang() returns the default language code of the app.

Few languages such as Arabic, Hebrew, or Persian are written from Right to Left. To handle them, views can be converted to RTL theme within Smartface.

Android Permission Dialog

When your app calls checkPermission(permission), the system shows a standard dialog box to the user. Your app cannot configure or alter that dialog box. If you need to provide any information or explanation to the user, you should do that before you call checkPermission(permission), as described in Explain why the app needs permissions.

Langugage Implementation in the User Interface

"lang" array in the “i18n.js” is used for language bindings with the keys. Let's add two labels into our project to get a specific text from the lang array.

JavaScript
JavaScript
const Page = require("sf-core/ui/page");
const extend = require("js-base/core/extend");
const Color = require('sf-core/ui/color');
const FlexLayout = require('sf-core/ui/flexlayout');
const Label = require('sf-core/ui/label');
const TextAlignment = require('sf-core/ui/textalignment');
var Page1 = extend(Page)(
function(_super) {
_super(this, {
onShow: function(params) {
Application.statusBar.visible = false;
this.headerBar.visible = false;
}
});
this.layout.flexDirection = FlexLayout.FlexDirection.ROW;
this.layout.justifyContent = FlexLayout.JustifyContent.CENTER;
this.layout.alignItems = FlexLayout.AlignItems.CENTER;
var myLabel = new Label({
width: 120,
height: 60,
backgroundColor: Color.create("#00A1F1"),
text: lang['cancel'],
textAlignment: TextAlignment.MIDCENTER
});
this.layout.addChild(myLabel);
}
);
module.exports = Page1;

Handling permission and platform controlled dialog contents

Both Android and iOS has their own dialog structure. For native experience, Smartface advises to use what platform offers.

Texts on Android can be changed by the developer, so you can use common localization methods to customize them.

For iOS, Permission and some native dialogs cannot be customized on runtime, therefore developer needs to define what texts to use on Info.plist.

  • Create a folder called Localization inside of config/iOS.

  • Create a folder called e.g. en.lproj and a new folder for each language you want to include. Languages should be alpha2 coded.

  • Inside of each folder, create a new file called InfoPlist.strings and populate the file like example is shown at below:

config/iOS/Localization/en.Iproj/InfoPlist.strings
config/iOS/Localization/fr.lproj/InfoPlist.strings
config/iOS/Localization/en.Iproj/InfoPlist.strings
"NSLocationWhenInUseUsageDescription" = "Location access is required";
"NSPhotoLibraryUsageDescription" = "Gallery access is required";
"NSCameraUsageDescription" = "Camera access is required";
config/iOS/Localization/fr.lproj/InfoPlist.strings
"NSLocationWhenInUseUsageDescription" = "L'accès à l'emplacement est requis";
"NSPhotoLibraryUsageDescription" = "L'accès à la galerie est obligatoire";
"NSCameraUsageDescription" = "L'accès à la caméra est requis";

You can check what keys can be customized on this Apple documentation

Adding the keys at Info.plist

Don't forget to add relevant keys in Info.plist file in xml format as well. The language in the xml module should be in your default language, for failsafe approach.

Also refer: AboutInformationPropertyListFiles