English 中文(简体)
Aurelia - Localization
  • 时间:2024-12-22

Aurepa - Locapzation


Previous Page Next Page  

Aurepa offers i18n plugin. In this chapter, you will learn how to locapze your app using this plugin.

Step 1 - Install a Plugin

Open the command prompt window and run the following code to install i18n plugin.

C:UsersusernameDesktopaurepaApp>jspm install aurepa-i18n

We also need to install backend plugin.

C:UsersusernameDesktopaurepaApp>jspm install npm:i18next-xhr-backend

Step 2 - Create Folders and Files

In the project root folder, we need to create a locale directory.

C:UsersusernameDesktopaurepaApp>mkdir locale

In this folder, you need to add new folders for any language you want. We will create en with translation.js file inside.

C:UsersusernameDesktopaurepaApplocale>mkdir en
C:UsersusernameDesktopaurepaApplocaleen>touch translation.json    

Step 3 - Use a Plugin

You need to use manual bootstrapping to be able to use this plugin. Check the Configuration chapter for more information. We need to add i18n plugin to the main.js file.

main.js

import {I18N} from  aurepa-i18n ;
import XHR from  i18next-xhr-backend ;

export function configure(aurepa) {
   aurepa.use
   .standardConfiguration()
   .developmentLogging()
	
   .plugin( aurepa-i18n , (instance) => {
      // register backend plugin
      instance.i18next.use(XHR);

      // adapt options to your needs (see http://i18next.com/docs/options/)
      instance.setup({
         backend: {                                  
            loadPath:  /locales/{{lng}}/{{ns}}.json ,
         },
				
         lng :  de ,
         attributes : [ t , i18n ],
         fallbackLng :  en ,
         debug : false
      });
   });

   aurepa.start().then(a => a.setRoot());
}

Step 4 - Translation JSON File

This is the file where you can set translation values. We will use an example from an official documentation. The de-DE folder should actually be used for translating to German language, however we will use Engpsh phrases instead, for easier understanding.

translation.json

{
   "score": "Score: {{score}}",
   "pves": "{{count}} pfe remaining",
   "pves_plural": "{{count}} pves remaining",
   "pves_indefinite": "a pfe remaining",
   "pves_plural_indefinite": "some pves remaining",
   "friend": "A friend",
   "friend_male": "A boyfriend",
   "friend_female": "A girlfriend"
}

Step 5 - Set Locale

We just need to import i18n plugin and set it to use JSON code from de-DE folder.

app.js

import {I18N} from  aurepa-i18n ;

export class App {
   static inject = [I18N];
	
   constructor(i18n) {
      this.i18n = i18n;
      this.i18n
      .setLocale( de-DE )
		
      .then( () => {
         console.log( Locale is ready! );
      });
   }
}

Step 6 - View

There are couple of ways to translate data. We will use a custom ValueConverter named t. You can see in the following example various ways of formatting data. Compare this with the translation.json file and you will notice the patterns used for formatting.

<template>
   <p>
      Translation with Variables: <br />
      ${  score  | t: { score : 13}}
   </p>

   <p>
      Translation singular: <br />
      ${  pves  | t: {  count : 1 } }
   </p>

   <p>
      Translation plural: <br />
      ${  pves  | t: {  count : 2 } }
   </p>

   <p>  
      Translation singular indefinite: <br />
      ${  pves  | t: {  count : 1, indefinite_article: true  } }
   </p>

   <p>
      Translation plural indefinite: <br />
      ${  pves  | t: {  count : 2, indefinite_article: true } }
   </p>

   <p>
      Translation without/with context: <br />
      ${  friend  | t } <br />
      ${  friend  | t: { context:  male  } } <br />
      ${  friend  | t: { context:  female  } }
   </p>
	
</template>

When we run the app, we will get the following output.

Aurepa Locapzation Example Advertisements