Skip to content

Native internationalization plugin for NativeScript using platforms standards

License

Notifications You must be signed in to change notification settings

DickSmith/nativescript-localize

 
 

Repository files navigation

nativescript-localize

npm npm

This is a plugin for NativeScript that implements internationalization (i18n) using the native platforms standards. It is inspired from nativescript-i18n

Install

npm install --save 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

Angular 2

app.module.ts

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]
})
export class AppModule { }

Template

<Label text="{{ 'Hello world !' | L }}"></Label>
<Label text="{{ 'I am %s' | L:'user name' }}"></Label>

Javascript

app.js

const application = require("application");
const localize = require("nativescript-localize").localize;
application.resources.L = localize;

Template

<Label text="{{ L('Hello world !') }}"></Label>
<Label text="{{ L('I am %s', 'user name') }}"></Label>

Default language

Add the .default extension to the default language file to set it as the fallback language:

fr.default.json

Localize the application name

The "app.name" key is used to localize the application name:

{
  "app.name": "My app"
}

Localize iOS properties

Keys starting with ios.info.plist. are used to localize iOS properties:

{
  "ios.info.plist.NSLocationWhenInUseUsageDescription": "This will be added to InfoPlist.strings"
}

File format

Each file is imported using require, use the file format of your choice:

JSON

{
  "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"
}

Javascript

const i18n = {
  "app.name": "My app"
};

module.exports = i18n;

About

Native internationalization plugin for NativeScript using platforms standards

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 92.1%
  • JavaScript 6.9%
  • CSS 1.0%