NativeScript 7
- Use
@nativescript/localize
:~5.0.0
- Source managed here
If using 6 and below, see the following:
nativescript-localize
This is a plugin for NativeScript that implements internationalization (i18n) using the native capabilities of each platform. It is inspired from nativescript-i18n
Credits
A lot of thanks goes out to Ludovic Fabrèges (@lfabreges) for developing and maintaining this plugin in the past. When he had to abandon it due to shifted priorities, he was kind enough to move the repo to me.
Table of contents
Installation
tns plugin add nativescript-localize
Usage
Create a folder i18n
in the app
folder with the following structure:
app
| i18n
| en.json <-- english language
| fr.default.json <-- french language (default)
| es.js
You need to set the default langage and make sure it contains the application name to avoid any error.
Angular
app.module.ts
;;; ;
Template
Script
; console.loglocalize"Hello world !";
Javascript / XML
app.js
const application = ;const localize = ;application;
Template
Script
const localize = ; console;
Quirks
⚠️ If you notice translations work on your main XML page, but don't work on a page you navigate to, then add this little hack to the 'page loaded' function of that new page:
const page = argsobject; pagebindingContext = ;
Vue
app.js
; Vue;
Template
File format
Each file is imported using require
, use the file format of your choice:
JSON
Javascript
const i18n = "app.name": "My app"; moduleexports = i18n;
Frequently asked questions
How to set the default language?
Add the .default
extension to the default language file to set it as the fallback language:
fr.default.json
How to localize the application name?
The app.name
key is used to localize the application name:
How to localize iOS properties?
Keys starting with ios.info.plist.
are used to localize iOS properties:
How to change the language dynamically at runtime?
This plugin uses the native capabilities of each platform, language selection is therefore made by the OS.
On iOS you can programmatically override this language since plugin version 4.2.0 by doing this:
;; // or "nl-NL", etc (or even just the part before the hyphen)
On Android you can programatically override this language since plugin version 4.2.1 by doing this:
In your app.ts / main.ts / app.js
;; onlaunchEvent,;
And in your settings page where user chooses the language:
;; // or "nl-NL", etc (or even just the part before the hyphen)
Important: In both cases, after calling override Locale, you must ask the user to restart the app
For Example:
;; alert.then;
Important: In case you are using Android app bundle to release your android app, add this to App_Resources/Android/app.gradle to make sure all lanugages are bundled in the split apks
android { // there maybe other code here // bundle { language { enableSplit = false } }}
Tip: you can get the default language on user's phone by using this
; console.log"user's language is", device.language.split'-';
Tip: overrideLocale method stores the language in a special key in app-settings, you can access it like this,
; console.loggetString'__app__language__'; // only available after the first time you use overrideLocale(langName);
Troubleshooting
The angular localization pipe does not work when in a modal context
As a workaround, you can trigger a change detection from within your component constructor:
constructor private readonly params: ModalDialogParams, private readonly changeDetectorRef: ChangeDetectorRef,
Starting from Android N, there is a weird side effect while using a WebView.
For unknown reasons, the very first creation of it resets the application locale to the device default. Therefore, you have to set the desired locale back. This is native bug and the workaround is
;;const locale = { }