Cross Reference: /yui3/src/intl/docs/intl-basic.mustache
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<style scoped>
#out .word {
margin:0.3em 0.3em 0.3em 1em;
}
#out .speaking {
font-size:108%;
color:#00aa00;
margin-top:1em;
margin-bottom:1em;
}
</style>
<div class="intro">
<p>This example shows how you can define language resource bundles for your custom module implementations; it also illustrates how YUI Loader can load the correct bundle based on the language you've chosen for your YUI instance.</p>
</div>
<div class="example">
{{>intl-basic-source}}
</div>
<h3>Defining your Custom Module</h3>
<p>We use Loader's groups support to add a custom module called "translator" under the group "myapp". The "lang" property in the module's metadata specifies which set of languages it supports.</p>
```
var appMetaData = {
myapp: {
base: '{{componentAssets}}',
modules : {
"translator" : {
path: 'translator/translator.js',
lang: ["en", "fr", "es"]
}
}
}
};
YUI({
lang:'fr',
groups: appMetaData
}).use(...);
```
<p>NOTE: Since this example is hosted on a page with other YUI instances, we don't want to pollute their configuration, so we just pass our `groups: appMetaData` configuration property to each YUI instance we create as shown above.</p>
<p>If you own all YUI instances on the page, you can use the global `YUI_Config` variable to define a global configuration for all YUI instances on the page, to avoid passing the same meta-data to all your instances as shown below:</p>
```
var YUI_Config = {
groups: {
myapp: {
base: '{{componentAssets}}',
modules : {
"translator" : {
path: 'translator/translator.js',
lang: ["en", "fr", "es"]
}
}
}
}
};
YUI({
lang:'fr'
}).use(...);
```
<h3>What Language Resource Bundles Look Like</h3>
<p>The language resource bundles for any module follows the pattern below:</p>
```
YUI.add("lang/translator_fr", function(Y) {
Y.Intl.add(
"translator", // Associated Module
"fr", // BCP 47 Language Tag
{ // Translated String Key/Value Pairs
hello:"Bonjour",
goodbye: "Au revoir"
}
);
}, "3.1.0");
```
<p>The `"lang/[for-module]_[lang]"` passed to `YUI.add` is the default module name used for language resource bundles, and the `Y.Intl.add` method is used to register the string name/value pair hash for a given module and language combination.
<h3>Generating Language Resource Bundles</h3>
<p>YUI Builder will handle the creation of the boiler plate code shown above, from the raw language files found in the module's `src/[module]/lang` subdirectory. The raw files under the `lang` directory contain just the string name/value pairs for each language.</p>
<p>As the component developer, the only thing you need to do is specify the `component.lang` property in the build properties file:</p>
```
# In your component's build properties file (src/[module]/build.properties for example):
component.lang=en,fr,es
```
<p>Provide the raw string name/value pairs in the `src/[component]/lang` subdirectory in your component's source area:</p>
```
// Contents of the raw src/[component]/lang/[component]_fr.js file
{
hello:"Bonjour",
goodbye: "Au revoir"
}
```
<p>And whenever you build your component code, the language resource bundles will be built and deployed too.</p>
<p>You can checkout the <a href="http://yuilibrary.com/yui3"></a>YUI 3 Source Code</code> and see the source code and build configuration files for the "console" and "datatype-date-format" modules to see a concrete example of this.</p>
<h3>Accessing Localized Resources In Your Class</h3>
<p>The Translator class implementation gets access to the localized strings by using `Y.Intl.get`, passing in the module name whose strings we need access to:</p>
```
function Translator() {
// Get localized strings in the current language
this._strs = Y.Intl.get("translator");
}
Translator.prototype = {
hi : function() {
return this._strs.hello;
},
bye : function() {
return this._strs.goodbye;
}
...
}
```
<h3>Specifying the Language for an Instance</h3>
<p>We specify the language to use for each instance, using the "lang" configuration property for the instance.</p>
<h4> An English instance</h4>
```
YUI({
lang:"en",
...
}).use("node-base", "translator", function(Y) {
var translator = new Y.Translator(),
out = Y.one("#out");
say("Speaking in: " + Y.Intl.getLang("translator"), out);
say(translator.hi(), out);
say(translator.bye(), out);
});
```
<h4>A French YUI Instance</h4>
```
YUI({
lang:"fr",
...
}).use("node-base", "translator", function(Y) {
...
});
```
<h4>A Spanish YUI Instance</h4>
```
YUI({
lang:"es",
...
}).use("node-base", "translator", function(Y) {
...
});
```
<h3>Modules Shipping With Language Resource Bundles</h3>
<p>As mentioned above, the `datatype` module (specifically the `datatype-date-format` module) and `console` are shipped with language resource bundles. Datatype ships with over 50 different languages supported, and Console ships with en and es language resource bundles, mainly as a demonstration of how language resource bundles are defined and used for Widget development.</p>
<h2>Complete Example Source</h2>
```
{{>intl-basic-source}}
```