2017-05-26 15:28:06 -04:00
# Internationalization (i18n)
2017-03-31 19:57:13 -04:00
2018-03-07 02:42:49 -05:00
# 国际化( i18n)
2017-11-02 17:22:09 -04:00
Application internationalization is a many-faceted area of development, focused on making
applications available and user-friendly to a worldwide audience. This page describes Angular's
internationalization (i18n) tools, which can help you make your app available in multiple languages.
2017-03-31 19:57:13 -04:00
2018-03-21 04:21:28 -04:00
应用程序的国际化涉及到开发的很多方面,主要是如何让应用可以被全世界的用户使用而且用起来比较友好。
本页面讲的是 Angular 的*国际化*( *i18n*)工具,它可以帮助你使用多个语言发布应用。
2017-11-02 17:22:09 -04:00
See the < live-example downloadOnly name = "i18n" > i18n Example< / live-example > for a simple example of
an AOT-compiled app, translated into French.
2017-02-22 13:09:39 -05:00
2018-03-07 02:42:49 -05:00
可以把这个翻译为法语版的 AOT 应用< live-example downloadOnly name = "i18n" > i18n 例子< / live-example > 作为一个简单的例子。
2017-02-22 13:09:39 -05:00
{@a angular-i18n}
2018-03-03 08:06:01 -05:00
2017-11-02 17:22:09 -04:00
## Angular and i18n
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
## Angular 与 i18n
2017-11-02 17:22:09 -04:00
Angular simplifies the following aspects of internationalization:
2018-03-03 08:06:01 -05:00
2018-03-21 04:21:28 -04:00
Angular 简化了国际化工作的下列几个方面:
2017-11-02 17:22:09 -04:00
* Displaying dates, number, percentages, and currencies in a local format.
2018-03-03 08:06:01 -05:00
2018-03-21 04:21:28 -04:00
用本地格式显示日期、数字、百分比以及货币。
2017-11-02 17:22:09 -04:00
* Translating text in component templates.
2018-03-03 08:06:01 -05:00
2018-03-21 04:21:28 -04:00
翻译组件模板中的文本。
2017-11-02 17:22:09 -04:00
* Handling plural forms of words.
2018-03-03 08:06:01 -05:00
2018-03-21 04:21:28 -04:00
处理单词的复数形式。
2017-11-02 17:22:09 -04:00
* Handling alternative text.
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
处理候选文本。
2017-11-02 17:22:09 -04:00
This document focuses on [**Angular CLI** ](https://cli.angular.io/ ) projects, in which the Angular
CLI generates most of the boilerplate necessary to write your app in multiple languages.
2017-07-22 23:51:25 -04:00
2018-03-21 04:21:28 -04:00
本文档集中讲解 [**Angular CLI** ](https://cli.angular.io/ ) 项目, Angular CLI 为它生成了写多语言应用时必须的大部分样板代码。
2017-11-02 17:22:09 -04:00
{@a setting-up-locale}
2018-03-03 08:06:01 -05:00
2017-11-02 17:22:09 -04:00
## Setting up the locale of your app
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
## 为你的应用设置地区( locale)
2017-11-02 17:22:09 -04:00
A locale is an identifier (id) that refers to a set of user preferences that tend to be shared
within a region of the world, such as country. This document refers to a locale identifier as a
"locale" or "locale id".
2017-07-22 23:51:25 -04:00
2018-03-21 04:21:28 -04:00
地区是一个唯一性标识,它代表的是世界某个区域(比如国家)中共享的一组用户首选项。本文档中提到的“地区”就是指这个地区标识。
2017-11-02 17:22:09 -04:00
A Unicode locale identifier is composed of a Unicode language identifier and (optionally) the
character `-` followed by a locale extension. (For historical reasons the character `_` is supported
as an alternative to `-` .) For example, in the locale id `fr-CA` the `fr` refers to the French
language identifier, and the `CA` refers to the locale extension Canada.
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
Unicode 的地区标识是由 Unicode 语言标识、一个可选的 `-` 字符,后跟一个地区扩展项组合而成的。(由于历史的原因,还支持用 `_` 作为 `-` 的替代品。)比如,地区标识 `fr-CA` 中的 `fr` 代表法语的语言标识,而 `CA` 代表的是加拿大的语言扩展。
2017-11-02 17:22:09 -04:00
< div class = "alert is-critical" >
2017-07-22 23:51:25 -04:00
2017-11-02 17:22:09 -04:00
Angular follows the Unicode LDML convention that uses stable identifiers (Unicode locale identifiers)
based on the norm [BCP47 ](http://www.rfc-editor.org/rfc/bcp/bcp47.txt ). It is very important that
you follow this convention when you define your locale, because the Angular i18n tools use this
locale id to find the correct corresponding locale data.
2017-08-06 02:08:44 -04:00
2018-03-21 04:21:28 -04:00
Angular 遵循 Unicode 的 LDML 惯例,它使用基于 [BCP47 ](http://www.rfc-editor.org/rfc/bcp/bcp47.txt ) 标准的稳定标识( Unicode 的地区标识)。当你要定义自己的地区时,遵循这个惯例非常重要,因为 Angular 的 i18n 工具会使用这种地区标识来查找相应的本地化数据。
2017-07-22 23:51:25 -04:00
2017-04-10 11:51:13 -04:00
< / div >
2017-03-27 11:08:53 -04:00
2017-11-02 17:22:09 -04:00
By default, Angular uses the locale `en-US` , which is English as spoken in the United States of America.
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
默认情况下, Angular 使用的地区标识是 `en-US` ,它表示美国英语。
2017-11-02 17:22:09 -04:00
To set your app's locale to another value, use the CLI parameter `--locale` with the value
of the locale id that you want to use:
2017-07-22 23:51:25 -04:00
2018-03-21 04:21:28 -04:00
要把你的应用的地区改为其它值,可以使用 CLI 参数 `--locale` 来传入你要使用的地区标识:
2017-11-02 17:22:09 -04:00
< code-example language = "sh" class = "code-shell" >
2018-03-03 08:06:01 -05:00
2017-11-02 17:22:09 -04:00
ng serve --aot --locale fr
2018-03-03 08:06:01 -05:00
2017-11-02 17:22:09 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
If you use JIT, you also need to define the `LOCALE_ID` provider in your main module:
2017-07-22 23:51:25 -04:00
2018-03-21 04:21:28 -04:00
如果要使用 JIT, 你就得在你的主模块中定义 `LOCALE_ID` 这个服务提供商:
2017-11-02 17:22:09 -04:00
< code-example path = "i18n/doc-files/app.module.ts" title = "src/app/app.module.ts" linenums = "false" >
2017-02-22 13:09:39 -05:00
2018-03-03 08:06:01 -05:00
< / code-example >
2017-07-22 23:51:25 -04:00
2017-11-02 17:22:09 -04:00
For more information about Unicode locale identifiers, see the
[CLDR core spec ](http://cldr.unicode.org/core-spec#Unicode_Language_and_Locale_Identifiers ).
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
要了解关于 Unicode 地区标识的更多信息,参见 [CLDR 核心规范 ](http://cldr.unicode.org/core-spec#Unicode_Language_and_Locale_Identifiers )。
2017-11-02 17:22:09 -04:00
For a complete list of locales supported by Angular, see
[the Angular repository ](https://github.com/angular/angular/tree/master/packages/common/locales ).
2017-07-22 23:51:25 -04:00
2018-03-21 04:21:28 -04:00
要查看 Angular 支持的地区总表,参见 [Angular 源码仓库 ](https://github.com/angular/angular/tree/master/packages/common/locales )。
2017-07-04 11:07:11 -04:00
The locale identifiers used by CLDR and Angular are based on [BCP47 ](http://www.rfc-editor.org/rfc/bcp/bcp47.txt ).
These specifications change over time; the following table maps previous identifiers to current ones at
time of writing:
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
CLDR 和 Angular 使用的地区标识基于 [BCP47 ](http://www.rfc-editor.org/rfc/bcp/bcp47.txt )。
这些规范可能会随时间而变化,下表中是本文编写时地区标识的新旧版本对照表:
2018-03-21 22:55:55 -04:00
| < t > Locale name< / t > < t > 地区名称< / t > | < t > Old locale id< / t > < t > 旧 ID< / t > | < t > New locale id< / t > < t > 新 ID< / t > |
2017-07-04 11:07:11 -04:00
|-------------------------------|-------------------|---------------|
2018-03-21 22:55:55 -04:00
| < t > Indonesian< / t > < t > 印度尼西亚< / t > | in | id |
| < t > Hebrew< / t > < t > 希伯来< / t > | iw | he |
| < t > Romanian Moldova< / t > < t > 罗马尼亚摩尔多瓦< / t > | mo | ro-MD |
| < t > Norwegian Bokmål< / t > < t > 挪威 Bokmål< / t > | no, no-NO | nb |
| < t > Serbian Latin< / t > < t > 塞尔维亚拉丁语< / t > | sh | sr-Latn |
| < t > Filipino< / t > < t > 菲律宾< / t > | tl | fil |
| < t > Portuguese Brazil< / t > < t > 葡萄牙巴西< / t > | pt-BR | pt |
| < t > Chinese Simplified< / t > < t > 中文简体< / t > | zh-cn, zh-Hans-CN | zh-Hans |
| < t > Chinese Traditional< / t > < t > 中文繁体< / t > | zh-tw, zh-Hant-TW | zh-Hant |
| < t > Chinese Traditional Hong Kong< / t > < t > 中文繁体(香港)< / t > | zh-hk | zh-Hant-HK |
2017-07-22 23:51:25 -04:00
feat(common): drop use of the Intl API to improve browser support (#18284)
BREAKING CHANGE: Because of multiple bugs and browser inconsistencies, we have dropped the intl api in favor of data exported from the Unicode Common Locale Data Repository (CLDR).
Unfortunately we had to change the i18n pipes (date, number, currency, percent) and there are some breaking changes.
1. I18n pipes
* Breaking change:
- By default Angular now only contains locale data for the language `en-US`, if you set the value of `LOCALE_ID` to another locale, you will have to import new locale data for this language because we don't use the intl API anymore.
* Features:
- you don't need to use the intl polyfill for Angular anymore.
- all i18n pipes now have an additional last parameter `locale` which allows you to use a specific locale instead of the one defined in the token `LOCALE_ID` (whose value is `en-US` by default).
- the new locale data extracted from CLDR are now available to developers as well and can be used through an API (which should be especially useful for library authors).
- you can still use the old pipes for now, but their names have been changed and they are no longer included in the `CommonModule`. To use them, you will have to import the `DeprecatedI18NPipesModule` after the `CommonModule` (the order is important):
```ts
import { NgModule } from '@angular/core';
import { CommonModule, DeprecatedI18NPipesModule } from '@angular/common';
@NgModule({
imports: [
CommonModule,
// import deprecated module after
DeprecatedI18NPipesModule
]
})
export class AppModule { }
```
Dont forget that you will still need to import the intl API polyfill if you want to use those deprecated pipes.
2. Date pipe
* Breaking changes:
- the predefined formats (`short`, `shortTime`, `shortDate`, `medium`, ...) now use the patterns given by CLDR (like it was in AngularJS) instead of the ones from the intl API. You might notice some changes, e.g. `shortDate` will be `8/15/17` instead of `8/15/2017` for `en-US`.
- the narrow version of eras is now `GGGGG` instead of `G`, the format `G` is now similar to `GG` and `GGG`.
- the narrow version of months is now `MMMMM` instead of `L`, the format `L` is now the short standalone version of months.
- the narrow version of the week day is now `EEEEE` instead of `E`, the format `E` is now similar to `EE` and `EEE`.
- the timezone `z` will now fallback to `O` and output `GMT+1` instead of the complete zone name (e.g. `Pacific Standard Time`), this is because the quantity of data required to have all the zone names in all of the existing locales is too big.
- the timezone `Z` will now output the ISO8601 basic format, e.g. `+0100`, you should now use `ZZZZ` to get `GMT+01:00`.
| Field type | Format | Example value | v4 | v5 |
|------------|---------------|-----------------------|----|---------------|
| Eras | Narrow | A for AD | G | GGGGG |
| Months | Narrow | S for September | L | MMMMM |
| Week day | Narrow | M for Monday | E | EEEEE |
| Timezone | Long location | Pacific Standard Time | z | Not available |
| Timezone | Long GMT | GMT+01:00 | Z | ZZZZ |
* Features
- new predefined formats `long`, `full`, `longTime`, `fullTime`.
- the format `yyy` is now supported, e.g. the year `52` will be `052` and the year `2017` will be `2017`.
- standalone months are now supported with the formats `L` to `LLLLL`.
- week of the year is now supported with the formats `w` and `ww`, e.g. weeks `5` and `05`.
- week of the month is now supported with the format `W`, e.g. week `3`.
- fractional seconds are now supported with the format `S` to `SSS`.
- day periods for AM/PM now supports additional formats `aa`, `aaa`, `aaaa` and `aaaaa`. The formats `a` to `aaa` are similar, while `aaaa` is the wide version if available (e.g. `ante meridiem` for `am`), or equivalent to `a` otherwise, and `aaaaa` is the narrow version (e.g. `a` for `am`).
- extra day periods are now supported with the formats `b` to `bbbbb` (and `B` to `BBBBB` for the standalone equivalents), e.g. `morning`, `noon`, `afternoon`, ....
- the short non-localized timezones are now available with the format `O` to `OOOO`. The formats `O` to `OOO` will output `GMT+1` while the format `OOOO` will be `GMT+01:00`.
- the ISO8601 basic time zones are now available with the formats `Z` to `ZZZZZ`. The formats `Z` to `ZZZ` will output `+0100`, while the format `ZZZZ` will be `GMT+01:00` and `ZZZZZ` will be `+01:00`.
* Bug fixes
- the date pipe will now work exactly the same across all browsers, which will fix a lot of bugs for safari and IE.
- eras can now be used on their own without the date, e.g. the format `GG` will be `AD` instead of `8 15, 2017 AD`.
3. Currency pipe
* Breaking change:
- the default value for `symbolDisplay` is now `symbol` instead of `code`. This means that by default you will see `$4.99` for `en-US` instead of `USD4.99` previously.
* Deprecation:
- the second parameter of the currency pipe (`symbolDisplay`) is no longer a boolean, it now takes the values `code`, `symbol` or `symbol-narrow`. A boolean value is still valid for now, but it is deprecated and it will print a warning message in the console.
* Features:
- you can now choose between `code`, `symbol` or `symbol-narrow` which gives you access to more options for some currencies (e.g. the canadian dollar with the code `CAD` has the symbol `CA$` and the symbol-narrow `$`).
4. Percent pipe
* Breaking change
- if you don't specify the number of digits to round to, the local format will be used (and it usually rounds numbers to 0 digits, instead of not rounding previously), e.g. `{{ 3.141592 | percent }}` will output `314%` for the locale `en-US` instead of `314.1592%` previously.
Fixes #10809, #9524, #7008, #9324, #7590, #6724, #3429, #17576, #17478, #17319, #17200, #16838, #16624, #16625, #16591, #14131, #12632, #11376, #11187
PR Close #18284
2017-08-22 14:30:59 -04:00
## i18n pipes
2018-03-21 04:21:28 -04:00
## i18n 管道
feat(common): drop use of the Intl API to improve browser support (#18284)
BREAKING CHANGE: Because of multiple bugs and browser inconsistencies, we have dropped the intl api in favor of data exported from the Unicode Common Locale Data Repository (CLDR).
Unfortunately we had to change the i18n pipes (date, number, currency, percent) and there are some breaking changes.
1. I18n pipes
* Breaking change:
- By default Angular now only contains locale data for the language `en-US`, if you set the value of `LOCALE_ID` to another locale, you will have to import new locale data for this language because we don't use the intl API anymore.
* Features:
- you don't need to use the intl polyfill for Angular anymore.
- all i18n pipes now have an additional last parameter `locale` which allows you to use a specific locale instead of the one defined in the token `LOCALE_ID` (whose value is `en-US` by default).
- the new locale data extracted from CLDR are now available to developers as well and can be used through an API (which should be especially useful for library authors).
- you can still use the old pipes for now, but their names have been changed and they are no longer included in the `CommonModule`. To use them, you will have to import the `DeprecatedI18NPipesModule` after the `CommonModule` (the order is important):
```ts
import { NgModule } from '@angular/core';
import { CommonModule, DeprecatedI18NPipesModule } from '@angular/common';
@NgModule({
imports: [
CommonModule,
// import deprecated module after
DeprecatedI18NPipesModule
]
})
export class AppModule { }
```
Dont forget that you will still need to import the intl API polyfill if you want to use those deprecated pipes.
2. Date pipe
* Breaking changes:
- the predefined formats (`short`, `shortTime`, `shortDate`, `medium`, ...) now use the patterns given by CLDR (like it was in AngularJS) instead of the ones from the intl API. You might notice some changes, e.g. `shortDate` will be `8/15/17` instead of `8/15/2017` for `en-US`.
- the narrow version of eras is now `GGGGG` instead of `G`, the format `G` is now similar to `GG` and `GGG`.
- the narrow version of months is now `MMMMM` instead of `L`, the format `L` is now the short standalone version of months.
- the narrow version of the week day is now `EEEEE` instead of `E`, the format `E` is now similar to `EE` and `EEE`.
- the timezone `z` will now fallback to `O` and output `GMT+1` instead of the complete zone name (e.g. `Pacific Standard Time`), this is because the quantity of data required to have all the zone names in all of the existing locales is too big.
- the timezone `Z` will now output the ISO8601 basic format, e.g. `+0100`, you should now use `ZZZZ` to get `GMT+01:00`.
| Field type | Format | Example value | v4 | v5 |
|------------|---------------|-----------------------|----|---------------|
| Eras | Narrow | A for AD | G | GGGGG |
| Months | Narrow | S for September | L | MMMMM |
| Week day | Narrow | M for Monday | E | EEEEE |
| Timezone | Long location | Pacific Standard Time | z | Not available |
| Timezone | Long GMT | GMT+01:00 | Z | ZZZZ |
* Features
- new predefined formats `long`, `full`, `longTime`, `fullTime`.
- the format `yyy` is now supported, e.g. the year `52` will be `052` and the year `2017` will be `2017`.
- standalone months are now supported with the formats `L` to `LLLLL`.
- week of the year is now supported with the formats `w` and `ww`, e.g. weeks `5` and `05`.
- week of the month is now supported with the format `W`, e.g. week `3`.
- fractional seconds are now supported with the format `S` to `SSS`.
- day periods for AM/PM now supports additional formats `aa`, `aaa`, `aaaa` and `aaaaa`. The formats `a` to `aaa` are similar, while `aaaa` is the wide version if available (e.g. `ante meridiem` for `am`), or equivalent to `a` otherwise, and `aaaaa` is the narrow version (e.g. `a` for `am`).
- extra day periods are now supported with the formats `b` to `bbbbb` (and `B` to `BBBBB` for the standalone equivalents), e.g. `morning`, `noon`, `afternoon`, ....
- the short non-localized timezones are now available with the format `O` to `OOOO`. The formats `O` to `OOO` will output `GMT+1` while the format `OOOO` will be `GMT+01:00`.
- the ISO8601 basic time zones are now available with the formats `Z` to `ZZZZZ`. The formats `Z` to `ZZZ` will output `+0100`, while the format `ZZZZ` will be `GMT+01:00` and `ZZZZZ` will be `+01:00`.
* Bug fixes
- the date pipe will now work exactly the same across all browsers, which will fix a lot of bugs for safari and IE.
- eras can now be used on their own without the date, e.g. the format `GG` will be `AD` instead of `8 15, 2017 AD`.
3. Currency pipe
* Breaking change:
- the default value for `symbolDisplay` is now `symbol` instead of `code`. This means that by default you will see `$4.99` for `en-US` instead of `USD4.99` previously.
* Deprecation:
- the second parameter of the currency pipe (`symbolDisplay`) is no longer a boolean, it now takes the values `code`, `symbol` or `symbol-narrow`. A boolean value is still valid for now, but it is deprecated and it will print a warning message in the console.
* Features:
- you can now choose between `code`, `symbol` or `symbol-narrow` which gives you access to more options for some currencies (e.g. the canadian dollar with the code `CAD` has the symbol `CA$` and the symbol-narrow `$`).
4. Percent pipe
* Breaking change
- if you don't specify the number of digits to round to, the local format will be used (and it usually rounds numbers to 0 digits, instead of not rounding previously), e.g. `{{ 3.141592 | percent }}` will output `314%` for the locale `en-US` instead of `314.1592%` previously.
Fixes #10809, #9524, #7008, #9324, #7590, #6724, #3429, #17576, #17478, #17319, #17200, #16838, #16624, #16625, #16591, #14131, #12632, #11376, #11187
PR Close #18284
2017-08-22 14:30:59 -04:00
Angular pipes can help you with internationalization: the `DatePipe` , `CurrencyPipe` , `DecimalPipe`
2017-11-02 17:22:09 -04:00
and `PercentPipe` use locale data to format data based on the `LOCALE_ID` .
feat(common): drop use of the Intl API to improve browser support (#18284)
BREAKING CHANGE: Because of multiple bugs and browser inconsistencies, we have dropped the intl api in favor of data exported from the Unicode Common Locale Data Repository (CLDR).
Unfortunately we had to change the i18n pipes (date, number, currency, percent) and there are some breaking changes.
1. I18n pipes
* Breaking change:
- By default Angular now only contains locale data for the language `en-US`, if you set the value of `LOCALE_ID` to another locale, you will have to import new locale data for this language because we don't use the intl API anymore.
* Features:
- you don't need to use the intl polyfill for Angular anymore.
- all i18n pipes now have an additional last parameter `locale` which allows you to use a specific locale instead of the one defined in the token `LOCALE_ID` (whose value is `en-US` by default).
- the new locale data extracted from CLDR are now available to developers as well and can be used through an API (which should be especially useful for library authors).
- you can still use the old pipes for now, but their names have been changed and they are no longer included in the `CommonModule`. To use them, you will have to import the `DeprecatedI18NPipesModule` after the `CommonModule` (the order is important):
```ts
import { NgModule } from '@angular/core';
import { CommonModule, DeprecatedI18NPipesModule } from '@angular/common';
@NgModule({
imports: [
CommonModule,
// import deprecated module after
DeprecatedI18NPipesModule
]
})
export class AppModule { }
```
Dont forget that you will still need to import the intl API polyfill if you want to use those deprecated pipes.
2. Date pipe
* Breaking changes:
- the predefined formats (`short`, `shortTime`, `shortDate`, `medium`, ...) now use the patterns given by CLDR (like it was in AngularJS) instead of the ones from the intl API. You might notice some changes, e.g. `shortDate` will be `8/15/17` instead of `8/15/2017` for `en-US`.
- the narrow version of eras is now `GGGGG` instead of `G`, the format `G` is now similar to `GG` and `GGG`.
- the narrow version of months is now `MMMMM` instead of `L`, the format `L` is now the short standalone version of months.
- the narrow version of the week day is now `EEEEE` instead of `E`, the format `E` is now similar to `EE` and `EEE`.
- the timezone `z` will now fallback to `O` and output `GMT+1` instead of the complete zone name (e.g. `Pacific Standard Time`), this is because the quantity of data required to have all the zone names in all of the existing locales is too big.
- the timezone `Z` will now output the ISO8601 basic format, e.g. `+0100`, you should now use `ZZZZ` to get `GMT+01:00`.
| Field type | Format | Example value | v4 | v5 |
|------------|---------------|-----------------------|----|---------------|
| Eras | Narrow | A for AD | G | GGGGG |
| Months | Narrow | S for September | L | MMMMM |
| Week day | Narrow | M for Monday | E | EEEEE |
| Timezone | Long location | Pacific Standard Time | z | Not available |
| Timezone | Long GMT | GMT+01:00 | Z | ZZZZ |
* Features
- new predefined formats `long`, `full`, `longTime`, `fullTime`.
- the format `yyy` is now supported, e.g. the year `52` will be `052` and the year `2017` will be `2017`.
- standalone months are now supported with the formats `L` to `LLLLL`.
- week of the year is now supported with the formats `w` and `ww`, e.g. weeks `5` and `05`.
- week of the month is now supported with the format `W`, e.g. week `3`.
- fractional seconds are now supported with the format `S` to `SSS`.
- day periods for AM/PM now supports additional formats `aa`, `aaa`, `aaaa` and `aaaaa`. The formats `a` to `aaa` are similar, while `aaaa` is the wide version if available (e.g. `ante meridiem` for `am`), or equivalent to `a` otherwise, and `aaaaa` is the narrow version (e.g. `a` for `am`).
- extra day periods are now supported with the formats `b` to `bbbbb` (and `B` to `BBBBB` for the standalone equivalents), e.g. `morning`, `noon`, `afternoon`, ....
- the short non-localized timezones are now available with the format `O` to `OOOO`. The formats `O` to `OOO` will output `GMT+1` while the format `OOOO` will be `GMT+01:00`.
- the ISO8601 basic time zones are now available with the formats `Z` to `ZZZZZ`. The formats `Z` to `ZZZ` will output `+0100`, while the format `ZZZZ` will be `GMT+01:00` and `ZZZZZ` will be `+01:00`.
* Bug fixes
- the date pipe will now work exactly the same across all browsers, which will fix a lot of bugs for safari and IE.
- eras can now be used on their own without the date, e.g. the format `GG` will be `AD` instead of `8 15, 2017 AD`.
3. Currency pipe
* Breaking change:
- the default value for `symbolDisplay` is now `symbol` instead of `code`. This means that by default you will see `$4.99` for `en-US` instead of `USD4.99` previously.
* Deprecation:
- the second parameter of the currency pipe (`symbolDisplay`) is no longer a boolean, it now takes the values `code`, `symbol` or `symbol-narrow`. A boolean value is still valid for now, but it is deprecated and it will print a warning message in the console.
* Features:
- you can now choose between `code`, `symbol` or `symbol-narrow` which gives you access to more options for some currencies (e.g. the canadian dollar with the code `CAD` has the symbol `CA$` and the symbol-narrow `$`).
4. Percent pipe
* Breaking change
- if you don't specify the number of digits to round to, the local format will be used (and it usually rounds numbers to 0 digits, instead of not rounding previously), e.g. `{{ 3.141592 | percent }}` will output `314%` for the locale `en-US` instead of `314.1592%` previously.
Fixes #10809, #9524, #7008, #9324, #7590, #6724, #3429, #17576, #17478, #17319, #17200, #16838, #16624, #16625, #16591, #14131, #12632, #11376, #11187
PR Close #18284
2017-08-22 14:30:59 -04:00
2018-03-21 04:21:28 -04:00
Angular 的管道可以帮助你进行国际化:`DatePipe`、`CurrencyPipe`、`DecimalPipe` 和 `PercentPipe` 都使用本地化数据来根据 `LOCALE_ID` 格式化数据。
2017-11-02 17:22:09 -04:00
By default, Angular only contains locale data for `en-US` . If you set the value of
`LOCALE_ID` to another locale, you must import locale data for that new locale.
The CLI imports the locale data for you when you use the parameter `--locale` with `ng serve` and
`ng build` .
feat(common): drop use of the Intl API to improve browser support (#18284)
BREAKING CHANGE: Because of multiple bugs and browser inconsistencies, we have dropped the intl api in favor of data exported from the Unicode Common Locale Data Repository (CLDR).
Unfortunately we had to change the i18n pipes (date, number, currency, percent) and there are some breaking changes.
1. I18n pipes
* Breaking change:
- By default Angular now only contains locale data for the language `en-US`, if you set the value of `LOCALE_ID` to another locale, you will have to import new locale data for this language because we don't use the intl API anymore.
* Features:
- you don't need to use the intl polyfill for Angular anymore.
- all i18n pipes now have an additional last parameter `locale` which allows you to use a specific locale instead of the one defined in the token `LOCALE_ID` (whose value is `en-US` by default).
- the new locale data extracted from CLDR are now available to developers as well and can be used through an API (which should be especially useful for library authors).
- you can still use the old pipes for now, but their names have been changed and they are no longer included in the `CommonModule`. To use them, you will have to import the `DeprecatedI18NPipesModule` after the `CommonModule` (the order is important):
```ts
import { NgModule } from '@angular/core';
import { CommonModule, DeprecatedI18NPipesModule } from '@angular/common';
@NgModule({
imports: [
CommonModule,
// import deprecated module after
DeprecatedI18NPipesModule
]
})
export class AppModule { }
```
Dont forget that you will still need to import the intl API polyfill if you want to use those deprecated pipes.
2. Date pipe
* Breaking changes:
- the predefined formats (`short`, `shortTime`, `shortDate`, `medium`, ...) now use the patterns given by CLDR (like it was in AngularJS) instead of the ones from the intl API. You might notice some changes, e.g. `shortDate` will be `8/15/17` instead of `8/15/2017` for `en-US`.
- the narrow version of eras is now `GGGGG` instead of `G`, the format `G` is now similar to `GG` and `GGG`.
- the narrow version of months is now `MMMMM` instead of `L`, the format `L` is now the short standalone version of months.
- the narrow version of the week day is now `EEEEE` instead of `E`, the format `E` is now similar to `EE` and `EEE`.
- the timezone `z` will now fallback to `O` and output `GMT+1` instead of the complete zone name (e.g. `Pacific Standard Time`), this is because the quantity of data required to have all the zone names in all of the existing locales is too big.
- the timezone `Z` will now output the ISO8601 basic format, e.g. `+0100`, you should now use `ZZZZ` to get `GMT+01:00`.
| Field type | Format | Example value | v4 | v5 |
|------------|---------------|-----------------------|----|---------------|
| Eras | Narrow | A for AD | G | GGGGG |
| Months | Narrow | S for September | L | MMMMM |
| Week day | Narrow | M for Monday | E | EEEEE |
| Timezone | Long location | Pacific Standard Time | z | Not available |
| Timezone | Long GMT | GMT+01:00 | Z | ZZZZ |
* Features
- new predefined formats `long`, `full`, `longTime`, `fullTime`.
- the format `yyy` is now supported, e.g. the year `52` will be `052` and the year `2017` will be `2017`.
- standalone months are now supported with the formats `L` to `LLLLL`.
- week of the year is now supported with the formats `w` and `ww`, e.g. weeks `5` and `05`.
- week of the month is now supported with the format `W`, e.g. week `3`.
- fractional seconds are now supported with the format `S` to `SSS`.
- day periods for AM/PM now supports additional formats `aa`, `aaa`, `aaaa` and `aaaaa`. The formats `a` to `aaa` are similar, while `aaaa` is the wide version if available (e.g. `ante meridiem` for `am`), or equivalent to `a` otherwise, and `aaaaa` is the narrow version (e.g. `a` for `am`).
- extra day periods are now supported with the formats `b` to `bbbbb` (and `B` to `BBBBB` for the standalone equivalents), e.g. `morning`, `noon`, `afternoon`, ....
- the short non-localized timezones are now available with the format `O` to `OOOO`. The formats `O` to `OOO` will output `GMT+1` while the format `OOOO` will be `GMT+01:00`.
- the ISO8601 basic time zones are now available with the formats `Z` to `ZZZZZ`. The formats `Z` to `ZZZ` will output `+0100`, while the format `ZZZZ` will be `GMT+01:00` and `ZZZZZ` will be `+01:00`.
* Bug fixes
- the date pipe will now work exactly the same across all browsers, which will fix a lot of bugs for safari and IE.
- eras can now be used on their own without the date, e.g. the format `GG` will be `AD` instead of `8 15, 2017 AD`.
3. Currency pipe
* Breaking change:
- the default value for `symbolDisplay` is now `symbol` instead of `code`. This means that by default you will see `$4.99` for `en-US` instead of `USD4.99` previously.
* Deprecation:
- the second parameter of the currency pipe (`symbolDisplay`) is no longer a boolean, it now takes the values `code`, `symbol` or `symbol-narrow`. A boolean value is still valid for now, but it is deprecated and it will print a warning message in the console.
* Features:
- you can now choose between `code`, `symbol` or `symbol-narrow` which gives you access to more options for some currencies (e.g. the canadian dollar with the code `CAD` has the symbol `CA$` and the symbol-narrow `$`).
4. Percent pipe
* Breaking change
- if you don't specify the number of digits to round to, the local format will be used (and it usually rounds numbers to 0 digits, instead of not rounding previously), e.g. `{{ 3.141592 | percent }}` will output `314%` for the locale `en-US` instead of `314.1592%` previously.
Fixes #10809, #9524, #7008, #9324, #7590, #6724, #3429, #17576, #17478, #17319, #17200, #16838, #16624, #16625, #16591, #14131, #12632, #11376, #11187
PR Close #18284
2017-08-22 14:30:59 -04:00
2018-03-21 04:21:28 -04:00
默认情况下, Angular 只包含 `en-US` 的本地化数据。如果你要把 `LOCALE_ID` 的值设置为其它地区,就必须为那个新地区导入本地化数据。
当你使用 `ng serve` 和 `ng build` 的 `--locale` 参数时, CLI 会自动帮你导入相应的本地化数据。
2017-11-02 17:22:09 -04:00
If you want to import locale data for other languages, you can do it manually:
feat(common): drop use of the Intl API to improve browser support (#18284)
BREAKING CHANGE: Because of multiple bugs and browser inconsistencies, we have dropped the intl api in favor of data exported from the Unicode Common Locale Data Repository (CLDR).
Unfortunately we had to change the i18n pipes (date, number, currency, percent) and there are some breaking changes.
1. I18n pipes
* Breaking change:
- By default Angular now only contains locale data for the language `en-US`, if you set the value of `LOCALE_ID` to another locale, you will have to import new locale data for this language because we don't use the intl API anymore.
* Features:
- you don't need to use the intl polyfill for Angular anymore.
- all i18n pipes now have an additional last parameter `locale` which allows you to use a specific locale instead of the one defined in the token `LOCALE_ID` (whose value is `en-US` by default).
- the new locale data extracted from CLDR are now available to developers as well and can be used through an API (which should be especially useful for library authors).
- you can still use the old pipes for now, but their names have been changed and they are no longer included in the `CommonModule`. To use them, you will have to import the `DeprecatedI18NPipesModule` after the `CommonModule` (the order is important):
```ts
import { NgModule } from '@angular/core';
import { CommonModule, DeprecatedI18NPipesModule } from '@angular/common';
@NgModule({
imports: [
CommonModule,
// import deprecated module after
DeprecatedI18NPipesModule
]
})
export class AppModule { }
```
Dont forget that you will still need to import the intl API polyfill if you want to use those deprecated pipes.
2. Date pipe
* Breaking changes:
- the predefined formats (`short`, `shortTime`, `shortDate`, `medium`, ...) now use the patterns given by CLDR (like it was in AngularJS) instead of the ones from the intl API. You might notice some changes, e.g. `shortDate` will be `8/15/17` instead of `8/15/2017` for `en-US`.
- the narrow version of eras is now `GGGGG` instead of `G`, the format `G` is now similar to `GG` and `GGG`.
- the narrow version of months is now `MMMMM` instead of `L`, the format `L` is now the short standalone version of months.
- the narrow version of the week day is now `EEEEE` instead of `E`, the format `E` is now similar to `EE` and `EEE`.
- the timezone `z` will now fallback to `O` and output `GMT+1` instead of the complete zone name (e.g. `Pacific Standard Time`), this is because the quantity of data required to have all the zone names in all of the existing locales is too big.
- the timezone `Z` will now output the ISO8601 basic format, e.g. `+0100`, you should now use `ZZZZ` to get `GMT+01:00`.
| Field type | Format | Example value | v4 | v5 |
|------------|---------------|-----------------------|----|---------------|
| Eras | Narrow | A for AD | G | GGGGG |
| Months | Narrow | S for September | L | MMMMM |
| Week day | Narrow | M for Monday | E | EEEEE |
| Timezone | Long location | Pacific Standard Time | z | Not available |
| Timezone | Long GMT | GMT+01:00 | Z | ZZZZ |
* Features
- new predefined formats `long`, `full`, `longTime`, `fullTime`.
- the format `yyy` is now supported, e.g. the year `52` will be `052` and the year `2017` will be `2017`.
- standalone months are now supported with the formats `L` to `LLLLL`.
- week of the year is now supported with the formats `w` and `ww`, e.g. weeks `5` and `05`.
- week of the month is now supported with the format `W`, e.g. week `3`.
- fractional seconds are now supported with the format `S` to `SSS`.
- day periods for AM/PM now supports additional formats `aa`, `aaa`, `aaaa` and `aaaaa`. The formats `a` to `aaa` are similar, while `aaaa` is the wide version if available (e.g. `ante meridiem` for `am`), or equivalent to `a` otherwise, and `aaaaa` is the narrow version (e.g. `a` for `am`).
- extra day periods are now supported with the formats `b` to `bbbbb` (and `B` to `BBBBB` for the standalone equivalents), e.g. `morning`, `noon`, `afternoon`, ....
- the short non-localized timezones are now available with the format `O` to `OOOO`. The formats `O` to `OOO` will output `GMT+1` while the format `OOOO` will be `GMT+01:00`.
- the ISO8601 basic time zones are now available with the formats `Z` to `ZZZZZ`. The formats `Z` to `ZZZ` will output `+0100`, while the format `ZZZZ` will be `GMT+01:00` and `ZZZZZ` will be `+01:00`.
* Bug fixes
- the date pipe will now work exactly the same across all browsers, which will fix a lot of bugs for safari and IE.
- eras can now be used on their own without the date, e.g. the format `GG` will be `AD` instead of `8 15, 2017 AD`.
3. Currency pipe
* Breaking change:
- the default value for `symbolDisplay` is now `symbol` instead of `code`. This means that by default you will see `$4.99` for `en-US` instead of `USD4.99` previously.
* Deprecation:
- the second parameter of the currency pipe (`symbolDisplay`) is no longer a boolean, it now takes the values `code`, `symbol` or `symbol-narrow`. A boolean value is still valid for now, but it is deprecated and it will print a warning message in the console.
* Features:
- you can now choose between `code`, `symbol` or `symbol-narrow` which gives you access to more options for some currencies (e.g. the canadian dollar with the code `CAD` has the symbol `CA$` and the symbol-narrow `$`).
4. Percent pipe
* Breaking change
- if you don't specify the number of digits to round to, the local format will be used (and it usually rounds numbers to 0 digits, instead of not rounding previously), e.g. `{{ 3.141592 | percent }}` will output `314%` for the locale `en-US` instead of `314.1592%` previously.
Fixes #10809, #9524, #7008, #9324, #7590, #6724, #3429, #17576, #17478, #17319, #17200, #16838, #16624, #16625, #16591, #14131, #12632, #11376, #11187
PR Close #18284
2017-08-22 14:30:59 -04:00
2018-03-21 04:21:28 -04:00
如果还要为其它语言导入本地化数据,你可以手工完成它:
2017-11-02 17:22:09 -04:00
< code-example path = "i18n/doc-files/app.locale_data.ts" region = "import-locale" title = "src/app/app.module.ts" linenums = "false" >
2018-03-03 08:06:01 -05:00
feat(common): drop use of the Intl API to improve browser support (#18284)
BREAKING CHANGE: Because of multiple bugs and browser inconsistencies, we have dropped the intl api in favor of data exported from the Unicode Common Locale Data Repository (CLDR).
Unfortunately we had to change the i18n pipes (date, number, currency, percent) and there are some breaking changes.
1. I18n pipes
* Breaking change:
- By default Angular now only contains locale data for the language `en-US`, if you set the value of `LOCALE_ID` to another locale, you will have to import new locale data for this language because we don't use the intl API anymore.
* Features:
- you don't need to use the intl polyfill for Angular anymore.
- all i18n pipes now have an additional last parameter `locale` which allows you to use a specific locale instead of the one defined in the token `LOCALE_ID` (whose value is `en-US` by default).
- the new locale data extracted from CLDR are now available to developers as well and can be used through an API (which should be especially useful for library authors).
- you can still use the old pipes for now, but their names have been changed and they are no longer included in the `CommonModule`. To use them, you will have to import the `DeprecatedI18NPipesModule` after the `CommonModule` (the order is important):
```ts
import { NgModule } from '@angular/core';
import { CommonModule, DeprecatedI18NPipesModule } from '@angular/common';
@NgModule({
imports: [
CommonModule,
// import deprecated module after
DeprecatedI18NPipesModule
]
})
export class AppModule { }
```
Dont forget that you will still need to import the intl API polyfill if you want to use those deprecated pipes.
2. Date pipe
* Breaking changes:
- the predefined formats (`short`, `shortTime`, `shortDate`, `medium`, ...) now use the patterns given by CLDR (like it was in AngularJS) instead of the ones from the intl API. You might notice some changes, e.g. `shortDate` will be `8/15/17` instead of `8/15/2017` for `en-US`.
- the narrow version of eras is now `GGGGG` instead of `G`, the format `G` is now similar to `GG` and `GGG`.
- the narrow version of months is now `MMMMM` instead of `L`, the format `L` is now the short standalone version of months.
- the narrow version of the week day is now `EEEEE` instead of `E`, the format `E` is now similar to `EE` and `EEE`.
- the timezone `z` will now fallback to `O` and output `GMT+1` instead of the complete zone name (e.g. `Pacific Standard Time`), this is because the quantity of data required to have all the zone names in all of the existing locales is too big.
- the timezone `Z` will now output the ISO8601 basic format, e.g. `+0100`, you should now use `ZZZZ` to get `GMT+01:00`.
| Field type | Format | Example value | v4 | v5 |
|------------|---------------|-----------------------|----|---------------|
| Eras | Narrow | A for AD | G | GGGGG |
| Months | Narrow | S for September | L | MMMMM |
| Week day | Narrow | M for Monday | E | EEEEE |
| Timezone | Long location | Pacific Standard Time | z | Not available |
| Timezone | Long GMT | GMT+01:00 | Z | ZZZZ |
* Features
- new predefined formats `long`, `full`, `longTime`, `fullTime`.
- the format `yyy` is now supported, e.g. the year `52` will be `052` and the year `2017` will be `2017`.
- standalone months are now supported with the formats `L` to `LLLLL`.
- week of the year is now supported with the formats `w` and `ww`, e.g. weeks `5` and `05`.
- week of the month is now supported with the format `W`, e.g. week `3`.
- fractional seconds are now supported with the format `S` to `SSS`.
- day periods for AM/PM now supports additional formats `aa`, `aaa`, `aaaa` and `aaaaa`. The formats `a` to `aaa` are similar, while `aaaa` is the wide version if available (e.g. `ante meridiem` for `am`), or equivalent to `a` otherwise, and `aaaaa` is the narrow version (e.g. `a` for `am`).
- extra day periods are now supported with the formats `b` to `bbbbb` (and `B` to `BBBBB` for the standalone equivalents), e.g. `morning`, `noon`, `afternoon`, ....
- the short non-localized timezones are now available with the format `O` to `OOOO`. The formats `O` to `OOO` will output `GMT+1` while the format `OOOO` will be `GMT+01:00`.
- the ISO8601 basic time zones are now available with the formats `Z` to `ZZZZZ`. The formats `Z` to `ZZZ` will output `+0100`, while the format `ZZZZ` will be `GMT+01:00` and `ZZZZZ` will be `+01:00`.
* Bug fixes
- the date pipe will now work exactly the same across all browsers, which will fix a lot of bugs for safari and IE.
- eras can now be used on their own without the date, e.g. the format `GG` will be `AD` instead of `8 15, 2017 AD`.
3. Currency pipe
* Breaking change:
- the default value for `symbolDisplay` is now `symbol` instead of `code`. This means that by default you will see `$4.99` for `en-US` instead of `USD4.99` previously.
* Deprecation:
- the second parameter of the currency pipe (`symbolDisplay`) is no longer a boolean, it now takes the values `code`, `symbol` or `symbol-narrow`. A boolean value is still valid for now, but it is deprecated and it will print a warning message in the console.
* Features:
- you can now choose between `code`, `symbol` or `symbol-narrow` which gives you access to more options for some currencies (e.g. the canadian dollar with the code `CAD` has the symbol `CA$` and the symbol-narrow `$`).
4. Percent pipe
* Breaking change
- if you don't specify the number of digits to round to, the local format will be used (and it usually rounds numbers to 0 digits, instead of not rounding previously), e.g. `{{ 3.141592 | percent }}` will output `314%` for the locale `en-US` instead of `314.1592%` previously.
Fixes #10809, #9524, #7008, #9324, #7590, #6724, #3429, #17576, #17478, #17319, #17200, #16838, #16624, #16625, #16591, #14131, #12632, #11376, #11187
PR Close #18284
2017-08-22 14:30:59 -04:00
< / code-example >
2017-07-04 11:07:11 -04:00
The first parameter is an object containing the locale data imported from `@angular/common/locales` .
By default, the imported locale data is registered with the locale id that is defined in the Angular
locale data itself.
If you want to register the imported locale data with another locale id, use the second parameter to
specify a custom locale id. For example, Angular's locale data defines the locale id for French as
"fr". You can use the second parameter to associate the imported French locale data with the custom
2017-12-08 04:04:25 -05:00
locale id "fr-FR" instead of "fr".
feat(common): drop use of the Intl API to improve browser support (#18284)
BREAKING CHANGE: Because of multiple bugs and browser inconsistencies, we have dropped the intl api in favor of data exported from the Unicode Common Locale Data Repository (CLDR).
Unfortunately we had to change the i18n pipes (date, number, currency, percent) and there are some breaking changes.
1. I18n pipes
* Breaking change:
- By default Angular now only contains locale data for the language `en-US`, if you set the value of `LOCALE_ID` to another locale, you will have to import new locale data for this language because we don't use the intl API anymore.
* Features:
- you don't need to use the intl polyfill for Angular anymore.
- all i18n pipes now have an additional last parameter `locale` which allows you to use a specific locale instead of the one defined in the token `LOCALE_ID` (whose value is `en-US` by default).
- the new locale data extracted from CLDR are now available to developers as well and can be used through an API (which should be especially useful for library authors).
- you can still use the old pipes for now, but their names have been changed and they are no longer included in the `CommonModule`. To use them, you will have to import the `DeprecatedI18NPipesModule` after the `CommonModule` (the order is important):
```ts
import { NgModule } from '@angular/core';
import { CommonModule, DeprecatedI18NPipesModule } from '@angular/common';
@NgModule({
imports: [
CommonModule,
// import deprecated module after
DeprecatedI18NPipesModule
]
})
export class AppModule { }
```
Dont forget that you will still need to import the intl API polyfill if you want to use those deprecated pipes.
2. Date pipe
* Breaking changes:
- the predefined formats (`short`, `shortTime`, `shortDate`, `medium`, ...) now use the patterns given by CLDR (like it was in AngularJS) instead of the ones from the intl API. You might notice some changes, e.g. `shortDate` will be `8/15/17` instead of `8/15/2017` for `en-US`.
- the narrow version of eras is now `GGGGG` instead of `G`, the format `G` is now similar to `GG` and `GGG`.
- the narrow version of months is now `MMMMM` instead of `L`, the format `L` is now the short standalone version of months.
- the narrow version of the week day is now `EEEEE` instead of `E`, the format `E` is now similar to `EE` and `EEE`.
- the timezone `z` will now fallback to `O` and output `GMT+1` instead of the complete zone name (e.g. `Pacific Standard Time`), this is because the quantity of data required to have all the zone names in all of the existing locales is too big.
- the timezone `Z` will now output the ISO8601 basic format, e.g. `+0100`, you should now use `ZZZZ` to get `GMT+01:00`.
| Field type | Format | Example value | v4 | v5 |
|------------|---------------|-----------------------|----|---------------|
| Eras | Narrow | A for AD | G | GGGGG |
| Months | Narrow | S for September | L | MMMMM |
| Week day | Narrow | M for Monday | E | EEEEE |
| Timezone | Long location | Pacific Standard Time | z | Not available |
| Timezone | Long GMT | GMT+01:00 | Z | ZZZZ |
* Features
- new predefined formats `long`, `full`, `longTime`, `fullTime`.
- the format `yyy` is now supported, e.g. the year `52` will be `052` and the year `2017` will be `2017`.
- standalone months are now supported with the formats `L` to `LLLLL`.
- week of the year is now supported with the formats `w` and `ww`, e.g. weeks `5` and `05`.
- week of the month is now supported with the format `W`, e.g. week `3`.
- fractional seconds are now supported with the format `S` to `SSS`.
- day periods for AM/PM now supports additional formats `aa`, `aaa`, `aaaa` and `aaaaa`. The formats `a` to `aaa` are similar, while `aaaa` is the wide version if available (e.g. `ante meridiem` for `am`), or equivalent to `a` otherwise, and `aaaaa` is the narrow version (e.g. `a` for `am`).
- extra day periods are now supported with the formats `b` to `bbbbb` (and `B` to `BBBBB` for the standalone equivalents), e.g. `morning`, `noon`, `afternoon`, ....
- the short non-localized timezones are now available with the format `O` to `OOOO`. The formats `O` to `OOO` will output `GMT+1` while the format `OOOO` will be `GMT+01:00`.
- the ISO8601 basic time zones are now available with the formats `Z` to `ZZZZZ`. The formats `Z` to `ZZZ` will output `+0100`, while the format `ZZZZ` will be `GMT+01:00` and `ZZZZZ` will be `+01:00`.
* Bug fixes
- the date pipe will now work exactly the same across all browsers, which will fix a lot of bugs for safari and IE.
- eras can now be used on their own without the date, e.g. the format `GG` will be `AD` instead of `8 15, 2017 AD`.
3. Currency pipe
* Breaking change:
- the default value for `symbolDisplay` is now `symbol` instead of `code`. This means that by default you will see `$4.99` for `en-US` instead of `USD4.99` previously.
* Deprecation:
- the second parameter of the currency pipe (`symbolDisplay`) is no longer a boolean, it now takes the values `code`, `symbol` or `symbol-narrow`. A boolean value is still valid for now, but it is deprecated and it will print a warning message in the console.
* Features:
- you can now choose between `code`, `symbol` or `symbol-narrow` which gives you access to more options for some currencies (e.g. the canadian dollar with the code `CAD` has the symbol `CA$` and the symbol-narrow `$`).
4. Percent pipe
* Breaking change
- if you don't specify the number of digits to round to, the local format will be used (and it usually rounds numbers to 0 digits, instead of not rounding previously), e.g. `{{ 3.141592 | percent }}` will output `314%` for the locale `en-US` instead of `314.1592%` previously.
Fixes #10809, #9524, #7008, #9324, #7590, #6724, #3429, #17576, #17478, #17319, #17200, #16838, #16624, #16625, #16591, #14131, #12632, #11376, #11187
PR Close #18284
2017-08-22 14:30:59 -04:00
2018-03-21 04:21:28 -04:00
第一个参数是一个包含从 `@angular/common/locales` 中导入的本地化数据的对象。
默认情况下,导入 Angular 自带的本地化数据时会使用数据中自带的一个地区标识进行注册。
如果你要使用其它地区标识来注册这个导入的本地化数据,可以在第二个参数中指定一个自定义的地区标识。
比如, Angular 为法语定义的地区标识是 “fr”, 你可以通过第二个参数为这些导入的法语本地化数据指定一个自定义的地区标识 “fr-FR”。
2017-11-02 17:22:09 -04:00
The files in `@angular/common/locales` contain most of the locale data that you
feat(common): drop use of the Intl API to improve browser support (#18284)
BREAKING CHANGE: Because of multiple bugs and browser inconsistencies, we have dropped the intl api in favor of data exported from the Unicode Common Locale Data Repository (CLDR).
Unfortunately we had to change the i18n pipes (date, number, currency, percent) and there are some breaking changes.
1. I18n pipes
* Breaking change:
- By default Angular now only contains locale data for the language `en-US`, if you set the value of `LOCALE_ID` to another locale, you will have to import new locale data for this language because we don't use the intl API anymore.
* Features:
- you don't need to use the intl polyfill for Angular anymore.
- all i18n pipes now have an additional last parameter `locale` which allows you to use a specific locale instead of the one defined in the token `LOCALE_ID` (whose value is `en-US` by default).
- the new locale data extracted from CLDR are now available to developers as well and can be used through an API (which should be especially useful for library authors).
- you can still use the old pipes for now, but their names have been changed and they are no longer included in the `CommonModule`. To use them, you will have to import the `DeprecatedI18NPipesModule` after the `CommonModule` (the order is important):
```ts
import { NgModule } from '@angular/core';
import { CommonModule, DeprecatedI18NPipesModule } from '@angular/common';
@NgModule({
imports: [
CommonModule,
// import deprecated module after
DeprecatedI18NPipesModule
]
})
export class AppModule { }
```
Dont forget that you will still need to import the intl API polyfill if you want to use those deprecated pipes.
2. Date pipe
* Breaking changes:
- the predefined formats (`short`, `shortTime`, `shortDate`, `medium`, ...) now use the patterns given by CLDR (like it was in AngularJS) instead of the ones from the intl API. You might notice some changes, e.g. `shortDate` will be `8/15/17` instead of `8/15/2017` for `en-US`.
- the narrow version of eras is now `GGGGG` instead of `G`, the format `G` is now similar to `GG` and `GGG`.
- the narrow version of months is now `MMMMM` instead of `L`, the format `L` is now the short standalone version of months.
- the narrow version of the week day is now `EEEEE` instead of `E`, the format `E` is now similar to `EE` and `EEE`.
- the timezone `z` will now fallback to `O` and output `GMT+1` instead of the complete zone name (e.g. `Pacific Standard Time`), this is because the quantity of data required to have all the zone names in all of the existing locales is too big.
- the timezone `Z` will now output the ISO8601 basic format, e.g. `+0100`, you should now use `ZZZZ` to get `GMT+01:00`.
| Field type | Format | Example value | v4 | v5 |
|------------|---------------|-----------------------|----|---------------|
| Eras | Narrow | A for AD | G | GGGGG |
| Months | Narrow | S for September | L | MMMMM |
| Week day | Narrow | M for Monday | E | EEEEE |
| Timezone | Long location | Pacific Standard Time | z | Not available |
| Timezone | Long GMT | GMT+01:00 | Z | ZZZZ |
* Features
- new predefined formats `long`, `full`, `longTime`, `fullTime`.
- the format `yyy` is now supported, e.g. the year `52` will be `052` and the year `2017` will be `2017`.
- standalone months are now supported with the formats `L` to `LLLLL`.
- week of the year is now supported with the formats `w` and `ww`, e.g. weeks `5` and `05`.
- week of the month is now supported with the format `W`, e.g. week `3`.
- fractional seconds are now supported with the format `S` to `SSS`.
- day periods for AM/PM now supports additional formats `aa`, `aaa`, `aaaa` and `aaaaa`. The formats `a` to `aaa` are similar, while `aaaa` is the wide version if available (e.g. `ante meridiem` for `am`), or equivalent to `a` otherwise, and `aaaaa` is the narrow version (e.g. `a` for `am`).
- extra day periods are now supported with the formats `b` to `bbbbb` (and `B` to `BBBBB` for the standalone equivalents), e.g. `morning`, `noon`, `afternoon`, ....
- the short non-localized timezones are now available with the format `O` to `OOOO`. The formats `O` to `OOO` will output `GMT+1` while the format `OOOO` will be `GMT+01:00`.
- the ISO8601 basic time zones are now available with the formats `Z` to `ZZZZZ`. The formats `Z` to `ZZZ` will output `+0100`, while the format `ZZZZ` will be `GMT+01:00` and `ZZZZZ` will be `+01:00`.
* Bug fixes
- the date pipe will now work exactly the same across all browsers, which will fix a lot of bugs for safari and IE.
- eras can now be used on their own without the date, e.g. the format `GG` will be `AD` instead of `8 15, 2017 AD`.
3. Currency pipe
* Breaking change:
- the default value for `symbolDisplay` is now `symbol` instead of `code`. This means that by default you will see `$4.99` for `en-US` instead of `USD4.99` previously.
* Deprecation:
- the second parameter of the currency pipe (`symbolDisplay`) is no longer a boolean, it now takes the values `code`, `symbol` or `symbol-narrow`. A boolean value is still valid for now, but it is deprecated and it will print a warning message in the console.
* Features:
- you can now choose between `code`, `symbol` or `symbol-narrow` which gives you access to more options for some currencies (e.g. the canadian dollar with the code `CAD` has the symbol `CA$` and the symbol-narrow `$`).
4. Percent pipe
* Breaking change
- if you don't specify the number of digits to round to, the local format will be used (and it usually rounds numbers to 0 digits, instead of not rounding previously), e.g. `{{ 3.141592 | percent }}` will output `314%` for the locale `en-US` instead of `314.1592%` previously.
Fixes #10809, #9524, #7008, #9324, #7590, #6724, #3429, #17576, #17478, #17319, #17200, #16838, #16624, #16625, #16591, #14131, #12632, #11376, #11187
PR Close #18284
2017-08-22 14:30:59 -04:00
need, but some advanced formatting options might only be available in the extra dataset that you can
2017-11-02 17:22:09 -04:00
import from `@angular/common/locales/extra` . An error message informs you when this is the case.
2018-03-03 08:06:01 -05:00
2018-03-21 04:21:28 -04:00
`@angular/common/locales` 中的文件包含你需要的大多数本地化数据,
不过有些高级的格式选项只存在于从 `@angular/common/locales/extra` 中导入的扩展数据集中。遇到这种情况,你就会收到一条错误信息。
2017-11-02 17:22:09 -04:00
< code-example path = "i18n/doc-files/app.locale_data_extra.ts" region = "import-locale-extra" title = "src/app/app.module.ts" linenums = "false" >
2018-03-03 08:06:01 -05:00
2017-11-02 17:22:09 -04:00
< / code-example >
feat(common): drop use of the Intl API to improve browser support (#18284)
BREAKING CHANGE: Because of multiple bugs and browser inconsistencies, we have dropped the intl api in favor of data exported from the Unicode Common Locale Data Repository (CLDR).
Unfortunately we had to change the i18n pipes (date, number, currency, percent) and there are some breaking changes.
1. I18n pipes
* Breaking change:
- By default Angular now only contains locale data for the language `en-US`, if you set the value of `LOCALE_ID` to another locale, you will have to import new locale data for this language because we don't use the intl API anymore.
* Features:
- you don't need to use the intl polyfill for Angular anymore.
- all i18n pipes now have an additional last parameter `locale` which allows you to use a specific locale instead of the one defined in the token `LOCALE_ID` (whose value is `en-US` by default).
- the new locale data extracted from CLDR are now available to developers as well and can be used through an API (which should be especially useful for library authors).
- you can still use the old pipes for now, but their names have been changed and they are no longer included in the `CommonModule`. To use them, you will have to import the `DeprecatedI18NPipesModule` after the `CommonModule` (the order is important):
```ts
import { NgModule } from '@angular/core';
import { CommonModule, DeprecatedI18NPipesModule } from '@angular/common';
@NgModule({
imports: [
CommonModule,
// import deprecated module after
DeprecatedI18NPipesModule
]
})
export class AppModule { }
```
Dont forget that you will still need to import the intl API polyfill if you want to use those deprecated pipes.
2. Date pipe
* Breaking changes:
- the predefined formats (`short`, `shortTime`, `shortDate`, `medium`, ...) now use the patterns given by CLDR (like it was in AngularJS) instead of the ones from the intl API. You might notice some changes, e.g. `shortDate` will be `8/15/17` instead of `8/15/2017` for `en-US`.
- the narrow version of eras is now `GGGGG` instead of `G`, the format `G` is now similar to `GG` and `GGG`.
- the narrow version of months is now `MMMMM` instead of `L`, the format `L` is now the short standalone version of months.
- the narrow version of the week day is now `EEEEE` instead of `E`, the format `E` is now similar to `EE` and `EEE`.
- the timezone `z` will now fallback to `O` and output `GMT+1` instead of the complete zone name (e.g. `Pacific Standard Time`), this is because the quantity of data required to have all the zone names in all of the existing locales is too big.
- the timezone `Z` will now output the ISO8601 basic format, e.g. `+0100`, you should now use `ZZZZ` to get `GMT+01:00`.
| Field type | Format | Example value | v4 | v5 |
|------------|---------------|-----------------------|----|---------------|
| Eras | Narrow | A for AD | G | GGGGG |
| Months | Narrow | S for September | L | MMMMM |
| Week day | Narrow | M for Monday | E | EEEEE |
| Timezone | Long location | Pacific Standard Time | z | Not available |
| Timezone | Long GMT | GMT+01:00 | Z | ZZZZ |
* Features
- new predefined formats `long`, `full`, `longTime`, `fullTime`.
- the format `yyy` is now supported, e.g. the year `52` will be `052` and the year `2017` will be `2017`.
- standalone months are now supported with the formats `L` to `LLLLL`.
- week of the year is now supported with the formats `w` and `ww`, e.g. weeks `5` and `05`.
- week of the month is now supported with the format `W`, e.g. week `3`.
- fractional seconds are now supported with the format `S` to `SSS`.
- day periods for AM/PM now supports additional formats `aa`, `aaa`, `aaaa` and `aaaaa`. The formats `a` to `aaa` are similar, while `aaaa` is the wide version if available (e.g. `ante meridiem` for `am`), or equivalent to `a` otherwise, and `aaaaa` is the narrow version (e.g. `a` for `am`).
- extra day periods are now supported with the formats `b` to `bbbbb` (and `B` to `BBBBB` for the standalone equivalents), e.g. `morning`, `noon`, `afternoon`, ....
- the short non-localized timezones are now available with the format `O` to `OOOO`. The formats `O` to `OOO` will output `GMT+1` while the format `OOOO` will be `GMT+01:00`.
- the ISO8601 basic time zones are now available with the formats `Z` to `ZZZZZ`. The formats `Z` to `ZZZ` will output `+0100`, while the format `ZZZZ` will be `GMT+01:00` and `ZZZZZ` will be `+01:00`.
* Bug fixes
- the date pipe will now work exactly the same across all browsers, which will fix a lot of bugs for safari and IE.
- eras can now be used on their own without the date, e.g. the format `GG` will be `AD` instead of `8 15, 2017 AD`.
3. Currency pipe
* Breaking change:
- the default value for `symbolDisplay` is now `symbol` instead of `code`. This means that by default you will see `$4.99` for `en-US` instead of `USD4.99` previously.
* Deprecation:
- the second parameter of the currency pipe (`symbolDisplay`) is no longer a boolean, it now takes the values `code`, `symbol` or `symbol-narrow`. A boolean value is still valid for now, but it is deprecated and it will print a warning message in the console.
* Features:
- you can now choose between `code`, `symbol` or `symbol-narrow` which gives you access to more options for some currencies (e.g. the canadian dollar with the code `CAD` has the symbol `CA$` and the symbol-narrow `$`).
4. Percent pipe
* Breaking change
- if you don't specify the number of digits to round to, the local format will be used (and it usually rounds numbers to 0 digits, instead of not rounding previously), e.g. `{{ 3.141592 | percent }}` will output `314%` for the locale `en-US` instead of `314.1592%` previously.
Fixes #10809, #9524, #7008, #9324, #7590, #6724, #3429, #17576, #17478, #17319, #17200, #16838, #16624, #16625, #16591, #14131, #12632, #11376, #11187
PR Close #18284
2017-08-22 14:30:59 -04:00
< div class = "l-sub-section" >
2017-11-02 17:22:09 -04:00
All locale data used by Angular are extracted from the Unicode Consortium's
< a href = "http://cldr.unicode.org/" title = "CLDR" > Common Locale Data Repository (CLDR)< / a > .
feat(common): drop use of the Intl API to improve browser support (#18284)
BREAKING CHANGE: Because of multiple bugs and browser inconsistencies, we have dropped the intl api in favor of data exported from the Unicode Common Locale Data Repository (CLDR).
Unfortunately we had to change the i18n pipes (date, number, currency, percent) and there are some breaking changes.
1. I18n pipes
* Breaking change:
- By default Angular now only contains locale data for the language `en-US`, if you set the value of `LOCALE_ID` to another locale, you will have to import new locale data for this language because we don't use the intl API anymore.
* Features:
- you don't need to use the intl polyfill for Angular anymore.
- all i18n pipes now have an additional last parameter `locale` which allows you to use a specific locale instead of the one defined in the token `LOCALE_ID` (whose value is `en-US` by default).
- the new locale data extracted from CLDR are now available to developers as well and can be used through an API (which should be especially useful for library authors).
- you can still use the old pipes for now, but their names have been changed and they are no longer included in the `CommonModule`. To use them, you will have to import the `DeprecatedI18NPipesModule` after the `CommonModule` (the order is important):
```ts
import { NgModule } from '@angular/core';
import { CommonModule, DeprecatedI18NPipesModule } from '@angular/common';
@NgModule({
imports: [
CommonModule,
// import deprecated module after
DeprecatedI18NPipesModule
]
})
export class AppModule { }
```
Dont forget that you will still need to import the intl API polyfill if you want to use those deprecated pipes.
2. Date pipe
* Breaking changes:
- the predefined formats (`short`, `shortTime`, `shortDate`, `medium`, ...) now use the patterns given by CLDR (like it was in AngularJS) instead of the ones from the intl API. You might notice some changes, e.g. `shortDate` will be `8/15/17` instead of `8/15/2017` for `en-US`.
- the narrow version of eras is now `GGGGG` instead of `G`, the format `G` is now similar to `GG` and `GGG`.
- the narrow version of months is now `MMMMM` instead of `L`, the format `L` is now the short standalone version of months.
- the narrow version of the week day is now `EEEEE` instead of `E`, the format `E` is now similar to `EE` and `EEE`.
- the timezone `z` will now fallback to `O` and output `GMT+1` instead of the complete zone name (e.g. `Pacific Standard Time`), this is because the quantity of data required to have all the zone names in all of the existing locales is too big.
- the timezone `Z` will now output the ISO8601 basic format, e.g. `+0100`, you should now use `ZZZZ` to get `GMT+01:00`.
| Field type | Format | Example value | v4 | v5 |
|------------|---------------|-----------------------|----|---------------|
| Eras | Narrow | A for AD | G | GGGGG |
| Months | Narrow | S for September | L | MMMMM |
| Week day | Narrow | M for Monday | E | EEEEE |
| Timezone | Long location | Pacific Standard Time | z | Not available |
| Timezone | Long GMT | GMT+01:00 | Z | ZZZZ |
* Features
- new predefined formats `long`, `full`, `longTime`, `fullTime`.
- the format `yyy` is now supported, e.g. the year `52` will be `052` and the year `2017` will be `2017`.
- standalone months are now supported with the formats `L` to `LLLLL`.
- week of the year is now supported with the formats `w` and `ww`, e.g. weeks `5` and `05`.
- week of the month is now supported with the format `W`, e.g. week `3`.
- fractional seconds are now supported with the format `S` to `SSS`.
- day periods for AM/PM now supports additional formats `aa`, `aaa`, `aaaa` and `aaaaa`. The formats `a` to `aaa` are similar, while `aaaa` is the wide version if available (e.g. `ante meridiem` for `am`), or equivalent to `a` otherwise, and `aaaaa` is the narrow version (e.g. `a` for `am`).
- extra day periods are now supported with the formats `b` to `bbbbb` (and `B` to `BBBBB` for the standalone equivalents), e.g. `morning`, `noon`, `afternoon`, ....
- the short non-localized timezones are now available with the format `O` to `OOOO`. The formats `O` to `OOO` will output `GMT+1` while the format `OOOO` will be `GMT+01:00`.
- the ISO8601 basic time zones are now available with the formats `Z` to `ZZZZZ`. The formats `Z` to `ZZZ` will output `+0100`, while the format `ZZZZ` will be `GMT+01:00` and `ZZZZZ` will be `+01:00`.
* Bug fixes
- the date pipe will now work exactly the same across all browsers, which will fix a lot of bugs for safari and IE.
- eras can now be used on their own without the date, e.g. the format `GG` will be `AD` instead of `8 15, 2017 AD`.
3. Currency pipe
* Breaking change:
- the default value for `symbolDisplay` is now `symbol` instead of `code`. This means that by default you will see `$4.99` for `en-US` instead of `USD4.99` previously.
* Deprecation:
- the second parameter of the currency pipe (`symbolDisplay`) is no longer a boolean, it now takes the values `code`, `symbol` or `symbol-narrow`. A boolean value is still valid for now, but it is deprecated and it will print a warning message in the console.
* Features:
- you can now choose between `code`, `symbol` or `symbol-narrow` which gives you access to more options for some currencies (e.g. the canadian dollar with the code `CAD` has the symbol `CA$` and the symbol-narrow `$`).
4. Percent pipe
* Breaking change
- if you don't specify the number of digits to round to, the local format will be used (and it usually rounds numbers to 0 digits, instead of not rounding previously), e.g. `{{ 3.141592 | percent }}` will output `314%` for the locale `en-US` instead of `314.1592%` previously.
Fixes #10809, #9524, #7008, #9324, #7590, #6724, #3429, #17576, #17478, #17319, #17200, #16838, #16624, #16625, #16591, #14131, #12632, #11376, #11187
PR Close #18284
2017-08-22 14:30:59 -04:00
2018-03-21 04:21:28 -04:00
Angular 中使用的所有本地化数据都是从 Unicode 联盟的< a href = "http://cldr.unicode.org/" title = "CLDR" > 常用本地化数据仓库 (CLDR)< / a > 中提取的。
2017-11-02 17:22:09 -04:00
< / div >
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
## Template translations
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
## 模板翻译
2017-11-02 17:22:09 -04:00
< div class = "l-sub-section" >
2017-07-22 23:51:25 -04:00
2017-11-02 17:22:09 -04:00
This document refers to a unit of translatable text as "text," a "message", or a
2018-03-06 22:25:56 -05:00
"text message."
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
本文档中会把翻译文本的最小单元称为“文本”、“消息”或“文本消息”。
feat(common): drop use of the Intl API to improve browser support (#18284)
BREAKING CHANGE: Because of multiple bugs and browser inconsistencies, we have dropped the intl api in favor of data exported from the Unicode Common Locale Data Repository (CLDR).
Unfortunately we had to change the i18n pipes (date, number, currency, percent) and there are some breaking changes.
1. I18n pipes
* Breaking change:
- By default Angular now only contains locale data for the language `en-US`, if you set the value of `LOCALE_ID` to another locale, you will have to import new locale data for this language because we don't use the intl API anymore.
* Features:
- you don't need to use the intl polyfill for Angular anymore.
- all i18n pipes now have an additional last parameter `locale` which allows you to use a specific locale instead of the one defined in the token `LOCALE_ID` (whose value is `en-US` by default).
- the new locale data extracted from CLDR are now available to developers as well and can be used through an API (which should be especially useful for library authors).
- you can still use the old pipes for now, but their names have been changed and they are no longer included in the `CommonModule`. To use them, you will have to import the `DeprecatedI18NPipesModule` after the `CommonModule` (the order is important):
```ts
import { NgModule } from '@angular/core';
import { CommonModule, DeprecatedI18NPipesModule } from '@angular/common';
@NgModule({
imports: [
CommonModule,
// import deprecated module after
DeprecatedI18NPipesModule
]
})
export class AppModule { }
```
Dont forget that you will still need to import the intl API polyfill if you want to use those deprecated pipes.
2. Date pipe
* Breaking changes:
- the predefined formats (`short`, `shortTime`, `shortDate`, `medium`, ...) now use the patterns given by CLDR (like it was in AngularJS) instead of the ones from the intl API. You might notice some changes, e.g. `shortDate` will be `8/15/17` instead of `8/15/2017` for `en-US`.
- the narrow version of eras is now `GGGGG` instead of `G`, the format `G` is now similar to `GG` and `GGG`.
- the narrow version of months is now `MMMMM` instead of `L`, the format `L` is now the short standalone version of months.
- the narrow version of the week day is now `EEEEE` instead of `E`, the format `E` is now similar to `EE` and `EEE`.
- the timezone `z` will now fallback to `O` and output `GMT+1` instead of the complete zone name (e.g. `Pacific Standard Time`), this is because the quantity of data required to have all the zone names in all of the existing locales is too big.
- the timezone `Z` will now output the ISO8601 basic format, e.g. `+0100`, you should now use `ZZZZ` to get `GMT+01:00`.
| Field type | Format | Example value | v4 | v5 |
|------------|---------------|-----------------------|----|---------------|
| Eras | Narrow | A for AD | G | GGGGG |
| Months | Narrow | S for September | L | MMMMM |
| Week day | Narrow | M for Monday | E | EEEEE |
| Timezone | Long location | Pacific Standard Time | z | Not available |
| Timezone | Long GMT | GMT+01:00 | Z | ZZZZ |
* Features
- new predefined formats `long`, `full`, `longTime`, `fullTime`.
- the format `yyy` is now supported, e.g. the year `52` will be `052` and the year `2017` will be `2017`.
- standalone months are now supported with the formats `L` to `LLLLL`.
- week of the year is now supported with the formats `w` and `ww`, e.g. weeks `5` and `05`.
- week of the month is now supported with the format `W`, e.g. week `3`.
- fractional seconds are now supported with the format `S` to `SSS`.
- day periods for AM/PM now supports additional formats `aa`, `aaa`, `aaaa` and `aaaaa`. The formats `a` to `aaa` are similar, while `aaaa` is the wide version if available (e.g. `ante meridiem` for `am`), or equivalent to `a` otherwise, and `aaaaa` is the narrow version (e.g. `a` for `am`).
- extra day periods are now supported with the formats `b` to `bbbbb` (and `B` to `BBBBB` for the standalone equivalents), e.g. `morning`, `noon`, `afternoon`, ....
- the short non-localized timezones are now available with the format `O` to `OOOO`. The formats `O` to `OOO` will output `GMT+1` while the format `OOOO` will be `GMT+01:00`.
- the ISO8601 basic time zones are now available with the formats `Z` to `ZZZZZ`. The formats `Z` to `ZZZ` will output `+0100`, while the format `ZZZZ` will be `GMT+01:00` and `ZZZZZ` will be `+01:00`.
* Bug fixes
- the date pipe will now work exactly the same across all browsers, which will fix a lot of bugs for safari and IE.
- eras can now be used on their own without the date, e.g. the format `GG` will be `AD` instead of `8 15, 2017 AD`.
3. Currency pipe
* Breaking change:
- the default value for `symbolDisplay` is now `symbol` instead of `code`. This means that by default you will see `$4.99` for `en-US` instead of `USD4.99` previously.
* Deprecation:
- the second parameter of the currency pipe (`symbolDisplay`) is no longer a boolean, it now takes the values `code`, `symbol` or `symbol-narrow`. A boolean value is still valid for now, but it is deprecated and it will print a warning message in the console.
* Features:
- you can now choose between `code`, `symbol` or `symbol-narrow` which gives you access to more options for some currencies (e.g. the canadian dollar with the code `CAD` has the symbol `CA$` and the symbol-narrow `$`).
4. Percent pipe
* Breaking change
- if you don't specify the number of digits to round to, the local format will be used (and it usually rounds numbers to 0 digits, instead of not rounding previously), e.g. `{{ 3.141592 | percent }}` will output `314%` for the locale `en-US` instead of `314.1592%` previously.
Fixes #10809, #9524, #7008, #9324, #7590, #6724, #3429, #17576, #17478, #17319, #17200, #16838, #16624, #16625, #16591, #14131, #12632, #11376, #11187
PR Close #18284
2017-08-22 14:30:59 -04:00
< / div >
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
The i18n template translation process has four phases:
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
i18n 模板的翻译过程分为四个阶段
2017-11-02 17:22:09 -04:00
1. Mark static text messages in your component templates for translation.
2017-07-22 23:51:25 -04:00
2018-03-07 02:42:49 -05:00
在组件模板中标记需要翻译的静态文本信息。
2017-11-02 17:22:09 -04:00
2. An Angular i18n tool extracts the marked text into an industry standard translation source file.
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
Angular的 i18n 工具将标记的信息提取到一个行业标准的翻译源文件。
2017-11-02 17:22:09 -04:00
3. A translator edits that file, translating the extracted text into the target language,
and returns the file to you.
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
翻译人员编辑该文件,翻译提取出来的文本信息到目标语言,并将该文件还给你。
2017-11-02 17:22:09 -04:00
4. The Angular compiler imports the completed translation files,
replaces the original messages with translated text, and generates a new version of the app
in the target language.
2017-07-22 23:51:25 -04:00
2018-03-21 04:21:28 -04:00
Angular编译器导入完成翻译的文件, 使用翻译的文本替换原始信息, 并生成新的目标语言版本的应用程序。
2017-11-02 17:22:09 -04:00
You need to build and deploy a separate version of the app for each supported language.
2017-07-22 23:51:25 -04:00
2018-03-21 04:21:28 -04:00
你可以为每种支持的语言构建和部署单独的应用程序版本。
2017-11-02 17:22:09 -04:00
{@a i18n-attribute}
2018-03-03 08:06:01 -05:00
2017-11-02 17:22:09 -04:00
### Mark text with the i18n attribute
2018-03-21 04:21:28 -04:00
### 使用 `i18n` 属性标记文本
2017-11-02 17:22:09 -04:00
The Angular `i18n` attribute marks translatable content. Place it on every element tag whose fixed
text is to be translated.
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
Angular的`i18n`属性是可翻译内容的标记。
将它放到每个固定文本需要翻译的元素标签中。
2017-11-02 17:22:09 -04:00
In the example below, an `<h1>` tag displays a simple English language greeting, "Hello i18n!"
2018-03-21 04:21:28 -04:00
在下面的例子中,`< h1 > `标签显示了一句简单的英文问候语, “Hello i18n!”
2017-11-02 17:22:09 -04:00
< code-example path = "i18n/doc-files/app.component.html" region = "greeting" title = "src/app/app.component.html" linenums = "false" >
2018-03-03 08:06:01 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
To mark the greeting for translation, add the `i18n` attribute to the `<h1>` tag.
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
要想把它标记为需要翻译的文本,就给 `<h1>` 标签添加上 `i18n` 属性。
2017-08-03 19:39:15 -04:00
2017-11-02 17:22:09 -04:00
< code-example path = "i18n/doc-files/app.component.html" region = "i18n-attribute" title = "src/app/app.component.html" linenums = "false" >
2017-02-22 13:09:39 -05:00
2018-03-03 08:06:01 -05:00
< / code-example >
2017-04-12 15:53:18 -04:00
2017-11-02 17:22:09 -04:00
< div class = "alert is-helpful" >
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
`i18n` is a custom attribute, recognized by Angular tools and compilers.
After translation, the compiler removes it. It is not an Angular directive.
2017-07-22 23:51:25 -04:00
2018-03-21 04:21:28 -04:00
`i18n` 是一个自定义属性,会被 Angular 工具和编译器识别。
翻译之后,编译器就会移除它。它不是 Angular 指令。
2017-11-02 17:22:09 -04:00
< / div >
2017-02-22 13:09:39 -05:00
2017-05-02 02:01:20 -04:00
{@a help-translator}
2018-03-03 08:06:01 -05:00
2017-11-02 17:22:09 -04:00
### Help the translator with a description and meaning
2017-08-03 19:39:15 -04:00
2018-03-21 04:21:28 -04:00
### 用描述和意图来帮助翻译人员
2017-11-02 17:22:09 -04:00
To translate a text message accurately, the translator may need additional information or context.
2017-03-31 19:57:13 -04:00
2018-03-21 04:21:28 -04:00
要想翻译的更准确,翻译人员可能需要待翻译文本的额外信息或场景说明。
2017-11-02 17:22:09 -04:00
You can add a description of the text message as the value of the `i18n` attribute, as shown in the
example below:
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
你可以用 `i18n` 属性的值来添加这些文本信息的描述,例子如下:
2017-11-02 17:22:09 -04:00
< code-example path = "i18n/doc-files/app.component.html" region = "i18n-attribute-desc" title = "src/app/app.component.html" linenums = "false" >
2018-03-03 08:06:01 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
The translator may also need to know the meaning or intent of the text message within this particular
app context.
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
为了给出正确的翻译,翻译者需要知道你这段文本在特定情境下的*含义*或*真实意图*。
2017-07-22 23:51:25 -04:00
2017-11-02 17:22:09 -04:00
You add context by beginning the `i18n` attribute value with the _meaning_ and
separating it from the _description_ with the `|` character: `<meaning>|<description>`
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
在描述的前面,你可以用 `i18n` 开头的属性值来为指定的字符串添加一些上下文含义,用 `|` 将其与描述文字隔开(`< 意图 > |< 描述 > `)。
2017-03-27 11:08:53 -04:00
2017-11-02 17:22:09 -04:00
< code-example path = "i18n/doc-files/app.component.html" region = "i18n-attribute-meaning" title = "src/app/app.component.html" linenums = "false" >
2018-03-03 08:06:01 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
All occurrences of a text message that have the same meaning will have the same translation.
A text message that is associated with different meanings can have different translations.
2018-03-21 04:21:28 -04:00
如果所有地方出现的文本具有**相同**含义时,它们应该有**相同**的翻译,
但是如果在某些地方它具有**不同含义**,那么它应该有不同的翻译。
2017-11-02 17:22:09 -04:00
The Angular extraction tool preserves both the meaning and the description in the translation
source file to facilitate contextually-specific translations, but only the combination of meaning
and text message are used to generate the specific id of a translation. If you have two
similar text messages with different meanings, they are extracted separately. If you have two similar
text messages with different descriptions (not different meanings), then they are extracted only once.
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
Angular 的提取工具会在翻译源文件中保留**含义**和**描述**,以支持符合特定上下文的翻译。但它只会使用含义和文本消息的组合来为待翻译文本生成明确的 id。如果你有两个相同的文本消息, 但是含义不同, 它们就会被分别提取。如果你有两个相同的文本消息, 但是描述不同( 但含义相同) , 它们就只会提取一次。
2017-07-22 23:51:25 -04:00
2017-05-02 02:01:20 -04:00
{@a custom-id}
2018-03-03 08:06:01 -05:00
2017-11-02 17:22:09 -04:00
### Set a custom id for persistence and maintenance
2017-04-12 15:53:18 -04:00
2018-03-20 05:09:18 -04:00
### 设置一个自定义的 `id` 来提升可搜索性和可维护性
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
The angular i18n extractor tool generates a file with a translation unit entry for each `i18n`
attribute in a template. By default, it assigns each translation unit a unique id such as this one:
2017-02-22 13:09:39 -05:00
2018-03-20 05:09:18 -04:00
Angular 的 `i18n` 提取工具会为模板中每个带有 `i18n` 属性的元素生成一个*翻译单元( translation unit) *条目,并保存到一个文件中。默认情况下,它为每个翻译单元指定一个唯一的 `id` ,就像这样:
2017-08-03 20:47:51 -04:00
2017-11-02 17:22:09 -04:00
< code-example path = "i18n/doc-files/messages.fr.xlf.html" region = "generated-id" linenums = "false" >
2018-03-03 08:06:01 -05:00
2017-05-02 02:01:20 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
When you change the translatable text, the extractor tool generates a new id for that translation unit.
You must then update the translation file with the new id.
2017-08-03 20:47:51 -04:00
2018-03-21 04:21:28 -04:00
当你修改这段可翻译的文字时,提取工具会为那个翻译单元生成一个新的 `id` 。
你就要使用这个新的 id 来修改这个翻译文件。
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
Alternatively, you can specify a custom id in the `i18n` attribute by using the prefix `@@` .
2018-03-06 22:25:56 -05:00
The example below defines the custom id `introductionHeader` :
2017-08-03 20:47:51 -04:00
2018-03-21 04:21:28 -04:00
另一种方案是,你可以使用 `@@` 前缀在 `i18n` 属性中指定一个自定义的 id。
下面这个例子就定义了一个自定义 id `introductionHeader` :
2017-11-02 17:22:09 -04:00
< code-example path = 'i18n/doc-files/app.component.html' region = 'i18n-attribute-solo-id' title = 'app/app.component.html' linenums = "false" >
2018-03-03 08:06:01 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
When you specify a custom id, the extractor tool and compiler generate a translation unit with that
custom id.
2017-03-31 19:57:13 -04:00
2018-03-21 04:21:28 -04:00
一旦你指定了自定义 id, 提取工具和编译器就会用*你的自定义 id` 生成一个翻译单元,而不会再改变它。
2017-08-03 20:47:51 -04:00
2017-11-02 17:22:09 -04:00
< code-example path = "i18n/doc-files/messages.fr.xlf.html" region = "custom-id" linenums = "false" >
2018-03-03 08:06:01 -05:00
2017-05-02 02:01:20 -04:00
< / code-example >
2017-03-31 19:57:13 -04:00
2017-11-02 17:22:09 -04:00
The custom id is persistent. The extractor tool does not change it when the translatable text changes.
Therefore, you do not need to update the translation. This approach makes maintenance easier.
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
自定义 id 是永久性的,翻译工具待翻译文本发生变化时不会修改它。
因此,你不必修改翻译结果。这种方式可以让维护变得更简单。
2017-11-02 17:22:09 -04:00
#### Use a custom id with a description
2017-08-03 20:47:51 -04:00
2018-03-21 04:21:28 -04:00
#### 在描述中使用自定义 id
2017-11-02 17:22:09 -04:00
You can use a custom id in combination with a description by including both in the value of the
`i18n` attribute. In the example below, the `i18n` attribute value includes a description, followed
by the custom `id` :
2018-03-21 04:21:28 -04:00
你可以在 `i18n` 属性的值中使用自定义 id 与描述信息的组合。
下面的例子中,`i18n` 的属性中中就包含了一条跟在自定义 `id` 后面的描述信息:
2017-11-02 17:22:09 -04:00
< code-example path = 'i18n/doc-files/app.component.html' region = 'i18n-attribute-id' title = 'app/app.component.html' linenums = "false" >
2018-03-03 08:06:01 -05:00
2017-05-02 02:01:20 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
You also can add a meaning, as shown in this example:
2017-03-27 11:08:53 -04:00
2018-03-21 04:21:28 -04:00
你还可以添加含义,例子如下:
2017-08-03 20:47:51 -04:00
2017-11-02 17:22:09 -04:00
< code-example path = 'i18n/doc-files/app.component.html' region = 'i18n-attribute-meaning-and-id' title = 'app/app.component.html' linenums = "false" >
2018-03-03 08:06:01 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
#### Define unique custom ids
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
#### 定义唯一的自定义 ID
2017-11-02 17:22:09 -04:00
Be sure to define custom ids that are unique. If you use the same id for two different text messages,
only the first one is extracted, and its translation is used in place of both original text messages.
2018-03-03 08:06:01 -05:00
2018-03-22 05:18:48 -04:00
要确保自定义 id 是唯一的。如果你对两个*不同的*文本块使用了同一个 id, 那么就只有一个会被提取出来, 然后其翻译结果会被用于全部原始文本消息。
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
In the example below the custom id `myId` is used for two different messages:
2018-03-03 08:06:01 -05:00
2018-03-21 04:21:28 -04:00
在下面这个例子中,自定义的 id `myId` 就被用在了两个不同的消息上:
2017-05-02 02:01:20 -04:00
```html
2018-03-03 08:06:01 -05:00
2017-11-02 17:22:09 -04:00
< h3 i18n = "@@myId" > Hello< / h3 >
2018-03-03 08:06:01 -05:00
2017-11-02 17:22:09 -04:00
<!-- ... -->
2018-03-03 08:06:01 -05:00
2017-05-02 02:01:20 -04:00
< p i18n = "@@myId" > Good bye< / p >
2018-03-03 08:06:01 -05:00
2017-05-02 02:01:20 -04:00
```
2017-03-31 19:57:13 -04:00
2017-11-02 17:22:09 -04:00
Consider this translation to French:
2018-03-03 08:06:01 -05:00
2018-03-21 04:21:28 -04:00
考虑下列法文翻译:
2017-05-02 02:01:20 -04:00
```xml
2018-03-03 08:06:01 -05:00
2017-05-02 02:01:20 -04:00
< trans-unit id = "myId" datatype = "html" >
< source > Hello< / source >
2017-11-02 17:22:09 -04:00
< target state = "new" > Bonjour< / target >
2017-05-02 02:01:20 -04:00
< / trans-unit >
2018-03-03 08:06:01 -05:00
2017-05-02 02:01:20 -04:00
```
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
Because the custom id is the same, both of the elements in the resulting translation contain
the same text, `Bonjour` :
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
由于自定义 id 都是一样的,所以翻译结果中所有的元素都包含同样的文本 `Bonjour` :
2017-11-02 17:22:09 -04:00
```html
2018-03-03 08:06:01 -05:00
2017-11-02 17:22:09 -04:00
< h3 > Bonjour< / h3 >
2018-03-03 08:06:01 -05:00
2017-11-02 17:22:09 -04:00
<!-- ... -->
2018-03-03 08:06:01 -05:00
2017-11-02 17:22:09 -04:00
< p > Bonjour< / p >
2017-03-27 11:08:53 -04:00
2018-03-03 08:06:01 -05:00
```
2017-02-22 13:09:39 -05:00
2017-05-02 02:01:20 -04:00
{@a no-element}
2018-03-03 08:06:01 -05:00
2017-02-22 13:09:39 -05:00
### Translate text without creating an element
2017-03-31 19:57:13 -04:00
2017-08-03 20:47:51 -04:00
### 翻译文本,而不必创建元素
2017-11-02 17:22:09 -04:00
If there is a section of text that you would like to translate, you can wrap it in a `<span>` tag.
However, if you don't want to create a new DOM element merely to facilitate translation,
2017-09-01 08:50:58 -04:00
you can wrap the text in an `<ng-container>` element.
2017-11-02 17:22:09 -04:00
The `<ng-container>` is transformed into an html comment:
2017-08-03 20:47:51 -04:00
2018-03-22 05:18:48 -04:00
如果要翻译一段纯文本,你就可以把它用 `<span>` 标签包裹起来。
但如果由于某些原因(比如 CSS 结构方面的考虑),你可能不希望仅仅为了翻译而创建一个新的 DOM 元素,那么也可以把这段文本包裹进一个 `<ng-container>` 元素中。`< ng-container > ` 将被转换成一个 HTML 注释:
2017-08-03 20:47:51 -04:00
2017-04-21 20:21:45 -04:00
< code-example path = "i18n/src/app/app.component.html" region = "i18n-ng-container" title = "src/app/app.component.html" linenums = "false" >
2018-03-03 08:06:01 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
{@a translate-attributes}
2018-03-03 08:06:01 -05:00
2017-11-02 17:22:09 -04:00
## Add i18n translation attributes
2017-08-03 20:47:51 -04:00
## 添加 *i18n* 翻译属性
2017-11-02 17:22:09 -04:00
You also can translate attributes.
For example, assume that your template has an image with a `title` attribute:
2017-03-31 19:57:13 -04:00
2018-03-21 04:21:28 -04:00
你还可以翻译属性。
比如,假设你的模板中有一个带 `title` 属性的图片:
2017-08-03 20:47:51 -04:00
2017-11-02 17:22:09 -04:00
< code-example path = "i18n/doc-files/app.component.html" region = "i18n-title" title = "src/app/app.component.html" linenums = "false" >
2018-03-03 08:06:01 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
This `title` attribute needs to be translated.
2017-02-22 13:09:39 -05:00
2017-08-03 20:47:51 -04:00
这个 `title` 属性也需要翻译。
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
To mark an attribute for translation, add an attribute in the form of `i18n-x` ,
where `x` is the name of the attribute to translate. The following example shows how to mark the
`title` attribute for translation by adding the `i18n-title` attribute on the `img` tag:
2017-08-03 20:47:51 -04:00
2018-03-21 04:21:28 -04:00
要把一个属性标记为需要翻译的,就添加一个形如 `i18n-x` 的属性,其中的 `x` 是要翻译的属性的名字。
下面的例子中演示了如何通过给 `img` 标签添加 `i18n-title` 属性来把 `title` 属性标记为待翻译的。
2017-04-21 20:21:45 -04:00
< code-example path = "i18n/src/app/app.component.html" region = "i18n-title-translate" title = "src/app/app.component.html" linenums = "false" >
2018-03-03 08:06:01 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-03-31 19:57:13 -04:00
2017-11-02 17:22:09 -04:00
This technique works for any attribute of any element.
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
这个技巧适用于任何元素上的任何属性。
2017-11-02 17:22:09 -04:00
You also can assign a meaning, description, and id with the `i18n-x="<meaning>|<description>@@<id>"`
syntax.
2017-08-03 20:47:51 -04:00
2018-03-21 04:21:28 -04:00
你也同样可以使用 `i18n-x="<meaning>|<description>@@<id>"` 语法来指定一个含义和描述。
2017-03-31 19:57:13 -04:00
2017-11-02 17:22:09 -04:00
{@a plural-ICU}
2018-03-03 08:06:01 -05:00
2017-11-02 17:22:09 -04:00
## Translate singular and plural
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
## 翻译单数与复数
2017-08-03 20:47:51 -04:00
2017-02-22 13:09:39 -05:00
Different languages have different pluralization rules.
2017-08-03 20:47:51 -04:00
不同的语言有不同的单复数规则。
2017-11-02 17:22:09 -04:00
Suppose that you want to say that something was "updated x minutes ago".
In English, depending upon the number of minutes, you could display "just now", "one minute ago",
or "x minutes ago" (with x being the actual number).
Other languages might express the cardinality differently.
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
假设你要说某些东西“updated x minutes ago( 在 x 分钟前修改了)”。
在英语中,根据分钟数,可能要显示为 "just now"、"one minute ago" 或 "x minutes ago"(这里的 x 是实际的数量)。
而在其它语言中则可能会有不同的基数规则。
2017-08-03 20:47:51 -04:00
2017-11-02 17:22:09 -04:00
The example below shows how to use a `plural` ICU expression to display one of those three options
based on when the update occurred:
2017-08-03 20:47:51 -04:00
2018-03-21 04:21:28 -04:00
下面这个例子示范了如何使用 ICU 表达式 `plural` 来根据这次修改发生的时间显示这三个选项之一:
2017-04-21 20:21:45 -04:00
< code-example path = "i18n/src/app/app.component.html" region = "i18n-plural" title = "src/app/app.component.html" linenums = "false" >
2018-03-03 08:06:01 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2018-03-03 08:06:01 -05:00
* The first parameter is the key. It is bound to the component property (`minutes`), which determines
the number of minutes.
2018-03-21 04:21:28 -04:00
第一个参数是 key。它绑定到了组件中表示分钟数的 `minutes` 属性。
2017-08-03 20:47:51 -04:00
2017-02-22 13:09:39 -05:00
* The second parameter identifies this as a `plural` translation type.
2017-08-03 20:47:51 -04:00
2018-03-20 05:09:18 -04:00
第二个参数表示这是一个 `plural` (复数)翻译类型。
2018-02-27 19:08:59 -05:00
* The third parameter defines a pluralization pattern consisting of pluralization categories and their matching values.
2018-03-03 08:06:01 -05:00
第三个参数定义了一组复数表示模式,这个模式由复数类别和它们所匹配的值组成。
2017-11-02 17:22:09 -04:00
< div class = "l-sub-section" >
2017-08-03 20:47:51 -04:00
2017-11-02 17:22:09 -04:00
This syntax conforms to the
< a href = "http://userguide.icu-project.org/formatparse/messages" title = "ICU Message Format" > ICU Message Format< / a >
as specified in the
< a href = "http://cldr.unicode.org/index/cldr-spec/plural-rules" title = "Pluralization Rules" > CLDR pluralization rules< / a > .
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
这种语法遵守 < a href = "http://cldr.unicode.org/index/cldr-spec/plural-rules" title = "Pluralization Rules" > CLDR 复数规则< / a > 中指定的
< a href = "http://userguide.icu-project.org/formatparse/messages" title = "ICU Message Format" > ICU 消息格式< / a >
2017-11-02 17:22:09 -04:00
< / div >
2017-08-03 20:47:51 -04:00
2017-11-02 17:22:09 -04:00
Pluralization categories include (depending on the language):
2017-03-31 19:57:13 -04:00
2018-02-27 19:08:59 -05:00
复数类别包括(取决于语言):
2017-08-03 20:47:51 -04:00
2017-05-02 02:01:20 -04:00
* =0 (or any other number)
2017-08-03 20:47:51 -04:00
2018-03-03 08:06:01 -05:00
=0 (或其它数字)
2017-05-02 02:01:20 -04:00
* zero
2017-08-03 20:47:51 -04:00
2018-03-03 08:06:01 -05:00
zero( 零)
2017-08-03 20:47:51 -04:00
2017-05-02 02:01:20 -04:00
* one
2017-08-03 20:47:51 -04:00
2018-03-03 08:06:01 -05:00
one( 一个)
2017-08-03 20:47:51 -04:00
2017-05-02 02:01:20 -04:00
* two
2017-08-03 20:47:51 -04:00
2018-03-03 08:06:01 -05:00
two( 两个)
2017-08-03 20:47:51 -04:00
2017-02-22 13:09:39 -05:00
* few
2017-08-03 20:47:51 -04:00
2018-03-03 08:06:01 -05:00
few( 少数)
2017-08-03 20:47:51 -04:00
2017-05-02 02:01:20 -04:00
* many
2017-08-03 20:47:51 -04:00
2018-03-03 08:06:01 -05:00
many( 很多)
2017-08-03 20:47:51 -04:00
2017-02-22 13:09:39 -05:00
* other
2018-03-07 02:42:49 -05:00
other( 其它)
2017-11-02 17:22:09 -04:00
After the pluralization category, put the default English text in braces (`{}`).
2017-05-02 02:01:20 -04:00
2018-03-21 04:21:28 -04:00
复数类别之后的括号(`{}`)中是默认的*英语*文本。
2017-08-03 20:47:51 -04:00
2017-11-02 17:22:09 -04:00
In the example above, the three options are specified according to that pluralization pattern. For
talking about about zero minutes, you use `=0 {just now}` . For one minute, you use `=1 {one minute}` .
Any unmatched cardinality uses `other {{{minutes}} minutes ago}` . You could choose to add patterns
for two, three, or any other number if the pluralization rules were different. For the example of
"minute", only these three patterns are necessary in English.
2017-08-03 20:47:51 -04:00
2018-03-21 04:21:28 -04:00
在上面的例子中,这三个选项都是根据复数模式来指定的。要说零分钟,就用 `=0 {just now}` 。一分钟就用 `=1 {one minute}` 。
无法匹配的数量就用 `other {{{minutes}} minutes ago}` 。如果复数规则与此不同,你还可以为两个、三个火任意数量添加更多的模式。对于这个 “minute” 的例子,英语中只要这三种模式就够了。
2017-04-10 11:51:13 -04:00
< div class = "l-sub-section" >
2017-03-27 11:08:53 -04:00
2017-11-02 17:22:09 -04:00
You can use interpolations and html markup inside of your translations.
2017-08-03 20:47:51 -04:00
2018-03-21 04:21:28 -04:00
你可以在翻译结果中使用插值表达式和 HTML 标记。
2017-04-10 11:51:13 -04:00
< / div >
2017-03-27 11:08:53 -04:00
2017-11-02 17:22:09 -04:00
{@a select-ICU}
2018-03-03 08:06:01 -05:00
2017-11-02 17:22:09 -04:00
## Select among alternative text messages
2017-05-02 02:01:20 -04:00
2017-08-03 20:47:51 -04:00
## 在候选文本中选择
2017-11-02 17:22:09 -04:00
If your template needs to display different text messages depending on the value of a variable, you
need to translate all of those alternative text messages.
2017-08-03 20:47:51 -04:00
2018-03-21 04:21:28 -04:00
如果你的模板中需要根据某个变量的值显示出不同的文本消息,你还需要对所有这些候选文本进行翻译。
2017-11-02 17:22:09 -04:00
You can handle this with a `select` ICU expression. It is similar to the `plural` ICU expressions
except that you choose among alternative translations based on a string value instead of a number,
and you define those string values.
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
你可以使用 ICU 表达式 `select` 来翻译这些。它与 ICU 表达式 `plural` 类似,只是你要根据一个字符串值而不是数字来选择这些候选翻译文本,而这些字符串值是你自己定义的。
2017-11-02 17:22:09 -04:00
The following format message in the component template binds to the component's `gender` property,
which outputs one of the following string values: "m", "f" or "o".
The message maps those values to the appropriate translations:
2017-08-03 20:47:51 -04:00
2018-03-20 05:09:18 -04:00
组件模板中的下列消息格式绑定到了组件的 `gender` 属性,这个属性的取值是 "m" 或 "f" 或 "o"。
2017-08-03 20:47:51 -04:00
这个消息会把那些值映射到适当的翻译文本:
2017-04-21 20:21:45 -04:00
< code-example path = "i18n/src/app/app.component.html" region = "i18n-select" title = "src/app/app.component.html" linenums = "false" >
2018-03-03 08:06:01 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
{@a nesting-ICUS}
2018-03-03 08:06:01 -05:00
2017-11-02 17:22:09 -04:00
## Nesting plural and select ICU expressions
2017-02-22 13:09:39 -05:00
2017-08-03 20:47:51 -04:00
## 把"复数"与"选择"表达式嵌套在一起
2017-11-02 17:22:09 -04:00
You can also nest different ICU expressions together, as shown in this example:
2017-02-22 13:09:39 -05:00
2018-03-22 05:18:48 -04:00
你也可以把不同的 ICU 表达式嵌套在一起,比如:
2017-08-03 20:47:51 -04:00
2017-05-02 02:01:20 -04:00
< code-example path = "i18n/src/app/app.component.html" region = "i18n-nested" title = "src/app/app.component.html" >
2018-03-03 08:06:01 -05:00
2017-05-02 02:01:20 -04:00
< / code-example >
2017-03-31 19:57:13 -04:00
2017-05-02 02:01:20 -04:00
{@a ng-xi18n}
2018-03-03 08:06:01 -05:00
2017-11-02 17:22:09 -04:00
## Create a translation source file with _ng xi18n_
2017-02-22 13:09:39 -05:00
2018-03-20 05:15:36 -04:00
## 使用 *ng-xi18n* 工具创建翻译源文件
2017-07-22 23:51:25 -04:00
2018-03-03 08:06:01 -05:00
Use the `ng xi18n` command provided by the CLI to extract the text messages marked with `i18n` into
a translation source file.
2018-03-21 04:21:28 -04:00
使用 CLI 提供的 `ng xi18n` 命令来将带 `i18n` 标记的文本消息提取到一个翻译源文件中。
2017-07-22 23:51:25 -04:00
2017-11-02 17:22:09 -04:00
Open a terminal window at the root of the app project and enter the `ng xi18n` command:
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
在应用的项目根目录打开一个终端窗口,并输入 `ng xi18n` 命令:
2017-07-22 23:51:25 -04:00
2017-02-22 13:09:39 -05:00
< code-example language = "sh" class = "code-shell" >
2018-03-03 08:06:01 -05:00
2017-11-02 17:22:09 -04:00
ng xi18n
2018-03-03 08:06:01 -05:00
2017-02-22 13:09:39 -05:00
< / code-example >
2017-11-02 17:22:09 -04:00
By default, the tool generates a translation file named `messages.xlf` in the
< a href = "https://en.wikipedia.org/wiki/XLIFF" > XML Localization Interchange File Format
(XLIFF, version 1.2)< / a > .
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
本工具默认会生成一个名叫 `messages.xlf` 的翻译文件,格式为< a href = "https://en.wikipedia.org/wiki/XLIFF" target = "_blank" > XML本土化互换文件格式(XLIFF, version 1.2)</ a > 。
2017-04-10 11:51:13 -04:00
< div class = "l-sub-section" >
2017-03-27 11:08:53 -04:00
2018-01-25 03:52:26 -05:00
If you don't use the CLI, you have two options:
2018-03-03 08:06:01 -05:00
2018-03-21 04:21:28 -04:00
如果你不使用 CLI, 那么你有两个选择:
2018-01-25 03:52:26 -05:00
* You can use the `ng-xi18n` tool directly from the `@angular/compiler-cli` package.
For more information, see [i18n in the CLI documentation ](https://github.com/angular/angular-cli/wiki/xi18n ).
2018-03-03 08:06:01 -05:00
2018-03-21 04:21:28 -04:00
你可以直接使用来自 `@angular/compiler-cli` 包中的 `ng-xi18n` 工具。更多信息,参见 [CLI 文档中的 i18n 部分 ](https://github.com/angular/angular-cli/wiki/xi18n )。
2018-01-25 03:52:26 -05:00
* You can use the CLI Webpack plugin `AngularCompilerPlugin` from the `@ngtools/webpack` package.
Set the parameters `i18nOutFile` and `i18nOutFormat` to trigger the extraction.
For more information, see the [Angular Ahead-of-Time Webpack Plugin documentation ](https://github.com/angular/angular-cli/tree/master/packages/%40ngtools/webpack ).
2017-03-27 11:08:53 -04:00
2018-03-21 04:21:28 -04:00
你可以使用 CLI 中来自 `@ngtools/webpack` 包中的 Webpack 插件。设置其 `i18nOutFile` 和 `i18nOutFormat` 参数进行触发。
更多信息,参见 [Angular AOT Webpack 插件文档 ](https://github.com/angular/angular-cli/tree/master/packages/%40ngtools/webpack )。
2017-04-10 11:51:13 -04:00
< / div >
2017-03-27 11:08:53 -04:00
2017-02-22 13:09:39 -05:00
{@a other-formats}
2018-03-03 08:06:01 -05:00
2017-02-22 13:09:39 -05:00
### Other translation formats
2017-08-04 02:06:15 -04:00
### 其它翻译格式
2017-11-02 17:22:09 -04:00
Angular i18n tooling supports three translation formats:
2018-03-03 08:06:01 -05:00
2018-03-21 04:21:28 -04:00
Angular 的 i18n 工具支持三种翻译格式:
2017-11-02 17:22:09 -04:00
* XLIFF 1.2 (default)
2018-03-03 08:06:01 -05:00
2018-03-21 04:21:28 -04:00
XLIFF 1.2 (默认)
2017-11-02 17:22:09 -04:00
* XLIFF 2
2018-03-03 08:06:01 -05:00
2017-11-02 17:22:09 -04:00
* < a href = "http://cldr.unicode.org/development/development-process/design-proposals/xmb" > XML Message
Bundle (XMB)< / a >
2017-08-05 01:23:14 -04:00
2018-03-21 04:21:28 -04:00
< a href = "http://cldr.unicode.org/development/development-process/design-proposals/xmb" > XML 消息包 (XMB)< / a >
2017-11-02 17:22:09 -04:00
You can specify the translation format explicitly with the `--i18nFormat` flag as illustrated in
these example commands:
2017-03-30 15:04:18 -04:00
2018-03-21 04:21:28 -04:00
你可以使用 `--i18nFormat` 来明确指定想用的格式,范例如下:
2017-08-05 01:23:14 -04:00
2017-02-22 13:09:39 -05:00
< code-example language = "sh" class = "code-shell" >
2018-03-03 08:06:01 -05:00
2017-11-02 17:22:09 -04:00
ng xi18n --i18nFormat=xlf
ng xi18n --i18nFormat=xlf2
ng xi18n --i18nFormat=xmb
2018-03-03 08:06:01 -05:00
2017-02-22 13:09:39 -05:00
< / code-example >
2017-11-02 17:22:09 -04:00
The sample in this guide uses the default XLIFF 1.2 format.
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
本章的范例使用默认的 XLIFF 1.2 格式。
2017-11-02 17:22:09 -04:00
< div class = "l-sub-section" >
2017-08-05 01:23:14 -04:00
2017-11-02 17:22:09 -04:00
XLIFF files have the extension .xlf. The XMB format generates .xmb source files but uses
.xtb (XML Translation Bundle: XTB) translation files.
2018-03-21 04:21:28 -04:00
XLIFF 文件带有 .xlf 扩展名。XMB 格式会生成 .xmb 格式的源文件,但使用 .xtb( XML 翻译包)格式的翻译文件。
2017-11-02 17:22:09 -04:00
< / div >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
{@a ng-xi18n-options}
2018-03-03 08:06:01 -05:00
2017-02-22 13:09:39 -05:00
### Other options
2017-05-02 02:01:20 -04:00
2017-08-05 01:23:14 -04:00
### 其它选项
2017-11-02 17:22:09 -04:00
You can specify the output path used by the CLI to extract your translation source file with
the parameter `--outputPath` :
2017-03-30 15:04:18 -04:00
2018-03-21 04:21:28 -04:00
你还可以使用 `--outputPath` 参数在 CLI 提取翻译源文件时指定输出路径:
2017-08-05 01:23:14 -04:00
2017-02-22 13:09:39 -05:00
< code-example language = "sh" class = "code-shell" >
2017-11-02 17:22:09 -04:00
ng xi18n --outputPath src/locale
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
< / code-example >
2017-11-02 17:22:09 -04:00
You can change the name of the translation source file that is generated by the extraction tool with
the parameter `--outFile` :
2017-08-05 01:23:14 -04:00
2018-03-21 04:21:28 -04:00
你还可以使用 `--outFile` 参数来为提取工具生成的翻译源文件改名:
2017-11-02 17:22:09 -04:00
< code-example language = "sh" class = "code-shell" >
2017-03-30 15:04:18 -04:00
2017-11-02 17:22:09 -04:00
ng xi18n --outFile source.xlf
2017-08-05 01:23:14 -04:00
2017-02-22 13:09:39 -05:00
< / code-example >
2017-11-02 17:22:09 -04:00
You can specify the base locale of your app with the parameter `--locale` :
2017-08-05 01:23:14 -04:00
2018-03-21 04:21:28 -04:00
你还可以使用 `--locale` 参数来指定应用的基本地区:
2017-02-22 13:09:39 -05:00
< code-example language = "sh" class = "code-shell" >
2017-11-02 17:22:09 -04:00
ng xi18n --locale fr
2017-02-22 13:09:39 -05:00
< / code-example >
2017-11-02 17:22:09 -04:00
The extraction tool uses the locale to add the app locale information into your translation source
file. This information is not used by Angular, but external translation tools may need it.
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
该提取工具会用这个地区标识把本地化信息添加到你的翻译源文件中。这个信息对 Angular 来说没用,不过外部翻译工具可能会需要它。
2017-02-22 13:09:39 -05:00
{@a translate}
2018-03-03 08:06:01 -05:00
2017-02-22 13:09:39 -05:00
## Translate text messages
2017-07-22 23:51:25 -04:00
## 翻译文本信息
2018-03-03 08:06:01 -05:00
The `ng xi18n` command generates a translation source file named `messages.xlf` in the project `src`
folder.
2017-08-05 01:23:14 -04:00
2018-03-21 04:21:28 -04:00
`ng xi18n` 命令会在项目根目录生成一个名为 `messages.xlf` 的翻译源文件。
2017-07-22 23:51:25 -04:00
2018-03-03 08:06:01 -05:00
The next step is to translate this source file into the specific language
translation files. The example in this guide creates a French translation file.
2018-02-27 19:08:59 -05:00
下一步是将英文模板文本翻译到指定语言的翻译文件。
这个例子中创建了一个法语翻译文件。
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
{@a localization-folder}
2018-03-03 08:06:01 -05:00
2017-02-22 13:09:39 -05:00
### Create a localization folder
2017-07-22 23:51:25 -04:00
### 新建一个本土化目录
2017-11-02 17:22:09 -04:00
Most apps are translated into more than one other language. For this reason, it is standard practice
for the project structure to reflect the entire internationalization effort.
2017-02-22 13:09:39 -05:00
2018-02-27 19:08:59 -05:00
大多数应用都要被翻译成多种其它语言,因此,为全部国际化工作做适当的调整项目目录结构是一种标准实践。
2017-07-22 23:51:25 -04:00
2018-03-03 08:06:01 -05:00
One approach is to dedicate a folder to localization and store related assets, such as
2017-11-02 17:22:09 -04:00
internationalization files, there.
2017-03-27 11:08:53 -04:00
2018-03-21 04:21:28 -04:00
方法之一是为本土化和相关资源(比如国际化文件)创建一个专门的目录。
2017-07-22 23:51:25 -04:00
2017-04-10 11:51:13 -04:00
< div class = "l-sub-section" >
2017-03-27 11:08:53 -04:00
2017-11-02 17:22:09 -04:00
Localization and internationalization are
< a href = "https://en.wikipedia.org/wiki/Internationalization_and_localization" > different but
closely related terms< / a > .
2017-03-27 11:08:53 -04:00
2018-03-03 08:06:01 -05:00
本土化和国际化是< a href = "https://en.wikipedia.org/wiki/Internationalization_and_localization" target = "_blank" > 不同但是很相近的概念< / a > 。
2017-07-22 23:51:25 -04:00
2017-04-10 11:51:13 -04:00
< / div >
2017-03-27 11:08:53 -04:00
2017-11-02 17:22:09 -04:00
This guide follows that approach. It has a `locale` folder under `src/` .
Assets within that folder have a filename extension that matches their associated locale.
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
本指南遵循了这种方式。在`src/`目录下,有一个专门的`locale`目录,该目录中的文件都带一个与相关地区匹配的扩展名。
2017-11-02 17:22:09 -04:00
### Create the translation files
2017-08-06 01:21:34 -04:00
2018-03-21 04:21:28 -04:00
### 创建翻译文件
2017-11-02 17:22:09 -04:00
For each translation source file, there must be at least one language translation file for the
resulting translation.
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
对每个翻译文件来说,都必须至少有一个语言的翻译文件作为翻译结果。
2017-11-02 17:22:09 -04:00
For this example:
2017-08-06 01:21:34 -04:00
2018-03-21 04:21:28 -04:00
对于这个例子:
2017-11-02 17:22:09 -04:00
1. Make a copy of the `messages.xlf` file.
2018-03-03 08:06:01 -05:00
2018-03-21 04:21:28 -04:00
复制一份 `messages.xlf` 文件。
2017-11-02 17:22:09 -04:00
2. Put the copy in the `locale` folder.
2018-03-03 08:06:01 -05:00
2018-03-21 04:21:28 -04:00
把这个副本放进 `locale` 目录下。
2017-11-02 17:22:09 -04:00
3. Rename the copy to `messages.fr.xlf` for the French language translation.
2017-04-12 15:53:18 -04:00
2018-03-21 04:21:28 -04:00
把这个副本改名为 `messages.fr.xlf` 以作为法语翻译结果。
2017-11-02 17:22:09 -04:00
If you were translating to other languages, you would repeat these steps for each target language.
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
如果你要翻译为其他语言,那就为每一个目标语种重复上述步骤。
2017-04-12 15:53:18 -04:00
{@a translate-text-nodes}
2018-03-03 08:06:01 -05:00
2017-02-22 13:09:39 -05:00
### Translate text nodes
2017-08-04 02:06:15 -04:00
### 翻译文本节点
2018-03-03 08:06:01 -05:00
In a large translation project, you would send the `messages.fr.xlf` file to a French translator who
would enter the translations using an XLIFF file editor.
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
在现实世界中,`messages.fr.xlf` 文件会被发给法语翻译,他们会使用某种 XLIFF 文件编辑器来翻译它。
2017-07-22 23:51:25 -04:00
2017-11-02 17:22:09 -04:00
This sample file is easy to translate without a special editor or knowledge of French.
2017-02-22 13:09:39 -05:00
2018-03-22 05:18:48 -04:00
你不需要任何编辑器或者法语知识就可以轻易的翻译本例子文件。
2017-03-27 11:08:53 -04:00
2017-11-02 17:22:09 -04:00
1. Open `messages.fr.xlf` and find the first `<trans-unit>` section:
2017-08-03 19:39:15 -04:00
2018-03-21 04:21:28 -04:00
打开 `messages.fr.xlf` 并找到第一个 `<trans-unit>` 区:
2018-03-07 03:48:44 -05:00
> <code-example path="i18n/doc-files/messages.fr.xlf.html" region="translated-hello-before" title="src/locale/messages.fr.xlf (<trans-unit>)" linenums="false"></code-example>
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
> This XML element represents the translation of the `<h1>` greeting tag that you marked with the
`i18n` attribute earlier in this guide.
2018-03-21 04:21:28 -04:00
> 这个 XML 元素表示前面你加过 `i18n` 属性的那个打招呼用的 `<h1>` 标签。
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
> Note that the translation unit `id=introductionHeader` is derived from the
[custom `id` ](#custom-id "Set a custom id" ) that you set earlier, but
without the `@@` prefix required in the source HTML.
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
> 注意这个 `id=introductionHeader` 的翻译单元是来自你以前设置过的[自定义 `id`](#custom-id "Set a custom id")的。但是并没有源 HTML 所需的 `@@` 前缀。
2017-11-02 17:22:09 -04:00
2. Duplicate the `<source/>` tag, rename it `target` , and then replace its content with the French
greeting. If you were working with a more complex translation, you could use the the information
and context provided by the source, description, and meaning elements to guide your selection of
the appropriate French translation.
2017-07-22 23:51:25 -04:00
2018-03-21 04:21:28 -04:00
复制 `<source/>` 标记,把它改名为 `target` ,并把它的内容改为法语版的 “greeting”。
如果你要做的是更复杂的翻译,可能会使用由源文本、描述信息和含义等提供的信息和上下文来给出更恰当的法语翻译。
2018-03-07 03:48:44 -05:00
> <code-example path="i18n/doc-files/messages.fr.xlf.html" region="translated-hello" title="src/locale/messages.fr.xlf (<trans-unit>, after translation)" linenums="false"></code-example>
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
3. Translate the other text nodes the same way:
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
用同样的方式翻译其它文本节点:
2018-03-07 03:48:44 -05:00
> <code-example path="i18n/doc-files/messages.fr.xlf.html" region="translated-other-nodes" title="src/locale/messages.fr.xlf (<trans-unit>)" linenums="false"></code-example>
2017-02-22 13:09:39 -05:00
2017-04-10 11:51:13 -04:00
< div class = "alert is-important" >
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
**The Angular i18n tools generated the ids for these translation units. Don't change them.**
Each `id` depends upon the content of the template text and its assigned meaning.
If you change either the text or the meaning, then the `id` changes.
For more information, see the ** [translation file maintenance discussion ](#custom-id )**.
2017-08-05 01:23:14 -04:00
2018-03-21 04:21:28 -04:00
**Angular 的 i18n 工具会为这些翻译单元生成一些 id, 不要修改它们。**
每个 `id` 都是根据模板文本的内容及其含义来生成的。
无论你修改了文本还是含义,它们的 `id` 都会改变。
要了解更多信息,参见 ** [关于翻译文件可维护性的讨论 ](#custom-id )**。
2017-04-10 11:51:13 -04:00
< / div >
2017-02-22 13:09:39 -05:00
{@a translate-plural-select}
2018-03-03 08:06:01 -05:00
2017-11-02 17:22:09 -04:00
## Translate plural and select expressions
2017-03-31 19:57:13 -04:00
2018-03-21 04:21:28 -04:00
## 翻译复数( plural) 和选择( select) 表达式
2017-11-02 17:22:09 -04:00
_Plural_ and _select_ ICU expressions are extracted separately, so they require special attention
when preparing for translation.
2017-08-05 01:23:14 -04:00
2018-03-21 04:21:28 -04:00
*复数*和*选择*的 ICU 表达式都是分别提取的,所以在准备翻译时,它们需要格外注意。
2017-11-02 17:22:09 -04:00
Look for these expressions in relation to other translation units that you recognize from
elsewhere in the source template. In this example, you know the translation unit for the `select`
must be just below the translation unit for the logo.
2017-08-05 01:23:14 -04:00
2018-03-21 04:21:28 -04:00
你要从原始模板中其它地方识别出来的翻译单元来找到这些表达式之间的联系。
比如在这个例子中,你知道 `select` 一定会出现在 logo 的翻译单元的紧下方。
2017-04-12 15:53:18 -04:00
{@a translate-plural}
2018-03-03 08:06:01 -05:00
2017-04-12 15:53:18 -04:00
### Translate _plural_
2017-08-05 01:23:14 -04:00
2018-03-07 02:42:49 -05:00
### 翻译*复数*
2017-02-22 13:09:39 -05:00
To translate a `plural` , translate its ICU format match values:
2018-03-20 05:09:18 -04:00
要翻译一个复数,就要翻译它的 ICU 格式中匹配的值:
2017-08-05 01:23:14 -04:00
2017-11-02 17:22:09 -04:00
< code-example path = "i18n/doc-files/messages.fr.xlf.html" region = "translated-plural" title = "src/locale/messages.fr.xlf (<trans-unit>)" linenums = "false" >
2018-03-03 08:06:01 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
You can add or remove plural cases, with each language having its own cardinality. (See
[CLDR plural rules ](http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html ).)
2017-04-12 15:53:18 -04:00
2018-03-21 04:21:28 -04:00
你可以添加或删除复数的情况,每种语言都有自己的基数规则。(参见 [CLDR 复数规则 ](http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html ))。
2017-04-12 15:53:18 -04:00
{@a translate-select}
2018-03-03 08:06:01 -05:00
2017-04-12 15:53:18 -04:00
### Translate _select_
2017-08-05 01:23:14 -04:00
### 翻译*选择*( select)
2017-11-02 17:22:09 -04:00
Below is the content of our example `select` ICU expression in the component template:
2017-08-05 01:23:14 -04:00
2018-03-21 04:21:28 -04:00
下面是组件模板中的 ICU 表达式 `select` 的例子:
2017-04-21 20:21:45 -04:00
< code-example path = "i18n/src/app/app.component.html" region = "i18n-select" title = "src/app/app.component.html" linenums = "false" >
2018-03-03 08:06:01 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
The extraction tool broke that into two translation units because ICU expressions are extracted
separately.
2017-02-22 13:09:39 -05:00
2018-02-27 19:08:59 -05:00
提取工具会把它拆成*两个*翻译单元,因为 ICU 表达式是分别提取的。
2017-08-05 01:23:14 -04:00
2017-11-02 17:22:09 -04:00
The first unit contains the text that was outside of the `select` .
2017-04-12 15:53:18 -04:00
In place of the `select` is a placeholder, `<x id="ICU">` , that represents the `select` message.
2017-11-02 17:22:09 -04:00
Translate the text and move around the placeholder if necessary, but don't remove it. If you remove
the placeholder, the ICU expression will not be present in your translated app.
2017-02-22 13:09:39 -05:00
2018-03-20 05:09:18 -04:00
第一个单元包含 `select` 之外的文本。
这里的 `select` 是一个占位符 `<x id="ICU">` ,用来表示 `select` 中的消息。
2018-03-21 04:21:28 -04:00
翻译这段文本,如果需要就把占位符放在那里,但不要删除它。
如果删除了占位符, ICU 表达式就不会出现在翻译后的应用中。
2017-08-05 01:23:14 -04:00
2017-11-02 17:22:09 -04:00
< code-example path = "i18n/doc-files/messages.fr.xlf.html" region = "translate-select-1" title = "src/locale/messages.fr.xlf (<trans-unit>)" linenums = "false" >
2018-03-03 08:06:01 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
The second translation unit, immediately below the first one, contains the `select` message.
Translate that as well.
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
第一个翻译单元的紧下方就是第二个翻译单元,包含 `select` 中的消息。照样翻译它。
2017-08-05 01:23:14 -04:00
2017-11-02 17:22:09 -04:00
< code-example path = "i18n/doc-files/messages.fr.xlf.html" region = "translate-select-2" title = "src/locale/messages.fr.xlf (<trans-unit>)" linenums = "false" >
2018-03-03 08:06:01 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
Here they are together, after translation:
2017-03-31 19:57:13 -04:00
2017-08-05 01:23:14 -04:00
在翻译之后,它们会放在一起:
2017-11-02 17:22:09 -04:00
< code-example path = "i18n/doc-files/messages.fr.xlf.html" region = "translated-select" title = "src/locale/messages.fr.xlf (<trans-unit>)" linenums = "false" >
2018-03-03 08:06:01 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-03-31 19:57:13 -04:00
2017-11-02 17:22:09 -04:00
{@a translate-nested}
2018-03-03 08:06:01 -05:00
2017-05-02 02:01:20 -04:00
### Translate a nested expression
2017-02-22 13:09:39 -05:00
2017-08-05 01:23:14 -04:00
### 翻译嵌套的表达式
2017-11-02 17:22:09 -04:00
A nested expression is similar to the previous examples. As in the previous example, there are
two translation units. The first one contains the text outside of the nested expression:
2017-02-22 13:09:39 -05:00
2018-03-22 05:18:48 -04:00
嵌套的表达式和前一节没有什么不同。就像上一个例子中那样,这里有*两个*翻译单元。
2018-03-21 04:21:28 -04:00
第一个包含嵌套表达式之外的文本:
2017-08-05 01:23:14 -04:00
2017-11-02 17:22:09 -04:00
< code-example path = "i18n/doc-files/messages.fr.xlf.html" region = "translate-nested-1" title = "src/locale/messages.fr.xlf (<trans-unit>)" linenums = "false" >
2018-03-03 08:06:01 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-05-02 02:01:20 -04:00
The second unit contains the complete nested expression:
2017-02-22 13:09:39 -05:00
2017-08-05 01:23:14 -04:00
第二个包含完整的嵌套表达式:
2017-11-02 17:22:09 -04:00
< code-example path = "i18n/doc-files/messages.fr.xlf.html" region = "translate-nested-2" title = "src/locale/messages.fr.xlf (<trans-unit>)" linenums = "false" >
2018-03-03 08:06:01 -05:00
2017-05-02 02:01:20 -04:00
< / code-example >
2017-03-30 15:04:18 -04:00
2017-05-02 02:01:20 -04:00
And both together:
2017-02-22 13:09:39 -05:00
2017-08-05 01:23:14 -04:00
放在一起时:
2017-11-02 17:22:09 -04:00
< code-example path = "i18n/doc-files/messages.fr.xlf.html" region = "translate-nested" title = "src/locale/messages.fr.xlf (<trans-unit>)" linenums = "false" >
2018-03-03 08:06:01 -05:00
2017-05-02 02:01:20 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
The entire template translation is complete. The next section describes how to load that translation
into the app.
2017-03-31 19:57:13 -04:00
2018-03-21 04:21:28 -04:00
整个模板的翻译就完成了。下一节会讲如何翻译结果加载到应用程序中。
2017-11-02 17:22:09 -04:00
{@a app-pre-translation}
2018-03-03 08:06:01 -05:00
2017-11-02 17:22:09 -04:00
### The app and its translation file
2017-03-31 19:57:13 -04:00
2018-02-27 19:08:59 -05:00
### 应用及其翻译文件
2017-08-06 01:42:51 -04:00
2017-11-02 17:22:09 -04:00
The sample app and its translation file are now as follows:
2017-02-22 13:09:39 -05:00
2018-02-27 19:08:59 -05:00
下面是例子应用及其翻译文件:
2017-07-22 23:51:25 -04:00
2017-03-27 11:08:53 -04:00
< code-tabs >
2018-03-06 23:26:05 -05:00
2017-04-21 20:21:45 -04:00
< code-pane title = "src/app/app.component.html" path = "i18n/src/app/app.component.html" >
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-04-21 20:21:45 -04:00
< code-pane title = "src/app/app.component.ts" path = "i18n/src/app/app.component.ts" >
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-04-21 20:21:45 -04:00
< code-pane title = "src/app/app.module.ts" path = "i18n/src/app/app.module.ts" >
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-11-02 17:22:09 -04:00
< code-pane title = "src/main.ts" path = "i18n/doc-files/main.1.ts" >
2017-03-27 11:08:53 -04:00
< / code-pane >
2017-11-02 17:22:09 -04:00
< code-pane title = "src/locale/messages.fr.xlf" path = "i18n/doc-files/messages.fr.xlf.html" >
2017-03-27 11:08:53 -04:00
< / code-pane >
2018-03-06 23:26:05 -05:00
2017-03-27 11:08:53 -04:00
< / code-tabs >
2017-02-22 13:09:39 -05:00
{@a merge}
2018-03-03 08:06:01 -05:00
2017-02-22 13:09:39 -05:00
## Merge the completed translation file into the app
2017-07-22 23:51:25 -04:00
## 合并已经翻译的文件
2017-11-02 17:22:09 -04:00
To merge the translated text into component templates, compile the app with the completed
translation file. Provide the Angular compiler with three translation-specific pieces of information:
2017-07-22 23:51:25 -04:00
2018-03-21 04:21:28 -04:00
要把已经翻译的文件合并到组件模板,就要用翻译过的文件编译应用。
为 Angular 编译器提供三种与翻译有关的信息:
2017-04-12 15:53:18 -04:00
* The translation file.
2018-03-03 08:06:01 -05:00
翻译文件
2017-04-12 15:53:18 -04:00
* The translation file format.
2018-03-03 08:06:01 -05:00
翻译文件的格式
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
* The locale (`fr` or `en-US` for instance).
2017-07-22 23:51:25 -04:00
2018-03-21 04:21:28 -04:00
目标地区(比如 `fr` 或 `en-US` )。
2017-11-02 17:22:09 -04:00
The compilation process is the same whether the translation file is in `.xlf` format or in another
format that Angular understands, such as `.xtb` .
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
无论翻译文件是 `.xlf` 还是 Angular 支持的其它格式(比如 `.xtb` ),其编译过程都是一样的。
2017-11-02 17:22:09 -04:00
How you provide this information depends upon whether you compile with
the JIT compiler or the AOT compiler.
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
你如何提供这些信息取决于你使用的是JIT( 即时) 编译器还是AOT( 预先) 编译器。
2017-07-04 11:07:11 -04:00
* With [AOT ](guide/i18n#merge-aot ), you pass the information as a CLI parameter.
2017-07-22 23:51:25 -04:00
2018-03-21 04:21:28 -04:00
使用[AOT](guide/i18n#merge-aot)时,用在 CLI 的参数里传入这些信息。
2018-03-03 08:06:01 -05:00
* With [JIT ](guide/i18n#merge-jit ), you provide the information at bootstrap time.
2017-07-22 23:51:25 -04:00
2018-03-21 22:52:34 -04:00
使用[JIT](guide/i18n#merge-jit)时,在引导时提供。
2018-03-21 04:21:28 -04:00
2017-07-04 11:07:11 -04:00
{@a merge-aot}
2018-03-03 08:06:01 -05:00
2017-11-02 17:22:09 -04:00
### Merge with the AOT compiler
2017-07-22 23:51:25 -04:00
2018-03-21 04:21:28 -04:00
### 使用 AOT 编译器合并
2017-11-02 17:22:09 -04:00
The AOT (_Ahead-of-Time_) compiler is part of a build process that produces a small, fast,
ready-to-run application package.
2017-07-22 23:51:25 -04:00
2018-03-21 04:21:28 -04:00
AOT( *预先*)编译器是构建过程的一部分,它可以生成又小又快,直接可用的应用包。
2017-11-02 17:22:09 -04:00
When you internationalize with the AOT compiler, you must pre-build a separate application
package for each language and serve the appropriate package based on either server-side language
detection or url parameters.
2017-07-22 23:51:25 -04:00
2018-03-21 04:21:28 -04:00
当你使用 AOT 编译器进行国际化时,你必须为每种语言预先编译一个独立的应用包,并且依靠服务端语言检测或 URL 参数来找出合适的包。
2017-11-02 17:22:09 -04:00
You also need to instruct the AOT compiler to use your translation file. To do so, you use three
2018-03-03 08:06:01 -05:00
options with the `ng serve` or `ng build` commands:
2017-07-22 23:51:25 -04:00
2018-03-21 04:21:28 -04:00
你还要指示 AOT 编译器使用你的翻译结果文件,要这么做,你就要在 `ng serve` 或 `ng build` 命令中使用三个选项:
2017-11-02 17:22:09 -04:00
* `--i18nFile` : the path to the translation file.
2018-03-03 08:06:01 -05:00
2018-03-21 04:21:28 -04:00
`--i18nFile` : 翻译文件的路径。
2018-03-07 02:42:49 -05:00
2017-11-02 17:22:09 -04:00
* `--i18nFormat` : the format of the translation file.
2018-03-03 08:06:01 -05:00
2018-03-21 04:21:28 -04:00
`--i18nFormat` : 翻译文件的格式。
2017-11-02 17:22:09 -04:00
* `--locale` : the locale id.
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
`--locale` : 地区的 ID。
2017-11-02 17:22:09 -04:00
The example below shows how to serve the French language file created in previous sections of this
guide:
2017-07-22 23:51:25 -04:00
2018-03-21 04:21:28 -04:00
下面的例子演示了如何使用前面部分创建的法语文件来启动开发服务器:
2017-11-02 17:22:09 -04:00
< code-example language = "sh" class = "code-shell" >
2018-03-03 08:06:01 -05:00
2017-11-02 17:22:09 -04:00
ng serve --aot --i18nFile=src/locale/messages.fr.xlf --i18nFormat=xlf --locale=fr
2018-03-03 08:06:01 -05:00
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-07-04 11:07:11 -04:00
{@a merge-jit}
2018-03-03 08:06:01 -05:00
2017-11-02 17:22:09 -04:00
### Merge with the JIT compiler
2017-07-22 23:51:25 -04:00
2018-03-21 04:23:00 -04:00
### 用 JIT 编译器合并
2017-02-22 13:09:39 -05:00
2017-11-02 17:22:09 -04:00
The JIT compiler compiles the app in the browser as the app loads.
Translation with the JIT compiler is a dynamic process of:
2017-07-22 23:51:25 -04:00
2018-03-21 04:23:00 -04:00
JIT( 即时) 编译器在应用程序加载时, 在浏览器中编译应用。
在使用 JIT 编译器的环境中翻译是一个动态的流程,包括:
2017-07-22 23:51:25 -04:00
2017-11-02 17:22:09 -04:00
1. Importing the appropriate language translation file as a string constant.
2017-02-22 13:09:39 -05:00
2018-03-03 08:06:01 -05:00
把合适的语言翻译文件导入成一个字符串常量
2017-11-02 17:22:09 -04:00
2. Creating corresponding translation providers for the JIT compiler.
2017-04-12 15:53:18 -04:00
2018-03-21 04:23:00 -04:00
为 JIT 编译器创建相应的翻译提供商。
2018-03-03 08:06:01 -05:00
2017-11-02 17:22:09 -04:00
3. Bootstrapping the app with those providers.
2017-02-22 13:09:39 -05:00
2018-03-03 08:06:01 -05:00
使用这些提供商来启动应用。
2017-07-22 23:51:25 -04:00
2017-02-22 13:09:39 -05:00
Three providers tell the JIT compiler how to translate the template texts for a particular language
2017-11-02 17:22:09 -04:00
while compiling the app:
2017-02-22 13:09:39 -05:00
2018-03-21 04:23:00 -04:00
三种提供商帮助 JIT 编译在编译应用时,将模板文本翻译到某种语言:
2017-07-22 23:51:25 -04:00
2017-02-22 13:09:39 -05:00
* `TRANSLATIONS` is a string containing the content of the translation file.
2017-07-22 23:51:25 -04:00
2018-03-20 05:09:18 -04:00
`TRANSLATIONS` 是含有翻译文件内容的字符串。
2017-07-22 23:51:25 -04:00
2017-05-02 02:01:20 -04:00
* `TRANSLATIONS_FORMAT` is the format of the file: `xlf` , `xlf2` , or `xtb` .
2017-07-22 23:51:25 -04:00
2018-03-20 05:09:18 -04:00
`TRANSLATIONS_FORMAT` 是文件的格式: `xlf` 、`xlif` 或 `xtb` 。
2018-03-03 08:06:01 -05:00
2017-02-22 13:09:39 -05:00
* `LOCALE_ID` is the locale of the target language.
2018-03-20 05:09:18 -04:00
`LOCALE_ID` 是目标语言的语言环境。
2017-07-22 23:51:25 -04:00
2017-11-02 17:22:09 -04:00
The Angular `bootstrapModule` method has a second `compilerOptions` parameter that can influence the
behavior of the compiler. You can use it to provide the translation providers:
2017-02-22 13:09:39 -05:00
2018-03-20 05:09:18 -04:00
在下面的 `src/app/i18n-providers.ts` 文件的 `getTranslationProviders()` 函数中,根据用户的**语言环境**和对应的翻译文件构建这些提供商:
2017-07-22 23:51:25 -04:00
2017-11-02 17:22:09 -04:00
< code-example path = "i18n/doc-files/main.2.ts" title = "src/main.ts" >
2018-03-03 08:06:01 -05:00
2017-02-22 13:09:39 -05:00
< / code-example >
2017-11-02 17:22:09 -04:00
Then provide the `LOCALE_ID` in the main module:
2017-07-22 23:51:25 -04:00
2018-03-21 04:21:28 -04:00
然后在主文件包中提供 `LOCALE_ID` :
2017-11-02 17:22:09 -04:00
< code-example path = "i18n/doc-files/app.module.ts" title = "src/app/app.module.ts" linenums = "false" >
2017-02-22 13:09:39 -05:00
2018-03-03 08:06:01 -05:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-07-04 11:07:11 -04:00
{@a missing-translation}
2018-03-03 08:06:01 -05:00
2017-05-02 02:01:20 -04:00
### Report missing translations
2018-03-03 08:06:01 -05:00
2018-03-07 02:42:49 -05:00
### 汇报缺失的翻译
2017-07-04 11:07:11 -04:00
By default, when a translation is missing, the build succeeds but generates a warning such as
`Missing translation for message "foo"` . You can configure the level of warning that is generated by
the Angular compiler:
2017-02-22 13:09:39 -05:00
2018-03-21 04:21:28 -04:00
默认情况下,如果缺少了某个翻译文件,构建工具会成功但给出警告(如`Missing translation for message "foo"`)。你可以配置 Angular 编译器生成的警告级别:
2017-07-04 11:07:11 -04:00
* Error: throw an error. If you are using AOT compilation, the build will fail. If you are using JIT
compilation, the app will fail to load.
2018-03-03 08:06:01 -05:00
2018-03-21 04:21:28 -04:00
Error( 错误) : 抛出错误, 如果你使用的是 AOT 编译器,构建就会失败。如果使用的是 JIT 编译器,应用的加载就会失败。
2017-07-04 11:07:11 -04:00
* Warning (default): show a 'Missing translation' warning in the console or shell.
2018-03-03 08:06:01 -05:00
2018-03-21 04:21:28 -04:00
Warning( 警告 - 默认):在控制台中显示一条 “Missing translation” 警告。
2017-07-04 11:07:11 -04:00
* Ignore: do nothing.
2017-05-02 02:01:20 -04:00
2018-03-21 04:21:28 -04:00
Ignore( 忽略) : 什么也不做。
2017-07-04 11:07:11 -04:00
If you use the AOT compiler, specify the warning level by using the CLI parameter
`--missingTranslation` . The example below shows how to set the warning level to error:
2017-08-05 01:23:14 -04:00
2018-03-21 04:21:28 -04:00
如果你使用 AOT 编译器,可以使用 CLI 参数 `--missingTranslation` 来指定警告级别。下面的例子示范了如何把警告级别设置为 `error` :
2017-05-02 02:01:20 -04:00
< code-example language = "sh" class = "code-shell" >
2018-03-03 08:06:01 -05:00
2017-07-04 11:07:11 -04:00
ng serve --aot --missingTranslation=error
2018-03-03 08:06:01 -05:00
2017-05-02 02:01:20 -04:00
< / code-example >
2017-07-04 11:07:11 -04:00
If you use the JIT compiler, specify the warning level in the compiler config at bootstrap by adding
the 'MissingTranslationStrategy' property. The example below shows how to set the warning level to
error:
2017-08-05 01:23:14 -04:00
2018-03-21 04:21:28 -04:00
如果你要使用 JIT 编译器,就在启动时往编译器的配置中添加一个 `MissingTranslationStrategy` 属性来指定警告级别。下面的例子示范了如何把警告级别设置为 `error` :
2017-07-04 11:07:11 -04:00
< code-example path = "i18n/doc-files/main.3.ts" title = "src/main.ts" >
2018-03-03 08:06:01 -05:00
2017-07-04 11:07:11 -04:00
< / code-example >