# @nativescript/localize ## Contents - [@nativescript/localize](#nativescriptlocalize) - [Contents](#contents) - [Intro](#intro) - [Installation](#installation) - [Use @nativescript/localize](#use-nativescriptlocalize) - [Localization in NativeScript Core](#localization-in-nativescript-core) - [Quirks](#quirks) - [Localization in Angular](#localization-in-angular) - [Localization in Vue](#localization-in-vue) - [Localization in Svelte](#localization-in-svelte) - [Setting the default language](#setting-the-default-language) - [Localizing the application name](#localizing-the-application-name) - [File format](#file-format) - [JSON](#json) - [Javascript](#javascript) - [Localizing iOS properties](#localizing-ios-properties) - [Changing the language dynamically at runtime](#changing-the-language-dynamically-at-runtime) - [iOS](#ios) - [Android](#android) - [Troubleshooting](#troubleshooting) - [Angular localization pipe and the modal context](#angular-localization-pipe-and-the-modal-context) - [Issues with WebView on Android N+](#issues-with-webview-on-android-n) - [API](#api) - [localize()](#localize) - [overrideLocale()](#overridelocale) - [androidLaunchEventLocalizationHandler()](#androidlauncheventlocalizationhandler) - [Credits](#credits) - [License](#license) ## Intro A plugin that implements internationalization (i18n) using the native capabilities of each platform. It is inspired by [nativescript-i18n](https://github.com/rborn/nativescript-i18n) ## Installation To install the plugin, run the following command in the root directory of your project. ```cli npm install @nativescript/localize ``` ## Use @nativescript/localize This section describes how to use the `@nativescript/localize` plugin in several flavors that NativeScript supports. ### Localization in NativeScript Core 1. Create a folder named `i18n` in the `app` folder, with the following structure: ``` app | i18n | en.json <-- english language | es.default.json <-- spanish language (default)     ``` `es.default.json` example: ```json { "app.name" : "Comida Rica!", "user":{ "name": "Paula" } } ``` 2. In the `main.ts` file, register the `localize` function with the `setResources` method of the [Application](https://docs.nativescript.org/api-reference/modules#application) class, as follows. ```js import { Application } from "@nativescript/core"; import { localize } from '@nativescript/localize'; Application.setResources({ L: localize }); ``` Then, use the `L` property in the markup. ```xml ``` To localize in code-behind, just call the `localize` method directly. ```js import { localize } from '@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: ```js const page = args.object; page.bindingContext = new Observable(); ``` ### Localization in Angular 1. Create a folder `i18n` in the `src` folder, with the following structure: ``` src | i18n | en.json <-- english language | fr.default.json <-- french language (default)     | es.js ``` You need to [set the default langage](#how-to-set-the-default-language) and make sure it contains the [application name](#how-to-localize-the-application-name) to avoid any errors. 2. Register the localizing module(`NativeScriptLocalizeModule`) in the `app.module.ts` file ```ts import { NgModule, NO_ERRORS_SCHEMA } from '@angular/core'; import { NativeScriptLocalizeModule } from '@nativescript/localize/angular'; import { NativeScriptModule } from '@nativescript/angular'; import { AppComponent } from './app.component'; @NgModule({ declarations: [AppComponent], bootstrap: [AppComponent], imports: [NativeScriptModule, NativeScriptLocalizeModule], schemas: [NO_ERRORS_SCHEMA], }) export class AppModule {} ``` 3. Then, in an HTML file, use the localizer as follows: ```xml