angular i18n_如何在Angular中使用国际化(i18n)

本文详细介绍了如何在Angular应用中实现国际化(i18n),包括创建新Angular项目、设置翻译、创建项目模板、标记待翻译文本、构建不同语言版本,并提供了具体的步骤和示例。
摘要由CSDN通过智能技术生成

angular i18n

The consumption of modern web and mobile experiences is a worldwide thing. It isn’t just related to the locals around you and the surrounding culture. Therefore, just as you’d ensure that your design is aesthetically-pleasing and accessible, you should also ensure that your text is localized.

现代网络和移动体验的消费已成为世界性的事情。 它不仅与您周围的当地人和周围的文化有关。 因此,就像您确保设计在美学上令人愉悦且易于访问一样,您还应该确保文本已本地化。

This is the process of ensuring that your application supports multiple languages. This is traditionally done in an Angular application via multiple ways.

这是确保您的应用程序支持多种语言的过程。 传统上,这是在Angular应用程序中通过多种方式完成的。

You can, for example, use third party libraries such as ngx-translate, or, you can use the built-in i18n functionality.

例如,您可以使用第三方库(例如ngx-translate ),也可以使用内置的i18n功能。

We’ll specifically be looking at using the built-in i18n inside of this article.

我们将专门研究在本文中使用内置的i18n。

新角度项目 (New Angular Project)

Create a new Angular project with the Angular CLI to establish a common base:

使用Angular CLI创建一个新的Angular项目以建立公共基础:

# Install the Angular CLI globally
$ npm i @angular/cli -g

# Create a new Angular project && change directory
$ ng new AngularInt

> N
> SCSS

$ cd AngularInt

# Open this up in VS Code and Serve
$ code . && ng serve

翻译 (Translations)

By default, Angular considers everything to be in en-US locale. We’ll have to add other locales and update our configuration to support this. You can see a list of various locales here: http://cldr.unicode.org/core-spec#Unicode_Language_and_Locale_Identifiers.

默认情况下,Angular会将所有内容都视为在en-US语言环境中。 我们必须添加其他区域设置并更新我们的配置以支持此设置。 您可以在此处查看各种语言环境的列表: http : //cldr.unicode.org/core-spec#Unicode_Language_and_Locale_Identifiers

项目模板 (Project Templates)

To create the basis for our translation project, head over to app.component.html and create the following template:

要为我们的翻译项目创建基础,请转到app.component.html并创建以下模板:

<section>
  <article>
    <h1>Under Construction!</h1>
    <p>This page is under construction.</p>
  </article>
</section>

We can also add some css to make it a little more magic, inside of app.component.scss:

我们还可以在app.component.scss内部添加一些CSS使其更具魔力:

section {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: #8e2de2; /* fallback for old browsers */
  background: -webkit-linear-gradient(to right, #4a00e0, #8e2de2); /* Chrome 10-25, Safari 5.1-6 */
  background: linear-gradient(to right, #4a00e0, #8e2de2);
}

article {
  text-align: center;
  color: white;
  border: 1px solid white;
  padding: 40px;
  box-shadow: 1px 1px 100px 10px rgba(0, 0, 0, 0.8);
}

Finally, inside of styles.scss let’s default the html and body styles:

最后,在styles.scss内部,我们默认使用html和body样式:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现AngularJS 1.4.4的i18n国际化可以通过使用angular-translate库来完成。下面是大致的步骤: 1. 安装angular-translate库,在命令行执行以下命令: ``` npm install angular-translate --save ``` 2. 在HTML引入angular-translate库的js文件,例如: ```html <script src="bower_components/angular-translate/angular-translate.js"></script> ``` 3. 在AngularJS应用程序注册'pascalprecht.translate'模块,例如: ```javascript var myApp = angular.module('myApp', ['pascalprecht.translate']); ``` 4. 配置语言文件,例如: ```javascript myApp.config(function($translateProvider) { $translateProvider.translations('en', { TITLE: 'Hello', FOO: 'This is a paragraph.', BUTTON_LANG_EN: 'english', BUTTON_LANG_DE: 'german' }); $translateProvider.translations('de', { TITLE: 'Hallo', FOO: 'Dies ist ein Absatz.', BUTTON_LANG_EN: 'englisch', BUTTON_LANG_DE: 'deutsch' }); $translateProvider.preferredLanguage('en'); }); ``` 5. 在HTML使用AngularJS提供的指令来实现i18n,例如: ```html <div ng-controller="Ctrl"> <h1>{{ 'TITLE' | translate }}</h1> <p>{{ 'FOO' | translate }}</p> <button ng-click="changeLanguage('en')" translate="BUTTON_LANG_EN"></button> <button ng-click="changeLanguage('de')" translate="BUTTON_LANG_DE"></button> </div> ``` 6. 在控制器定义changeLanguage方法,例如: ```javascript myApp.controller('Ctrl', function ($scope, $translate) { $scope.changeLanguage = function (key) { $translate.use(key); }; }); ``` 以上是基于AngularJS 1.4.4和开发工具Eclipse实现i18n国际化的大致步骤,具体实现会因应用程序的需求而有所不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值