2
2
@name i18n and l10n
3
3
@description
4
4
5
- # I18n and L10n in AngularJS
5
+ # i18n and l10n
6
6
7
- **What is i18n and l10n?**
7
+ Internationalization (i18n) is the process of developing products in such a way that they can be
8
+ localized for languages and cultures easily. Localization (l10n), is the process of adapting
9
+ applications and text to enable their usability in a particular cultural or linguistic market. For
10
+ application developers, internationalizing an application means abstracting all of the strings and
11
+ other locale-specific bits (such as date or currency formats) out of the application. Localizing an
12
+ application means providing translations and localized formats for the abstracted bits.
8
13
9
- Internationalization, abbreviated i18n, is the process of developing products in such a way that
10
- they can be localized for languages and cultures easily. Localization, abbreviated l10n, is the
11
- process of adapting applications and text to enable their usability in a particular cultural or
12
- linguistic market. For application developers, internationalizing an application means abstracting
13
- all of the strings and other locale-specific bits (such as date or currency formats) out of the
14
- application. Localizing an application means providing translations and localized formats for the
15
- abstracted bits.
16
14
17
- **What level of support for i18n/l10n is currently in Angular?**
15
+ ## How does Angular support i18n/l10n?
18
16
19
- Currently, Angular supports i18n/l10n for
20
- [datetime](http://docs.angularjs.org/#!/api/ng.filter:date),
21
- [number](http://docs.angularjs.org/#!/api/ng.filter:number) and
22
- [currency](http://docs.angularjs.org/#!/api/ng.filter:currency) filters.
17
+ Angular supports i18n/l10n for {@link ng.filter:date date}, {@link ng.filter:number number} and
18
+ {@link ng.filter:currency currency} filters.
23
19
24
- Additionally, Angular supports localizable pluralization support provided by the {@link
25
- ng.directive:ngPluralize ngPluralize directive}.
20
+ Additionally, Angular supports localizable pluralization support through the {@link
21
+ ng.directive:ngPluralize ` ngPluralize` directive}.
26
22
27
23
All localizable Angular components depend on locale-specific rule sets managed by the {@link
28
- ng.$locale $locale service}.
24
+ ng.$locale ` $locale` service}.
29
25
30
- For readers who want to jump straight into examples, we have a few web pages that showcase how to
31
- use Angular filters with various locale rule sets. You can find these examples either on
32
- [Github](https://github.com/angular/angular.js/tree/master/i18n/e2e) or in the i18n/e2e folder of
33
- Angular development package.
26
+ There a few examples that showcase how to use Angular filters with various locale rule sets in the
27
+ [`i18n/e2e` directory](https://github.com/angular/angular.js/tree/master/i18n/e2e) of the Angular
28
+ source code.
34
29
35
- **What is a locale id?**
30
+
31
+ ## What is a locale ID?
36
32
37
33
A locale is a specific geographical, political, or cultural region. The most commonly used locale
38
- ID consists of two parts: language code and country code. For example, en-US, en-AU, zh-CN are all
39
- valid locale IDs that have both language codes and country codes. Because specifying a country code
40
- in locale ID is optional, locale IDs such as en, zh, and sk are also valid. See the
41
- [ICU ](http://userguide.icu-project.org/locale) website for more information about using locale IDs.
34
+ ID consists of two parts: language code and country code. For example, `en-US`, `en-AU`, and
35
+ `zh-CN` are all valid locale IDs that have both language codes and country codes. Because
36
+ specifying a country code in locale ID is optional, locale IDs such as `en`, `zh`, and `sk` are
37
+ also valid. See the [ICU](http://userguide.icu-project.org/locale) website for more information
38
+ about using locale IDs.
39
+
40
+
41
+ ## Supported locales in Angular
42
42
43
- **Supported locales in Angular**
44
43
Angular separates number and datetime format rule sets into different files, each file for a
45
44
particular locale. You can find a list of currently supported locales
46
45
[here](https://github.com/angular/angular.js/tree/master/src/ngLocale)
47
- # Providing locale rules to Angular
46
+
47
+
48
+ ## Providing locale rules to Angular
48
49
49
50
There are two approaches to providing locale rules to Angular:
50
51
51
- ** 1. Pre-bundled rule sets**
52
+ ### 1. Pre-bundled rule sets
52
53
53
54
You can pre-bundle the desired locale file with Angular by concatenating the content of the
54
55
locale-specific file to the end of `angular.js` or `angular.min.js` file.
@@ -61,7 +62,7 @@ locale, you can do the following:
61
62
When the application containing `angular_de-de.js` script instead of the generic angular.js script
62
63
starts, Angular is automatically pre-configured with localization rules for the german locale.
63
64
64
- ** 2. Including locale js script in index.html page**
65
+ ### 2. Including a locale script in ` index.html`
65
66
66
67
You can also include the locale specific js file in the index.html page. For example, if one client
67
68
requires German locale, you would serve index_de-de.html which will look something like this:
@@ -77,48 +78,61 @@ requires German locale, you would serve index_de-de.html which will look somethi
77
78
</html>
78
79
```
79
80
80
- **Comparison of the two approaches**
81
- Both approaches described above requires you to prepare different index.html pages or js files for
82
- each locale that your app may be localized into. You also need to configure your server to serve
81
+ ### Comparison of the two approaches
82
+
83
+ Both approaches described above require you to prepare different `index.html` pages or JavaScript
84
+ files for each locale that your app may use. You also need to configure your server to serve
83
85
the correct file that correspond to the desired locale.
84
86
85
- However, the second approach (Including locale js script in index.html page) is likely to be slower
86
- because an extra script needs to be loaded.
87
+ The second approach (including the locale JavaScript file in `index.html`) may be slower because
88
+ an extra script needs to be loaded.
89
+
87
90
91
+ ## Caveats
88
92
89
- # "Gotchas"
93
+ Although Angular makes i18n convenient, there are several things you need to be conscious of as you
94
+ develop your app.
90
95
91
- ** Currency symbol "gotcha"**
96
+ ### Currency symbol
92
97
93
- Angular's [currency filter](http://docs.angularjs.org/#!/api/ng.filter:currency) allows
94
- you to use the default currency symbol from the {@link ng.$locale locale service},
95
- or you can provide the filter with a custom currency symbol. If your app will be used only in one
96
- locale, it is fine to rely on the default currency symbol. However, if you anticipate that viewers
97
- in other locales might use your app, you should provide your own currency symbol to make sure the
98
- actual value is understood.
98
+ Angular's {@link ng.filter:currency currency filter} allows you to use the default currency symbol
99
+ from the {@link ng.$locale locale service}, or you can provide the filter with a custom currency
100
+ symbol.
99
101
100
- For example, if you want to display an account balance of 1000 dollars with the following binding
101
- containing currency filter: `{{ 1000 | currency }}`, and your app is currently in en-US locale.
102
- '$1000.00' will be shown. However, if someone in a different local (say, Japan) views your app, their
103
- browser will specify the locale as ja, and the balance of '¥1000.00' will be shown instead. This
104
- will really upset your client.
102
+ <div class="alert alert-success">
103
+ **Best Practice:** If your app will be used only in one locale, it is fine to rely on the default
104
+ currency symbol. If you anticipate that viewers in other locales might use your app, you should
105
+ explicitly provide a currency symbol.
106
+ </div>
107
+
108
+ Let's say you are writing a banking app and you want to display an account balance of 1000 dollars.
109
+ You write the following binding using the currency filter:
110
+
111
+ ```html
112
+ {{ 1000 | currency }}
113
+ ```
114
+
115
+ If your app is currently in the `en-US` locale, the browser will show `$1000.00`. If someone in the
116
+ Japanese locale (`ja`) views your app, their browser will show a balance of `¥1000.00` instead.
117
+ This is problematinc because $1000 is not the same as ¥1000.
105
118
106
119
In this case, you need to override the default currency symbol by providing the
107
- [currency filter](http://docs.angularjs.org/#!/api/ng.filter:currency) with a currency symbol as
108
- a parameter when you configure the filter, for example, {{ 1000 | currency:"USD$"}}. This way,
109
- Angular will always show a balance of 'USD$1000' and disregard any locale changes.
120
+ {@link ng.filter:currency} currency filter with a currency symbol as a parameter.
121
+
122
+ If we change the above to `{{ 1000 | currency:"USD$"}}`, Angular will always show a balance of
123
+ `USD$1000` regardless of locale.
110
124
111
- ** Translation length "gotcha"**
125
+ ### Translation length
112
126
113
- Keep in mind that translated strings/datetime formats can vary greatly in length. For example,
114
- `June 3, 1977` will be translated to Spanish as `3 de junio de 1977`. There are bound to be other
115
- more extreme cases. Hence, when internationalizing your apps, you need to apply CSS rules
116
- accordingly and do thorough testing to make sure UI components do not overlap.
127
+ Translated strings/datetime formats can vary greatly in length. For example, `June 3, 1977` will be
128
+ translated to Spanish as `3 de junio de 1977`.
117
129
130
+ When internationalizing your app, you need to do thorough testing to make sure UI components behave
131
+ as expected even when their contents vary greatly in content size.
118
132
119
- ** Timezones**
133
+ ### Timezones
120
134
121
- Keep in mind that Angular datetime filter uses the time zone settings of the browser. So the same
135
+ The Angular datetime filter uses the time zone settings of the browser. The same
122
136
application will show different time information depending on the time zone settings of the
123
- computer that the application is running on. Neither Javascript nor Angular currently supports
137
+ computer that the application is running on. Neither JavaScript nor Angular currently supports
124
138
displaying the date with a timezone specified by the developer.
0 commit comments