Internationalization of the Ionic app (1) handling of individual parameters

Source: Internet
Author: User

Recent app development needs to consider the problem of multi-language internationalization, through the data, most of the current use of angular-translate.js this component, the site description is this: Https://angular-translate.github.io

Specific steps:

1. Download Angular-translate.min.js and Angular-translate-loader-static-files.min.js, and add references in index.html

    <!--multi-language module--    <script src="lib/angular-translate/angular-translate.min.js" ></script>    <script src="lib/angular-translate/ Angular-translate-loader-static-files.min.js"></script>

2. Setting the Dependencies

var app = Angular.module ("ionicapp", ["ionic","  pascalprecht.translate"]);

3. Configure multiple languages, here is the loading of localized JSON files, in the WWW directory to establish the languages directory, there are two JSON files: En.json and Zh.json

{  "Login_register":"Login/register",  "A":"e-Bike a",  "B":"Electric Tricycle B",  "Myla":  {    "length":2,   "Values":[    {      "name":"Huanhuan","value":"1"    },    {      "name":"Alex","value":"2"    }  ]  }}
En.json
{  "Login_register":"Login/Register",  "A":"Electric Bicycles",  "B":"Electric Tricycle",  "Myla":{    "length":2,    "Values":[    {      "name":"Guo Huanhuan","value":"1"    },    {      "name":"Sea","value":"2"    }  ]}}
Zh.json

These two files are our multilingual files and should be placed in the future.

4. Loading multi-language files, this need to be loaded in advance (seemingly can also delay loading, not yet studied)

App. Config (function ($locationProvider, $stateProvider, $urlRouterProvider, $ionicConfigProvider, $ Scedelegateprovider, $compileProvider, $translateProvider) {$ionicConfigProvider. Tabs.style (" Standard"); $ionicConfigProvider. Tabs.position ("Bottom"); $ionicConfigProvider. Navbar.aligntitle ("Center"); $ionicConfigProvider. Views.transition ("iOS"); $ionicConfigProvider. Views.maxcache (2); $ionicConfigProvider. views.swipebackenabled (false); $ionicConfigProvider. Backbutton.text (""); $ionicConfigProvider. Backbutton.previoustitletext (false); $ionicConfigProvider. Templates.maxprefetch (5);/*URL Replace by dynamic load Urls.json 2016.10.25*/$urlRouterProvider. deferintercept (); $urlRouterProvider. Otherwise ('Tab/home'); $locationProvider. Html5mode ({enabled:false}); $stateProviderRef=$stateProvider; /*add multi-language Settings*/$translateProvider. Usestaticfilesloader ({prefix:'languages/', suffix:'. JSON'  }); $translateProvider. Use ('en');})

Here the Prefix,suffix and en three parameters, will actually synthesize a string similar to {Prefix}{en}{suffix}, that is, the Languages/en.json, that is, our multilingual file address

5. Use the instructions to achieve the translation effect, for example, we have the following code in the view, Login_register to achieve the display in different languages.

          class="sign" ng-if=! $root .user>              <a href="#/ login/">                

Since we are using the English en by default in the 4th step, the corresponding content in the first boot is En.json login_register corresponding value "Login/register"

6. Language switch, we set two button events, corresponding to the Chinese/English switch

$scope. chinese= function ()              {                $translate. Use ("zh");              }; 
$scope. english= function ()              {                $translate. Use ("en");              };

Every click on the Chinese button, Login_register will instantly become the corresponding "Login/registration" in Zh.json, click the English button, then write to "Login/register"

This allows us to basically complete a multi-language implementation of a single value.

Internationalization of the Ionic app (1) handling of individual parameters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.