All Projects → EddyVerbruggen → Nativescript Localize

EddyVerbruggen / Nativescript Localize

Licence: mit
Internationalization plugin for NativeScript using native capabilities of each platform

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Nativescript Localize

inlang
Open Source Localization Solution for Software.
Stars: ✭ 160 (+105.13%)
Mutual labels:  i18n, internationalization, l10n
i18n
Package i18n is for app Internationalization and Localization.
Stars: ✭ 79 (+1.28%)
Mutual labels:  i18n, internationalization, l10n
Angular-Gulp-Boilerplate
Clean but full-featured AngularJS boilerplate using Gulp workflow and best practices
Stars: ✭ 30 (-61.54%)
Mutual labels:  i18n, internationalization, l10n
Locale2
💪 Try as hard as possible to detect the client's language tag ("locale") in node or the browser. Browserify and Webpack friendly!
Stars: ✭ 65 (-16.67%)
Mutual labels:  i18n, internationalization, l10n
Laravel Js Localization
🌐 Convert your Laravel messages and consume them in the front-end!
Stars: ✭ 451 (+478.21%)
Mutual labels:  i18n, internationalization, l10n
lisan
🌈i18n, Reimagined! 🚀A blazing fast and super small i18n library for Javascript
Stars: ✭ 85 (+8.97%)
Mutual labels:  i18n, internationalization, l10n
python-fluent
Python implementation of Project Fluent
Stars: ✭ 142 (+82.05%)
Mutual labels:  i18n, internationalization, l10n
figma-static-localizer
A Figma plugin for static localization
Stars: ✭ 30 (-61.54%)
Mutual labels:  i18n, internationalization, l10n
Gotext
Go (Golang) GNU gettext utilities package
Stars: ✭ 292 (+274.36%)
Mutual labels:  i18n, internationalization, l10n
Mojito
An automation platform that enables continuous localization.
Stars: ✭ 256 (+228.21%)
Mutual labels:  i18n, internationalization, l10n
stone.js
gettext-like client-side Javascript Internationalization Library
Stars: ✭ 20 (-74.36%)
Mutual labels:  i18n, internationalization, l10n
Redux React I18n
An i18n solution for React/Redux and React Native projects
Stars: ✭ 64 (-17.95%)
Mutual labels:  i18n, internationalization, l10n
I18N
I18N Library for .NET, and Delphi
Stars: ✭ 48 (-38.46%)
Mutual labels:  i18n, internationalization, l10n
i18n
internationalize projects to Arabic
Stars: ✭ 67 (-14.1%)
Mutual labels:  i18n, internationalization, l10n
android-studio-plugin
Integrate your Android project with Crowdin
Stars: ✭ 52 (-33.33%)
Mutual labels:  i18n, internationalization, l10n
rosetta
A blazing fast internationalization (i18n) library for Crystal with compile-time key lookup.
Stars: ✭ 23 (-70.51%)
Mutual labels:  i18n, internationalization, l10n
awesome-i18n
🌍 A curated list of i18n resources for all kind of languages and frameworks
Stars: ✭ 205 (+162.82%)
Mutual labels:  i18n, internationalization, l10n
msgtools
Tools for Developing Diagnostic Messages
Stars: ✭ 18 (-76.92%)
Mutual labels:  i18n, internationalization, l10n
labels
Bolt Labels extension - Translatable labels for Bolt
Stars: ✭ 18 (-76.92%)
Mutual labels:  i18n, internationalization, l10n
Fluent Rs
Rust implementation of Project Fluent
Stars: ✭ 503 (+544.87%)
Mutual labels:  i18n, internationalization, l10n

NativeScript 7

If using 6 and below, see the following:

nativescript-localize

npm npm

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

import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
import { NativeScriptLocalizeModule } from "nativescript-localize/angular";
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 text="{{ 'I am %s' | L:'user name' }}"/>

Script

import { localize } from "nativescript-localize";

console.log(localize("Hello world !"));

Javascript / XML

app.js

const application = require("application");
const localize = require("nativescript-localize");
application.setResources({ L: localize });

Template

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

Script

const localize = require("nativescript-localize");

console.log(localize("Hello world !"));

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 = args.object;
  page.bindingContext = new Observable();

Vue

app.js

import { localize } from "nativescript-localize";

Vue.filter("L", localize);

Template

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

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",
  "sprintf with numbered placeholders": "format me %2$s one more time %1$s"
}

Javascript

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

module.exports = 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:

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

How to 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"
}

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:

import { overrideLocale } from "nativescript-localize/localize";
const localeOverriddenSuccessfully = overrideLocale("en-GB"); // 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

import { on, launchEvent } from '@nativescript/core/application';
import { androidLaunchEventLocalizationHandler } from 'nativescript-localize/localize';

on(launchEvent, (args) => {
  if (args.android) {
    androidLaunchEventLocalizationHandler();
  }
});

And in your settings page where user chooses the language:

import { overrideLocale } from "nativescript-localize/localize";
const localeOverriddenSuccessfully = overrideLocale("en-GB"); // 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:

import { android as _android } from '@nativescript/core/application';
import { overrideLocale } from 'nativescript-localize/localize';

alert({
  title: 'Switch Language',
  message: 'The application needs to be restarted to change language',
  okButtonText: 'Quit!'
}).then(() => {
  L.localize.overrideLocale(selectedLang);
  if (isAndroid) {
    _android.foregroundActivity.finish();
  } else {
    exit(0);
  }
});

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

import { device } from '@nativescript/core/platform';

console.log("user's language is", device.language.split('-')[0]);

Tip: overrideLocale method stores the language in a special key in app-settings, you can access it like this,

import { getString } from '@nativescript/core/application-settings'; 

console.log(getString('__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,
) {
  setTimeout(() => this.changeDetectorRef.detectChanges(), 0);
}

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

 <WebView url="https://someurl.com" @loaded="webViewLoaded"/>
import {overrideLocale, androidLaunchEventLocalizationHandler} from "nativescript-localize/localize";
import {getString} from '@nativescript/core/application-settings';
const locale = getString('__app__language__')

function webViewLoaded(){
    overrideLocale(locale)
    androidLaunchEventLocalizationHandler()
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].