Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit e6199a8

Browse files
authored
docs(i18n): add internationalization (i18n) guide (#2491)
* docs(i18n): add internationalization (i18n) guide * docs(cb-i18n): revamped to System.import the translation file
1 parent 64d5b3d commit e6199a8

27 files changed

+590
-5
lines changed

gulpfile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ function runE2eTsTests(appDir, outputFile) {
297297
} catch (e) {
298298
exampleConfig = {};
299299
}
300-
300+
301301
var config = {
302302
build: exampleConfig.build || 'tsc',
303303
run: exampleConfig.run || 'http-server:e2e'
@@ -1263,7 +1263,7 @@ function apiExamplesWatch(postShredAction) {
12631263
}
12641264

12651265
function devGuideExamplesWatch(shredOptions, postShredAction, focus) {
1266-
var watchPattern = focus ? '**/' + focus + '/**/*.*' : '**/*.*';
1266+
var watchPattern = focus ? '**/{' + focus + ',cb-' + focus+ '}/**/*.*' : '**/*.*';
12671267
var includePattern = path.join(shredOptions.examplesDir, watchPattern);
12681268
// removed this version because gulp.watch has the same glob issue that dgeni has.
12691269
// var excludePattern = '!' + path.join(shredOptions.examplesDir, '**/node_modules/**/*.*');
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path='../_protractor/e2e.d.ts' />
2+
'use strict';
3+
describe('i18n E2E Tests', () => {
4+
5+
beforeEach(function () {
6+
browser.get('');
7+
});
8+
9+
it('should display i18n translated welcome: Bonjour i18n!', function () {
10+
expect(element(by.css('h1')).getText()).toEqual('Bonjour i18n!');
11+
});
12+
13+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
**/*.ngfactory.ts
2+
**/*.metadata.json
3+
**/messages.xlf
4+
dist
5+
!app/tsconfig.json
6+
!rollup.js
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!--#docregion greeting-->
2+
<h1>Hello i18n!</h1>
3+
<!--#enddocregion greeting-->
4+
5+
<!--#docregion i18n-attribute-->
6+
<h1 i18n>Hello i18n!</h1>
7+
<!--#enddocregion i18n-attribute-->
8+
9+
<!--#docregion i18n-attribute-desc-->
10+
<h1 i18n="An introduction header for this sample">Hello i18n!</h1>
11+
<!--#enddocregion i18n-attribute-desc-->
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<!--#docregion-->
2+
<!--#docregion i18n-attribute-meaning-->
3+
<h1 i18n="User welcome|An introduction header for this sample">Hello i18n!</h1>
4+
<!--#enddocregion i18n-attribute-meaning-->
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// #docregion
2+
import { Component } from '@angular/core';
3+
4+
@Component({
5+
moduleId: module.id,
6+
selector: 'my-app',
7+
templateUrl: 'app.component.html'
8+
})
9+
export class AppComponent { }
10+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// #docregion
2+
import { NgModule } from '@angular/core';
3+
import { BrowserModule } from '@angular/platform-browser';
4+
5+
import { AppComponent } from './app.component';
6+
7+
@NgModule({
8+
imports: [ BrowserModule ],
9+
declarations: [ AppComponent ],
10+
bootstrap: [ AppComponent ]
11+
})
12+
13+
export class AppModule { }
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// #docregion
2+
import { TRANSLATIONS, TRANSLATIONS_FORMAT, LOCALE_ID } from '@angular/core';
3+
4+
export function getTranslationProviders(): Promise<Object[]> {
5+
6+
// Get the locale id from the global
7+
const locale = document['locale'] as string;
8+
9+
// return no providers if fail to get translation file for locale
10+
const noProviders: Object[] = [];
11+
12+
// No locale or English: no translation providers
13+
if (!locale || locale === 'en') {
14+
return Promise.resolve(noProviders);
15+
}
16+
17+
// Ex: 'i18n/fr/messages.fr.xlf`
18+
const translationFile = `./i18n/${locale}/messages.${locale}.xlf`;
19+
20+
return getTranslationsWithSystemJs(translationFile)
21+
.then( (translations: string ) => [
22+
{ provide: TRANSLATIONS, useValue: translations },
23+
{ provide: TRANSLATIONS_FORMAT, useValue: 'xlf' },
24+
{ provide: LOCALE_ID, useValue: locale }
25+
])
26+
.catch(() => noProviders); // ignore if file not found
27+
}
28+
29+
declare var System: any;
30+
31+
function getTranslationsWithSystemJs(file: string) {
32+
return System.import(file + '!text'); // relies on text plugin
33+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// #docregion
2+
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
3+
4+
import { AppModule } from './app.module';
5+
6+
platformBrowserDynamic().bootstrapModule(AppModule);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// #docregion
2+
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
3+
import { getTranslationProviders } from './i18n-providers';
4+
5+
import { AppModule } from './app.module';
6+
7+
getTranslationProviders().then(providers => {
8+
const options = { providers };
9+
platformBrowserDynamic().bootstrapModule(AppModule, options);
10+
});

0 commit comments

Comments
 (0)