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

docs(i18n): add internationalization (i18n) guide #2491

Merged
merged 2 commits into from
Sep 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ function runE2eTsTests(appDir, outputFile) {
} catch (e) {
exampleConfig = {};
}

var config = {
build: exampleConfig.build || 'tsc',
run: exampleConfig.run || 'http-server:e2e'
Expand Down Expand Up @@ -1263,7 +1263,7 @@ function apiExamplesWatch(postShredAction) {
}

function devGuideExamplesWatch(shredOptions, postShredAction, focus) {
var watchPattern = focus ? '**/' + focus + '/**/*.*' : '**/*.*';
var watchPattern = focus ? '**/{' + focus + ',cb-' + focus+ '}/**/*.*' : '**/*.*';
var includePattern = path.join(shredOptions.examplesDir, watchPattern);
// removed this version because gulp.watch has the same glob issue that dgeni has.
// var excludePattern = '!' + path.join(shredOptions.examplesDir, '**/node_modules/**/*.*');
Expand Down
13 changes: 13 additions & 0 deletions public/docs/_examples/cb-i18n/e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path='../_protractor/e2e.d.ts' />
'use strict';
describe('i18n E2E Tests', () => {

beforeEach(function () {
browser.get('');
});

it('should display i18n translated welcome: Bonjour i18n!', function () {
expect(element(by.css('h1')).getText()).toEqual('Bonjour i18n!');
});

});
6 changes: 6 additions & 0 deletions public/docs/_examples/cb-i18n/ts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
**/*.ngfactory.ts
**/*.metadata.json
**/messages.xlf
dist
!app/tsconfig.json
!rollup.js
11 changes: 11 additions & 0 deletions public/docs/_examples/cb-i18n/ts/app/app.component.1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!--#docregion greeting-->
<h1>Hello i18n!</h1>
<!--#enddocregion greeting-->

<!--#docregion i18n-attribute-->
<h1 i18n>Hello i18n!</h1>
<!--#enddocregion i18n-attribute-->

<!--#docregion i18n-attribute-desc-->
<h1 i18n="An introduction header for this sample">Hello i18n!</h1>
<!--#enddocregion i18n-attribute-desc-->
4 changes: 4 additions & 0 deletions public/docs/_examples/cb-i18n/ts/app/app.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!--#docregion-->
<!--#docregion i18n-attribute-meaning-->
<h1 i18n="User welcome|An introduction header for this sample">Hello i18n!</h1>
<!--#enddocregion i18n-attribute-meaning-->
10 changes: 10 additions & 0 deletions public/docs/_examples/cb-i18n/ts/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// #docregion
import { Component } from '@angular/core';

@Component({
moduleId: module.id,
selector: 'my-app',
templateUrl: 'app.component.html'
})
export class AppComponent { }

13 changes: 13 additions & 0 deletions public/docs/_examples/cb-i18n/ts/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// #docregion
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent } from './app.component';

@NgModule({
imports: [ BrowserModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})

export class AppModule { }
33 changes: 33 additions & 0 deletions public/docs/_examples/cb-i18n/ts/app/i18n-providers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// #docregion
import { TRANSLATIONS, TRANSLATIONS_FORMAT, LOCALE_ID } from '@angular/core';

export function getTranslationProviders(): Promise<Object[]> {

// Get the locale id from the global
const locale = document['locale'] as string;

// return no providers if fail to get translation file for locale
const noProviders: Object[] = [];

// No locale or English: no translation providers
if (!locale || locale === 'en') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

en-US

return Promise.resolve(noProviders);
}

// Ex: 'i18n/fr/messages.fr.xlf`
const translationFile = `./i18n/${locale}/messages.${locale}.xlf`;

return getTranslationsWithSystemJs(translationFile)
.then( (translations: string ) => [
{ provide: TRANSLATIONS, useValue: translations },
{ provide: TRANSLATIONS_FORMAT, useValue: 'xlf' },
{ provide: LOCALE_ID, useValue: locale }
])
.catch(() => noProviders); // ignore if file not found
}

declare var System: any;

function getTranslationsWithSystemJs(file: string) {
return System.import(file + '!text'); // relies on text plugin
}
6 changes: 6 additions & 0 deletions public/docs/_examples/cb-i18n/ts/app/main.1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// #docregion
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app.module';

platformBrowserDynamic().bootstrapModule(AppModule);
10 changes: 10 additions & 0 deletions public/docs/_examples/cb-i18n/ts/app/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// #docregion
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { getTranslationProviders } from './i18n-providers';

import { AppModule } from './app.module';

getTranslationProviders().then(providers => {
const options = { providers };
platformBrowserDynamic().bootstrapModule(AppModule, options);
});
Empty file.
13 changes: 13 additions & 0 deletions public/docs/_examples/cb-i18n/ts/i18n/fr/messages.fr.xlf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="ng2.template">
<body>
<trans-unit id="af2ccf4b5dba59616e92cf1531505af02da8f6d2" datatype="html">
<source>Hello i18n!</source>
<target>Bonjour i18n!</target>
<note priority="1" from="description">An introduction header for this sample</note>
<note priority="1" from="meaning">User welcome</note>
</trans-unit>
</body>
</file>
</xliff>
17 changes: 17 additions & 0 deletions public/docs/_examples/cb-i18n/ts/i18n/fr/messages.fr.xlf.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!-- The `messages.fr.xlf` after translation for documentation purposes -->
<!-- #docregion -->
<?xml version="1.0" encoding="UTF-8" ?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="ng2.template">
<body>
<!-- #docregion trans-unit -->
<trans-unit id="af2ccf4b5dba59616e92cf1531505af02da8f6d2" datatype="html">
<source>Hello i18n!</source>
<target>Bonjour i18n!</target>
<note priority="1" from="description">An introduction header for this sample</note>
<note priority="1" from="meaning">User welcome</note>
</trans-unit>
<!-- #enddocregion trans-unit -->
</body>
</file>
</xliff>
7 changes: 7 additions & 0 deletions public/docs/_examples/cb-i18n/ts/i18n/trans-unit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!-- #docregion -->
<trans-unit id="af2ccf4b5dba59616e92cf1531505af02da8f6d2" datatype="html">
<source>Hello i18n!</source>
<target/>
<note priority="1" from="description">An introduction header for this sample</note>
<note priority="1" from="meaning">User welcome</note>
</trans-unit>
39 changes: 39 additions & 0 deletions public/docs/_examples/cb-i18n/ts/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<!-- #docregion -->
<html>
<head>
<title>Angular i18n example</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">

<script src="node_modules/core-js/client/shim.min.js"></script>

<script src="node_modules/zone.js/dist/zone.js"></script>
<script src="node_modules/reflect-metadata/Reflect.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>

<script src="systemjs.config.js"></script>

<!-- #docregion i18n -->
<script>
// Get the locale id somehow
document.locale = 'fr';

// Map to the text plugin
System.config({
map: {
text: 'systemjs-text-plugin.js'
}
});

// Launch the app
System.import('app').catch(function(err){ console.error(err); });
</script>
<!-- #enddocregion i18n -->
</head>

<body>
<my-app>Loading...</my-app>
</body>
</html>
17 changes: 17 additions & 0 deletions public/docs/_examples/cb-i18n/ts/plnkr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"description": "i18n",
"files": [
"app/**/*.css",
"app/**/*.html",
"app/**/*.ts",
"i18n/messages.xlf",
"i18n/fr/messages.fr.xlf",

"!**/*.[1].*",

"styles.css",
"systemjs-text-plugin.js",
"index.html"
],
"tags": ["i18n"]
}
14 changes: 14 additions & 0 deletions public/docs/_examples/cb-i18n/ts/systemjs-text-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// #docregion
/*
SystemJS Text plugin from
https://github.com/systemjs/plugin-text/blob/master/text.js
*/
exports.translate = function(load) {
if (this.builder && this.transpiler) {
load.metadata.format = 'esm';
return 'exp' + 'ort var __useDefault = true; exp' + 'ort default ' + JSON.stringify(load.source) + ';';
}

load.metadata.format = 'amd';
return 'def' + 'ine(function() {\nreturn ' + JSON.stringify(load.source) + ';\n});';
}
3 changes: 2 additions & 1 deletion public/docs/_examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"test:webpack": "karma start karma.webpack.conf.js",
"build:webpack": "rimraf dist && webpack --config config/webpack.prod.js --bail",
"build:cli": "ng build",
"build:aot": "ngc -p tsconfig-aot.json && rollup -c rollup.js"
"build:aot": "ngc -p tsconfig-aot.json && rollup -c rollup.js",
"extract": "ng-xi18n"
},
"keywords": [],
"author": "",
Expand Down
6 changes: 6 additions & 0 deletions public/docs/dart/latest/cookbook/_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
"hide": true
},

