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

[BACKUP] new I18n guide #2340

Closed
wants to merge 22 commits into from
Closed

Conversation

Foxandxss
Copy link
Member

@Foxandxss Foxandxss commented Sep 14, 2016

This supersedes #2309

Now superseded by #2491. Close it when that PR is merged.

@googlebot
Copy link

We found a Contributor License Agreement for you (the sender of this pull request) and all commit authors, but as best as we can tell these commits were authored by someone else. If that's the case, please add them to this pull request and have them confirm that they're okay with these commits being contributed to Google. If we're mistaken and you did author these commits, just reply here to confirm.

1 similar comment
@googlebot
Copy link

We found a Contributor License Agreement for you (the sender of this pull request) and all commit authors, but as best as we can tell these commits were authored by someone else. If that's the case, please add them to this pull request and have them confirm that they're okay with these commits being contributed to Google. If we're mistaken and you did author these commits, just reply here to confirm.

include ../_util-fns

:marked
With internationalization, also known as i18n, we can display our website in multiple languages. There are two different
Copy link
Contributor

Choose a reason for hiding this comment

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

what about wrapping lines ?

don't you have a max line length on this repo.

Copy link
Member Author

Choose a reason for hiding this comment

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

There is nothing written in stone. At the end the lines gets wrapped in the HTML

Copy link
Contributor

Choose a reason for hiding this comment

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

not in gh which is annoying for the review

@ocombe
Copy link

ocombe commented Sep 14, 2016

@googlebot I confirm (is this enough?)

@vicb
Copy link
Contributor

vicb commented Sep 14, 2016

@ocombe no, it will stay red forever when the PR author is not the same as the commit author

* No information/state is (typically) lost. The user is presumably changing the language because he could not understand the earlier language.
This means that he doesn't have unsaved information in the application.
* Tools support: the strings to translate are easily extracted from our templates.
The resulting translations are in a generic format that can be consumed both by Angular 2 and one of the many translation softwares available.
Copy link

Choose a reason for hiding this comment

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

we should replace all "Angular 2" references with "Angular"

Copy link
Contributor

Choose a reason for hiding this comment

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

Done

