This repository was archived by the owner on Dec 4, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 875
docs(i18n): add internationalization (i18n) guide #2491
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'); | ||
}); | ||
|
||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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--> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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--> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { } | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') { | ||
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
public/docs/_examples/cb-i18n/ts/i18n/fr/messages.fr.xlf.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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});'; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include ../../../_includes/_ts-temp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
!= partial("../../../_includes/_ts-temp") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include ../../../_includes/_ts-temp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
!= partial("../../../_includes/_ts-temp") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
en-US