Ionic App之国际化(1)单个参数的处理

最近的app开发中需要考虑多语言国际化的问题,经查资料,目前大部分使用的是angular-translate.js这个组件,网站说明是这个:https://angular-translate.github.io

具体步骤:

1.下载angular-translate.min.js和angular-translate-loader-static-files.min.js,并在index.html中添加引用

    <!--多语言模块-->
    <script src="lib/angular-translate/angular-translate.min.js"></script>
    <script src="lib/angular-translate/angular-translate-loader-static-files.min.js"></script>

2.设置依赖项

var app = angular.module("ionicApp", ["ionic","pascalprecht.translate"]);

3.配置多语言,此处是加载本地化的json文件,在www目录下建立languages目录,有两个json文件:en.json和zh.json

{
  "LOGIN_REGISTER": "Login/Register",
  "A" : "电动自行车A",
  "B" : "电动三轮车B",
  "MyLa":
  {
    "length" : 2,
   "values":[
    {
      "name" : "Huanhuan","value" : "1"
    },
    {
      "name" : "Alex","value" : "2"
    }
  ]
  }
}

en.json

{
  "LOGIN_REGISTER": "登录/注册",
  "A" : "电动自行车",
  "B" : "电动三轮车",
  "MyLa":{
    "length" : 2,
    "values":[
    {
      "name" : "郭欢欢","value" : "1"
    },
    {
      "name" : "大海","value" : "2"
    }
  ]}
}

zh.json

这两个文件就是我们的多语言文件,相关内容应该日后都放在这里。

4.加载多语言文件,这个是需要提前加载的(貌似也可以延迟加载,还没研究到)

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;
  /*
     添加多语言设置
  */
  $translateProvider.useStaticFilesLoader({
      prefix: ‘languages/‘,
      suffix: ‘.json‘
  });
  $translateProvider.use(‘en‘);
})

这里的prefix,suffix和en三个参数,实际上会合成一个类似{prefix}{en}{suffix}的字符串,即languages/en.json,也就是我们的多语言文件地址

5.使用指令实现翻译效果,比如我们在view中有如下一段代码,LOGIN_REGISTER实现不同语言下的显示。

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

由于我们在第4步中默认使用的是英文en,所以第一次启动的时候对应的内容是en.json中LOGIN_REGISTER对应的值"Login/Register"

6.语言的切换,我们设置两个按钮事件,对应中文/英文的切换

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

每点击下中文按钮,LOGIN_REGISTER就即刻变为zh.json中对应的 "登录/注册",点下英文按钮,则写换到"Login/Register"

这样我们基本完成了单个值的多语言的实现.

时间: 2024-08-15 12:22:27

Ionic App之国际化(1)单个参数的处理的相关文章

Ionic App之国际化(3) json数组的处理

接上一篇Ionic App之国际化(2) json数组的处理 之后,如何处理json数据长度的问题,目前的一个折中方法是翻译长度字段. 多语言文件内容: "MyLa": { "length" : "2", "values":[ { "name" : "Huanhuan","value" : "1" }, { "name" : &

[转]Ionic + AngularJS angular-translate 国际化本地化解决方案

本文转自:http://www.cnblogs.com/maoyazhi/p/4332362.html 欢迎访问我们的网站,网站上有更多关于技术性的交流:http://www.ncloud.hk/技术分享/ionic-plus-angularjs-angular-translate-国际化本地化解决方案/ 一.手动切换语言 1.在app.js文件中首先要加上一个参数: ? 1 angular.module(’passbox’,[‘ionic’,’pascalprecht.translate’])

Ionic app升级插件开发

终于走到了写插件的这个地方了,插件的过程: 1.安装plugman插件,管理我们的程序 npm install -g plugman 2.创建插件项目appUpgrade,cd 到你的目标目录下,执行如下命令: plugman create --name appUpgrade --plugin_id com.caseStudy.plugin --plugin_version 0.0.1 其中com.caseStudy.plugin是java类的包名,以后会引用到. 执行完成后,目录下会看到新建了

Ionic + AngularJS angular-translate 国际化本地化解决方案

一.手动切换语言 1.在app.js文件中首先要加上一个参数: angular.module(’passbox’,[‘ionic’,’pascalprecht.translate’]) 2.需要引用js文件 <script src=”js/angular-translate.js”></script> 3.在edit.html文件中加入点击切换按钮: <button ng-click=”changeLanguage(‘en’)” translate=”Language_en”

[Ionic2] Device Interaction in an Ionic App with Cordova Plugins

In this lesson, we are going to learn how to interact with native components through Cordova plugins. We will walk through how to add a Cordova plugin to our application and use it to interact with our native device. In this case, we are going to ins

Ionic app 上传图片之webApi接口

App上传图片对应的webApi服务端是怎么处理的呢? using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Threading; using System.Threading.Tasks; using System.Web;

mybitas 传入单个参数_parameter

mybitas 传入单个参数,可采用这种形式.  in(${_parameter})

Mybatis单个参数的if判断(针对异常:There is no getter for property..)

我们都知道mybatis在进行参数判断的时候,直接可以用<if test=""></if> 就可以了,如下: 1.常规代码 <update id="update" parameterType="com.cq2022.zago.order.entity.Test" > update t_test_l <set > <if test="trnsctWayId != null"

springMVC怎么接受前台传过来的多种类型参数?(集合、实体、单个参数)

创建一个实体:里面包含需要接受的多种类型的参数.如实体.list集合.单个参数.(因为springMVC无法同时接受这几种参数,需要创建一个实体包含它们来接受) 如接收User(bean里面包含List)集合参数: 1.User实体类: 1.public class User { 2. private String name; 3. private String pwd; 4. private List<User> customers;//属于用户的客户群 5. //省略getter/sett