-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
488 lines (436 loc) · 14.2 KB
/
index.html
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Ember and Localization</title>
<meta name="description" content="Quick presentation on how to effectivly localize ember applications">
<meta name="author" content="Chris Rice">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/black.css" id="theme">
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1>Localization In Ember</h1>
<h3>How to effectivly globalize and localize an ember application</h3>
<h6>(and web apps in general)</h6>
<p>
<small>Created by <a href="http://wetzelrice.com">Chris Rice</a> / <a href="https://github.com/Kilowhisky">Github: Kilowhisky</a></small> <br/>
</p>
</section>
<section>
<h3>What we are after</h3>
<img src="localization-example.gif" width="400"/>
</section>
<section>
<h2>Whats the difference?</h2>
<p><strong>Globalization</strong> is the process of designing and developing applications that function for multiple cultures.</p>
<p><strong>Localization</strong> is the process of customizing your application for a given culture and locale.</p>
<a href="http://msdn.microsoft.com/en-us/library/c6zyy3s9.aspx">Source</a>
</section>
<section>
<h2>What things need globalization?</h2>
<ol>
<li>Number parsing</li>
<li>Date parsing</li>
<li>Regex</li>
<li>Currency</li>
<li>Non-Latin Characters</li>
</ol>
<p>123.43 -> 123,43</p>
<p>10/01/2016 -> 2016/10/01</p>
<p>Mitigation is in the API and platform specific</p>
</section>
<section>
<h2>Globalization basics</h2>
Here are some quick tips for API communication.
<ul>
<li>Never pass a string formatted date. Always .toJSON()!</li>
<li>Never pass a string formatted number.</li>
<li>Always use and store in UTF-8!</li>
<li>Be flexible in validation. Try not to apply your locale's conventions. (phone numbers,etc..)</li>
<li>Be sure to localize error messages!</li>
<li>Decide early on if you want to support right-to-left languages or other alphabets.</li>
</ol>
</section>
<section>
<h2>What needs localization?</h2>
<ol>
<li>Numbers</li>
<li>Dates</li>
<li>Text</li>
<li>Plugins</li>
<li>API Responses</li>
</ol>
</section>
<section>
<h2>Basic Premise of localizing a web app</h2>
<ol>
<li>Detect the users localization automatically</li>
<li>Tell the browser we want to localize</li>
<li>Tell our API that we want to localize</li>
<li>Manipulate our application into awesomeness</li>
</ol>
</section>
<section>
<h2>What will we need?</h2>
<ul>
<li><a href="https://github.com/jamesarosen/ember-i18n">ember-i18n</a> addon that is the de facto standard localizing an ember application</li>
<li><a href="https://github.com/stefanpenner/ember-moment">ember-moment</a> addon that helps with date localization</li>
<li><a href="https://github.com/jasonmit/ember-cli-moment-shim">ember-cli-moment-shim</a> if you don't like ember-moment you use this instead</li>
<li><a href="https://github.com/kellyselden/ember-i18n-csv">ember-i18n-csv</a> for exporting translations to csv so you can send them to a translator</li>
</ul>
</section>
<section data-markdown>
<script type="text/template">
## Install Plugins
```bash
ember install ember-i18n
ember install ember-moment
```
</script>
</section>
<section data-markdown>
<script type="text/template">
## Setup Plugins
```javascript
// config/environment.js
module.exports = function (environment)
{
var ENV = {
i18n: {
defaultLocale: 'en-us'
},
moment: {
includeLocales: ['en', 'pt-br', 'es'],
}
};
return ENV;
};
```
</script>
</section>
<section data-markdown>
<script type="text/template">
## Generate Locale's
```bash
ember generate locale en-us
ember generate locale pt-br
ember generate locale es-es
```
[Defining i18n translations](https://github.com/jamesarosen/ember-i18n/wiki/Doc:-Defining-Translations)
</script>
</section>
<section data-markdown>
<script type="text/template">
## Initializing the locale
```javascript
// app/instance-initializers/restore-locale.js
let locale = session.get('MyLocalePreference');
let locales = i18n.get('locales');
if (!locale) {
locale = navigator.language || // Everyone
navigator.userLanguage || // IE... because
Config.i18n.defaultLocale;
}
locale = locale.toLowerCase();
for (let i = 0; i < locales.length; i++) {
let appLocale = locales.objectAt(i);
if (appLocale.toLowerCase().indexOf(locale) === 0) {
i18n.set('locale', appLocale);
moment.locale(appLocale);
break;
}
}
```
</script>
</section>
<section data-markdown>
<script type="text/template">
## Protip
If you are using [ember-validations](https://github.com/DockYard/ember-validations) you need to hack i18n support
```javascript
// app/instance-initializers/restore-locale.js
export function initialize(instance) {
Ember.I18n = instance.lookup('service:i18n');
}
```
[Issue on the subject](https://github.com/dockyard/ember-validations/issues/366)
</script>
</section>
<section data-markdown>
<script type="text/template">
## Setup the html lang tag
The HTML lang tag is important in informing the browser that the app is localized
```javascript
// app/instance-initializers/restore-locale.js
export function initialize(instance) {
let i18n = instance.lookup('service:i18n');
let router = instance.lookup('router:main');
Ember.addObserver(i18n, 'locale', function() {
Ember.run.schedule('afterRender', this, function() {
$('html').attr('lang', locale);
if(router.isActive('application')){
router.updateTitle();
}
});
});
```
Protip: If you are using [ember-cli-document-title](https://github.com/kimroen/ember-cli-document-title) you need to hack i18n support
</script>
</section>
<section data-markdown>
<script type="text/template">
## Tell the world
Inform all servers of our locale preference on each request
```javascript
// app/instance-initializers/restore-locale.js
export function initialize(instance) {
let i18n = instance.lookup('service:i18n');
$.ajaxPrefilter((options, originalOptions, xhr) => {
xhr.setRequestHeader("Accept-Language", i18n.get('locale'));
});
}
```
[more info about the accept language header](https://www.w3.org/International/questions/qa-accept-lang-locales)
</script>
</section>
<section data-markdown>
<script type="text/template">
## API response flow
1. Perform conditional processing based on `Accept-language` header
2. Have our API inform us of the locale its sending with `Content-Language` header (optional)
3. Have ember data interpret the response header via `ajaxSuccess` hook
</script>
</section>
<section data-markdown>
<script type="text/template">
## Handle missing locales
i18n's default behavior for missing locales is to return nothing. Thats annoying to me.
```javascript
// app/utils/i18n/missing-message.js
var defaultLocale;
export default function(locale, key, data) {
if(locale === 'en-us'){
return `Missing Translation for ${key}`;
}
if(!defaultLocale){
defaultLocale = new Locale('en-us', getOwner(this));
}
const count = Ember.get(data, 'count');
const defaults = Ember.makeArray(Ember.get(data, 'default'));
defaults.unshift(key);
const template = defaultLocale.getCompiledTemplate(defaults, count);
return template(data);
}
```
[Grabbed from here](https://github.com/jamesarosen/ember-i18n/pull/312)
</script>
</section>
<section data-markdown>
<script type="text/template">
## Now on to the fun of i18n
(the incredibly unbearably tedious type of fun)
</script>
</section>
<section data-markdown>
<script type="text/template">
## Rules of the road
* Try and avoid putting HTML in your templates
* Build your app with as little text in images as possible
* When desiging your UI keep in mind that some words may be longer in other languages
</script>
</section>
<section data-markdown>
<script type="text/template">
## General flow
1. Write a component, route, unit of code in your native language
2. Pull all the language out of the template and into your locale file
3. Replace language with locale keys
</script>
</section>
<section data-markdown>
<script type="text/template">
## Locale files
```
// app/locales/en-us.js
export default {
"locales": {
"es-es": "Spanish",
"en-us": "English",
"pt-br": "Brazilian Portuguese"
},
```
Locale files are just JSON files.
Keys can be as nested as you want.
</script>
</section>
<section data-markdown>
<script type="text/template">
## Extracting text in templates
This
```html
<strong>Report Application Issue</strong>
```
Becomes
```html
<strong>{{t 'reportIssues.title'}}</strong>
```
```javascript
// app/locales/en-us.js
"reportIssues": {
"title" : "Report Application Issue"
}
```
</script>
</section>
<section data-markdown>
<script type="text/template">
## How to handle numbers
This
```html
<p>You have submitted 6 support cases</p>
```
Becomes
```html
<p>{{t 'reportIssues.numberCases' count=userReportedCaseCount}}</p>
```
```javascript
// app/locales/en-us.js
"reportIssues": {
"numberCases" : "You have submitted {{count}} support cases"
}
```
You can pass safeStrings, numbers, computedProperties, etc..
</script>
</section>
<section data-markdown>
<script type="text/template">
## Handling html
1. Use Individual keys (recommended for large html chunks).
2. Put HTML in locale file (ok for minor stuff).
```html
<p>You should <strong>really</strong> think</p>
```
```html
<p>{{{t 'reportIssues.think'}}}</p>
```
```javascript
// app/locales/en-us.js
"reportIssues": {
"think" : "You should <strong>really</strong> think"
}
```
Note the tripple `{{{`. This disables HTML escaping.
</script>
</section>
<section data-markdown>
<script type="text/template">
## Using localization in code
```javascript
// someComponent.js
i18n: Ember.inject.service(),
actions: {
error() {
this.notifications.error(this.get('i18n').t('loadingError'));
}
}
```
</script>
</section>
<section data-markdown>
<script type="text/template">
## Using directly as a property
```javascript
// someComponent.js
import { translationMacro as t } from "ember-i18n";
export default Ember.Component.extend({
title: t('myTitle')
})
```
```javascript
// someComponent.hbs
{{title}}
```
</script>
</section>
<section data-markdown>
<script type="text/template">
## Observe the locale change
```javascript
// someComponent.js
i18n: Ember.inject.service(),
landingImgUrl: Ember.computed('i18n.locale', () => {
this.get('i18n.locale').t('landingImgUrl');
})
```
</script>
</section>
<section data-markdown>
<script type="text/template">
## Now what?
What do you do with that hundred of keys in en-us.js
</script>
</section>
<section data-markdown>
<script type="text/template">
## Get your files translated!
Export your translations to CSV if you want
```Bash
ember-i18n-csv to-csv --locales-path=/app/locales/ --csv-path=i18n.csv --only-missing
```
Once done you can import them back.
```Bash
ember-i18n-csv to-js --csv-path=i18n.csv --locales-path=/app/ocales --merge
```
</script>
</section>
<section>
<h2>Questions?</h2>
<h3>Demo Time</h3>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
slideNumber: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true },
{ src: 'plugin/notes/notes.js', async: true }
]
});
</script>
</body>
</html>