export class AppComponent {
messages: string[];
messageMapping: {[k:string]: string} = {
'=0': 'No messages.',
Copy link
Contributor

Choose a reason for hiding this comment

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

better use one and other which is the standard for en

Copy link
Contributor

Choose a reason for hiding this comment

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

=0 remains the same?

Copy link
Contributor

Choose a reason for hiding this comment

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

one and other are caterogies, =x is for discrete values, works with any x

Copy link
Contributor

Choose a reason for hiding this comment

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

Done

// #enddocregion bootstrap-i18n

// #docregion messages-ts
export const TRANSLATION = `<?xml version="1.0" encoding="UTF-8"?>
Copy link
Contributor

Choose a reason for hiding this comment

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

FYI this will be in CompilerConfig soon, I'll try to remember to cc the doc team on the PR

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you have an ETA on that?

Copy link
Contributor

Choose a reason for hiding this comment

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

started working on that. probably 2.1 ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Alright, I'll leave it as is for now then we'll update it.

Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

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

I think this is missing

  • translating attrs: i18n-<attr name>
  • translating sibling nodes
    • <!--i18n:m|d --> ... <!--/i18n --> or
    • <ng-container i18n>

:marked
With internationalization, also known as i18n, we can display our website in multiple languages. There are two different
approaches to internationalization: with AoT compilation which requires a build step and a full reload when the language changes,
or with JiT which doesn't require a full reload and uses bindings to determine when a translation is needed.
Copy link
Contributor

Choose a reason for hiding this comment

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

Angular only provides 1 approach for now, full reload

Copy link
Contributor

Choose a reason for hiding this comment

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

Removed all mentions of reloading since it's not really relevant.


[Angular and i18n](#angular2-i18n)

* [i18n directive](#i18n-directive)
Copy link
Contributor

Choose a reason for hiding this comment

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

directiveS

Copy link
Contributor

Choose a reason for hiding this comment

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

Fixed

:marked
### Just in time (JiT) approach

The JiT approach relies on providing the translations to our application and binding them to our templates using directives, pipes and interpolations.
Copy link
Contributor

Choose a reason for hiding this comment

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

pipes also work in AoT

translations in the application code at build time.
* Our application must track all the pieces of the UI that need to be updated when the locale changes.
In addition, if the new language strings are being loaded over the network, this could take time and the UI needs to indicate this in some way to the user.
* Allows one to support multiple languages in the same view.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think there is a confusion:
dynamic/static vs JiT/AoT

Angular always use the static approach for both AoT and JiT

Usage of pipes should be discouraged. They should only be used by the compiler to transform codes.

If you use the pipes you basically can not i18n your app because different langs use different plural category

Copy link
Contributor

Choose a reason for hiding this comment

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

What should be used instead of the i18nSelect and i18nPlural pipes to allow for full translations?

Copy link
Contributor

Choose a reason for hiding this comment

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

nothing currently exists

Copy link
Contributor

Choose a reason for hiding this comment

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

Removed mentions of dynamic language change.
Also removed the whole pipe sections, if it actively breaks language switching then we shouldn't really be describing it's use in this chapter.

* We have to reload the app to change the language.
* Extra server side support is needed: Since we generate different precompiled files for each language, the server must perform cookie/user agent analysis
to decide which localized version of the application code should be returned to the user. This also causes a cache miss.
* The server is now responsible for determining the default localized version to serve. (e.g. cookies / geo-ip / Accept-Language header, etc.)
Copy link
Contributor

Choose a reason for hiding this comment

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

same in JiT, could be overriden by the user

Copy link
Contributor

@filipesilva filipesilva Sep 26, 2016

Choose a reason for hiding this comment

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

Removed comparison of server being relevant in the decision process.


.alert.is-important
:marked
The ng-xi18n messages extractor doesn't support the plural pipe right now.
Copy link
Contributor

Choose a reason for hiding this comment

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

it will never

Copy link
Contributor

Choose a reason for hiding this comment

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

Fixed, but might required further edits to the flow of the chapter.

:marked
### i18n select pipe

Just like the plural pipe displays different translations based on a number, the select pipe is used to display different
Copy link
Contributor

Choose a reason for hiding this comment

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

do not use directly, not extracted


.alert.is-helpful
:marked
Using a versioning system such as `GIT` can help us find out easily what new translations have been generated by
Copy link
Contributor

Choose a reason for hiding this comment

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

git

Copy link
Contributor

Choose a reason for hiding this comment

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

Using git is a bad idea.

Translation tools to the rescue here

Copy link
Contributor

Choose a reason for hiding this comment

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

Removed advice to use git, replaced with specialized translation software. If you have an example of what could be used, I can add it in.


Now that we have a localized file, we can tell Angular to use it for all of our elements that have the `i18n` directive attribute.

Angular understands `xlf` and `xmb` formats, but we have to provide these message into our application.
Copy link
Contributor

Choose a reason for hiding this comment

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

xmb is the source file
xtb is the translated file

Copy link
Contributor

Choose a reason for hiding this comment

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

Fixed

:marked
We have to provide three values: `TRANSLATIONS`, `TRANSLATIONS_FORMAT` and `LOCALE_ID`:
* `TRANSLATIONS` is a string containing the content of our `messages` file for the chosen localization.
* `TRANSLATIONS_FORMAT` is either `xlf` or `xmb` depending on the format of our `messages` file.
Copy link
Contributor

Choose a reason for hiding this comment

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

xlf, xlif or xtb

Copy link
Contributor

Choose a reason for hiding this comment

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

Is xmb not supported anymore?

Copy link
Contributor

Choose a reason for hiding this comment

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

Nevermind, your commend above answered my question.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fixed

@filipesilva
Copy link
Contributor

Heya @vicb, I'm picking up this chapter for now to get it ready for our testing setup and some more edits. I'm not too familiar with the i18n functionality aside from the chapter itself, so that's my starting point.

@vicb
Copy link
Contributor

vicb commented Sep 24, 2016

No pb, added a bunch of comments ping me if you need more info.

We should make it clear that i18n is supported equally well for JiT and AoT.

@@ -129,7 +129,7 @@ a(id="i18n-plural-pipe")

.alert.is-important
:marked
The ng-xi18n messages extractor doesn't support the plural pipe right now.
The ng-xi18n messages extractor doesn't support the plural pipe.
Copy link
Contributor

Choose a reason for hiding this comment

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

If we want to say that we probably want to say that support for ICU messages in attributes will come soon.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you elaborate on what that means?

Copy link
Contributor

Choose a reason for hiding this comment

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

<div title="{count, plural, ...}"> will be transformed to <div title="count|i18nPlural(...)"> before compilation.

extraction:

  • {count, plural, ...} will be extracted by ng-xi18n,

merge:

  • ngc will merge the translated ICU message,
  • then ICU messages will be converted to pipes,
  • then the template is compiled.

.l-main-section
:marked
### i18n directive
### i18n directives
Copy link
Contributor

Choose a reason for hiding this comment

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

It's actually not directives (notice that later in the review) - may be "markers" ?

Copy link
Contributor

Choose a reason for hiding this comment

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

From a user perspective, under the context of the Angular docs, these look like directives. We should explain then that these are not directives if they truly aren't.

What is the difference between these and directives? Are they just meaningless attributes that are being used as a marker for ng-xi18n to pick up?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, does this i18n attribute have any meaning outside of Angular? e.g. is it a convention, or part of a spec.

Copy link
Contributor

Choose a reason for hiding this comment

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

they are just passive markers that are removed once the translations are merged

<div i18n>Hello</div> -> <div>Bonjour</div>

the name is only an ng convention

@filipesilva
Copy link
Contributor

filipesilva commented Sep 26, 2016

The guide is heavily revised, I know that @wardbell wants to review but I would also appreciate @vicb, @ocombe and @Foxandxss to have a look. We aim to merge soon though.

We plan to further enhance it post AC, but for now the main priority is to get something simple and practical that users can follow.

Notably, I removed the pipe sections as per @vicb comment:

Usage of pipes should be discouraged. They should only be used by the compiler to transform codes.
If you use the pipes you basically can not i18n your app because different langs use different plural category

I also removed sections that talked about dynamic language switching. Angular itself doesn't support it and we don't have a server setup example to show. Also, as per @vicb:

Angular always use the static approach for both AoT and JiT

However, as @ocombe noted, this is a very common request and we should incorporate it in the guide in some form when revising.

@vicb I did not make the ICU messages note you mentioned simply because I don't know what it is nor what to say about it.

@@ -0,0 +1,13 @@
/// <reference path='../_protractor/e2e.d.ts' />
'use strict';
describe('i18n E2E Tests', function () {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: function () {...} => () => {...}

Copy link
Contributor

Choose a reason for hiding this comment

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

Fixed

Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

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

Added some "brief" comments, ping me if you need more info

@@ -0,0 +1,13 @@
/// <reference path='../_protractor/e2e.d.ts' />
'use strict';
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this required in TS ?

Copy link
Contributor

Choose a reason for hiding this comment

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

In our case yes, because we there's a couple of es2015 things that node requires 'use strict'; to support.

@NgModule({
imports: [ BrowserModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: trailing coma

Copy link
Contributor

Choose a reason for hiding this comment

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

We don't enforce it in the examples.

@@ -0,0 +1,5 @@
// #docregion
export const TRANSLATION = `

Copy link
Contributor

Choose a reason for hiding this comment

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

explain what this is ?

Copy link
Contributor

Choose a reason for hiding this comment

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

A file we need for example purpose, since our docs examples come from real files. Later in the guide we insert the content of the the .xlf file.

@@ -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"
Copy link
Contributor

Choose a reason for hiding this comment

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

what is the added value of the alias ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Not having to do ./node_modules/.bin/ng-xi18n.


:marked
With internationalization, also known as i18n, we can display our website in multiple languages.
Angular provides tools to export translation files that translators and work on.
Copy link
Contributor

Choose a reason for hiding this comment

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

meaning

Copy link
Contributor

Choose a reason for hiding this comment

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

and -> can

```

This XML element represents the translation of our header tag.
You might have a different string in `id="af2ccf4b5dba59616e92cf1531505af02da8f6d2"`, this is normal.
Copy link
Contributor

Choose a reason for hiding this comment

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

The id depends on the content of the message and it's meaning

Copy link
Contributor

Choose a reason for hiding this comment

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

Added


Now that we have a localized file, we can tell Angular to use it for all of our elements that have the `i18n` marker.

Angular understands `xlf`,`xlif` and `xtb` formats, but we have to provide these message into our application.
Copy link
Contributor

Choose a reason for hiding this comment

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

missing ws after comma

Copy link
Contributor

Choose a reason for hiding this comment

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

messages

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm sorry, not sure what you mean. Can you say it another way?

Copy link
Contributor

Choose a reason for hiding this comment

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

missing white space after xlf,& message should be messages

Copy link
Contributor

Choose a reason for hiding this comment

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

Fixed

In both approaches the general idea is the same. we have to give angular these three things:
* Translations file
* Translation format
* Locale ID
Copy link
Contributor

Choose a reason for hiding this comment

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

format = langugage-region, ie en-US

Copy link
Contributor

Choose a reason for hiding this comment

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

Added

When we use JiT, we'll provide those three things when bootstrapping out `AppModule`.

We have to provide three values: `TRANSLATIONS`, `TRANSLATIONS_FORMAT` and `LOCALE_ID`.
* `TRANSLATIONS` is a string containing the content of our `messages` file for the chosen localization.
Copy link
Contributor

Choose a reason for hiding this comment

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

localization -> locale

Copy link
Contributor

Choose a reason for hiding this comment

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

Fixed


:marked
That's all that we have to do! The `ngc` compiler will replace the content of our elements that have
the i18n attribute with our translations in the AoT precompiled files.
Copy link
Contributor

Choose a reason for hiding this comment

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

precompiled files ->generated templates ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Replaced

@wardbell
Copy link
Contributor

Now superseded by #2491

@wardbell wardbell closed this Sep 27, 2016
@wardbell wardbell reopened this Sep 27, 2016
@wardbell wardbell changed the title I18n guide [BACKUP] new I18n guide Sep 27, 2016
@wardbell wardbell closed this Oct 5, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants