Home

Awesome

nativescript-i18n(+NS7 support)

This is a plugin for Nativescript that implements i18n in an easy manner.

It uses the native capabilities of each platform without the need of a separate JS or JSON file.

It is heavily inspired from NativeLang and this thread

The plugin defines an L() method at the application level so it will be accessible both from the XML and JS files.

Please don't forget to read the IMPORTANT section! :smile:

Credits

Most thanks goes out to Dan Tamas (@rborn) 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.

Thanks to @TheBrousse and @ValioStoychev for the help with iOS and @alejonext for creating the initial module.

Also a big thanks to all the contributors that made this repo better :)

Usage

Install the plugin in your app

npm install --save nativescript-i18n

Create a folder i18n in the app folder with the following structure:

app
  |
  |-- App_Resources
  |
[...]
  |
  |-- i18n
       |
       |-- en
             |- strings.xml
       |
       |-- es
             |- strings.xml

Require nativescript-i18n and globals in app.js as early as possible (I do it even before I require the application module, the very first 2 lines).

	require('globals');
	require('nativescript-i18n');

If you use TypeScript and want to use L() in the code you will need to cast the global var to any like belowin the file you intend to use L(). You can read more about this in https://github.com/rborn/nativescript-i18n/issues/57

	declare var global:any;

And in the code use it like this:

XML files

Simple string

	<Label text="{{ L('hello') }}">

It supports one or multiple replacements, directly or from the model

	<Label text="{{ L('hello') }}" class="title" textWrap="true" />
	<Label text="{{ L('hello_replace','my friend') }}" class="message" textWrap="true" />
	<Label text title="{{ L('multi_replace','direct replacement', modelReplacement) }}">

Assuming you have defined in strings.xml the definitions and in the model the replacement modelReplacement variable

	<string name="hello">Hello!</string>
	<string formatted="false" name="hello_replace">Hello %s!</string>
	<string formatted="false" name="multi_replace">We can replace directly in xml: %s or from the model: %s</string>

To define a custom path for the i18n files (other than App_Resources/i18n), add this configuration to your project's package.json

"nativescript-i18n": {
    "customLangPath": "app/resources/i18n"
}

Language defaults to english if the phone's language doesn't match any of your defined languages. If you want to set your own default language, add this configuration to your project's package.json

Keep in mind that on iOS the language picked by the device will be based on the order in Settings -> Language & Region -> Preferred language order

"nativescript-i18n": {
    "defaultLang": "es"
}

IMPORTANT

JS files

	console.log(L('home'));
	console.log(L('multi_replace', 'ONE', 'TWO'));

Angular

In case you use Angular for your app, things get a little more complicated.

My Angular skills are zero but @alejonext has a solution for it in this comment.

Update 28.06.2016

@AirMike and @loloof64 did a great job by testing and further improving @alejonext's PR so the plugin includes now support for Angular :bow:

After you import the plugin in the app in the usual way just need to import the module NativeScriptI18nModule from nativescript-i18n/angular in your file (main.ts)

(Please be aware that the below intructions are in typescript not pure js)

	import { NativeScriptI18nModule } from "nativescript-i18n/angular";

and then import it in your app module

	@NgModule({


      imports: [
        NativeScriptI18nModule
      ]


    })
    export class AppModule { }

Angular usage is {{ value | L:args }}

	<Button text="{{ 'Login' | L }}"></Button>

As for a more detailed example :

You can put a code like this in your main.ts :

    import { NativeScriptI18nModule } from 'nativescript-i18n/angular';

    import { NativeScriptModule } from "nativescript-angular/platform";
    import { NgModule } from "@angular/core";
    import { AppComponent } from "./app.component";


    @NgModule({

      imports: [
        NativeScriptModule,
        NativeScriptI18nModule
      ],

      declarations: [
        AppComponent,
      ],
      bootstrap: [AppComponent]
    })
    export class AppModule { }

For the main component, let's say that the following html template is used (the strings definitions follow next):

    <GridLayout rows="*,*,*">
        <label row="0" text="{{'menuitem_new_file' | L }}"></label>
        <label row="1" text="{{'menuitem_new_folder' | L }}"></label>
        <label row="2" text="{{'menuitem_new' | L:'---':'***':124.25693 }}"></label>
    </GridLayout>

And let's say that these are the string definitions for "en" (put in app/i18n/en/strings.xml)

    <resources>
        <string name="app_name">Chess Exercices Cupboard</string>
        <string name="title_activity_kimera">Chess Exercices Cupboard</string>

        <string formatted="false" name="menuitem_new">%s New... %s %0.2f</string>
        <string name="menuitem_new_file">File</string>
        <string name="menuitem_new_folder">Folder</string>
    </resources>

And the french translations (put in app/i18n/fr/srings.xml)

    <resources>
        <string name="app_name">Chess Exercices Cupboard</string>
        <string name="title_activity_kimera">Chess Exercices Cupboard</string>

        <string formatted="false" name="menuitem_new">%s Nouveau... %s %0.2f</string>
        <string name="menuitem_new_file">Fichier</string>
        <string name="menuitem_new_folder">Dossier</string>
    </resources>

Then if your phone is configured for french you'll see something like this :

    Fichier
    Dossier
    --- Nouveau... *** 124.25693

Or, if configured for english or "unrecognized" language :

    File
    Folder
    --- New... *** 124.25693

Demo

Please have a look in the demo folder for a working example.

iOS Permission text

Add this special keys to app/i18n/(lang)/strings.xml used to notify user, in configured language while showing permission alerts.

KeyDescription of key
NSAppleMusicUsageDescriptionSpecifies the reason for your app to use the media library
NSBluetoothPeripheralUsageDescriptionSpecifies the reason for your app to use Bluetooth
NSCalendarsUsageDescriptionSpecifies the reason for your app to access the user’s calendars
NSCameraUsageDescriptionSpecifies the reason for your app to access the device’s camera
NSContactsUsageDescriptionSpecifies the reason for your app to access the user’s contacts
NSHealthShareUsageDescriptionSpecifies the reason for your app to read the user’s health data
NSHealthUpdateUsageDescriptionSpecifies the reason for your app to make changes to the user’s health data
NSHomeKitUsageDescriptionSpecifies the reason for your app to access the user’s HomeKit configuration data
NSLocationAlwaysUsageDescriptionSpecifies the reason for your app to access the user’s location information at all times
NSLocationWhenInUseUsageDescriptionSpecifies the reason for your app to access the user’s location information while your app is in use
NSMicrophoneUsageDescriptionSpecifies the reason for your app to access any of the device’s microphones
NSMotionUsageDescriptionSpecifies the reason for your app to access the device’s accelerometer
NSPhotoLibraryUsageDescriptionSpecifies the reason for your app to access the user’s photo library
NSRemindersUsageDescriptionSpecifies the reason for your app to access the user’s reminders
NSSiriUsageDescriptionSpecifies the reason for your app to send user data to Siri
NSSpeechRecognitionUsageDescriptionSpecifies the reason for your app to send user data to Apple’s speech recognition servers

(Pseudo) roadmap/ideas

The following ideas are inspired by this comment and issues posted by the users of this plugin