This is a plugin for NativeScript that implements internationalization (i18n) using the native platforms standards. It is inspired from nativescript-i18n
npm install --save nativescript-localizeCreate 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
import{NgModule,NO_ERRORS_SCHEMA}from"@angular/core";import{NativeScriptLocalizeModule}from"nativescript-localize";import{NativeScriptModule}from"nativescript-angular/nativescript.module";import{AppComponent}from"./app.component";
@NgModule({declarations: [AppComponent],bootstrap: [AppComponent],imports: [NativeScriptModule,NativeScriptLocalizeModule],schemas: [NO_ERRORS_SCHEMA]})exportclassAppModule{}<Labeltext="{{ 'Hello world !' | L }}"></Label>
<Labeltext="{{ 'I am %s' | L:'user name' }}"></Label>constapplication=require("application");constlocalize=require("nativescript-localize").localize;application.resources.L=localize;<Labeltext="{{ L('Hello world !') }}"></Label>
<Labeltext="{{ L('I am %s', 'user name') }}"></Label>Add the .default extension to the default language file to set it as the fallback language:
fr.default.json
The "app.name" key is used to localize the application name:
{
"app.name": "My app"
}Keys starting with ios.info.plist. are used to localize iOS properties:
{
"ios.info.plist.NSLocationWhenInUseUsageDescription": "This will be added to InfoPlist.strings"
}Each file is imported using require, use the file format of your choice:
{
"app.name": "My app",
"ios.info.plist": {
"NSLocationWhenInUseUsageDescription": "This will be added to InfoPlist.strings"
},
"user": {
"name": "user.name",
"email": "user.email"
},
"array": [
"split the translation into ",
"multiples lines"
],
"sprintf": "format me %s"
}consti18n={"app.name": "My app"};module.exports=i18n;