"i18n": {
"title": "Internationalization (i18n)",
"intro": "Translate the app's template text into multiple languages",
"hide": true
},

"rc4-to-rc5": {
"title": "RC4 to RC5 Migration",
"intro": "Migrate your RC4 app to RC5 in minutes.",
Expand Down
1 change: 1 addition & 0 deletions public/docs/dart/latest/cookbook/i18n.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../../_includes/_ts-temp
1 change: 1 addition & 0 deletions public/docs/dart/latest/guide/i18n.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!= partial("../../../_includes/_ts-temp")
6 changes: 6 additions & 0 deletions public/docs/js/latest/cookbook/_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
"intro": "Validate user's form entries"
},

"i18n": {
"title": "Internationalization (i18n)",
"intro": "Translate the app's template text into multiple languages",
"hide": true
},

"rc4-to-rc5": {
"title": "RC4 to RC5 Migration",
"intro": "Migrate your RC4 app to RC5 in minutes.",
Expand Down
1 change: 1 addition & 0 deletions public/docs/js/latest/cookbook/i18n.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../../_includes/_ts-temp
1 change: 1 addition & 0 deletions public/docs/js/latest/guide/i18n.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!= partial("../../../_includes/_ts-temp")
5 changes: 5 additions & 0 deletions public/docs/ts/latest/cookbook/_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
"intro": "Validate user's form entries"
},

"i18n": {
"title": "Internationalization (i18n)",
"intro": "Translate the app's template text into multiple languages"
},

"rc4-to-rc5": {
"title": "RC4 to RC5 Migration",
"intro": "Migrate your RC4 app to RC5 in minutes."
Expand Down
Loading