[angular] Implement simple angular internationalization (i18n)

Target

Implement simple angular internationalization. This blog implements Chinese and French versions.

GeneralHello i18n!变为Chinese version: 你好 i18n!OrLegal version: Bonjour l'i18n !.

Insert image description here

process

Create a project:

ng new i18nDemo

Open in the integrated terminal.

Add localization package:

ng add @angular/localize

Add formatting tags in html:

<h1 i18n>Hello i18n!</h1>

Now run it, the page is:

Insert image description here
Generate translation template language pack:

ng extract-i18n --output-path src/locale

Generated a folder:locale, with a file in itmessages.xlf

Will translate the content in source into target:

Insert image description here

We want to have two languages ​​here, French and Chinese. therefore:

Why Chinese is zh-CN and French is fr-FR, see here:https://angular.cn/guide/i18n-common-locale-id

Insert image description here
Insert image description here

<!-- messages.zh.xlf -->
<?xml version="1.0" encoding="UTF-8" ?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
  <file source-language="zh-CN" datatype="plaintext" original="ng2.template">
    <body>
      <trans-unit id="4150330030790364157" datatype="html">
        <source>Hello i18n!</source>
        <target>中文版:你好 i18n!</target>
        <context-group purpose="location">
          <context context-type="sourcefile">src/app/app.component.html</context>
          <context context-type="linenumber">1</context>
        </context-group>
      </trans-unit>
    </body>
  </file>
</xliff>
<!-- messages.fr.xlf -->
<?xml version="1.0" encoding="UTF-8" ?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
  <file source-language="fr-FR" datatype="plaintext" original="ng2.template">
    <body>
      <trans-unit id="4150330030790364157" datatype="html">
        <source>Hello i18n!</source>
        <target>法语版:Bonjour l'i18n !</target>
        <context-group purpose="location">
          <context context-type="sourcefile">src/app/app.component.html</context>
          <context context-type="linenumber">1</context>
        </context-group>
      </trans-unit>
    </body>
  </file>
</xliff>

Related documents:https://angular.cn/guide/i18n-common-translation-files

Next is configuration angular.json (according to the documentation).

Insert image description here

"i18n": {
    
    
    "sourceLocale": "en-US",
    "locales": {
    
    
        "fr": {
    
    
            "translation": "src/locale/messages.fr.xlf"
        },
        "zh": {
    
    
            "translation": "src/locale/messages.zh.xlf"
        }
    }
},

Insert image description here

"localize": true,

The configuration is complete. Next build from the command line:

ng build --localize

Then configure:

Insert image description here

"build"->"configurations"

"fr": {
    
    
     "localize": [
         "fr"
     ]
 },
 "zh": {
    
    
     "localize": [
         "zh"
     ]
 }

"serve"->"configurations"

"fr": {
    
    
   "browserTarget": "i18nDemo:build:development,fr"
},
"zh": {
    
    
   "browserTarget": "i18nDemo:build:development,zh"
}

run

French version:

ng serve --configuration=fr

Insert image description here
Chinese Version:

ng serve --configuration=zh

Insert image description here

Same as the previous xml file.

ps: The following production buildng build --configuration=production,fr reported an error. I don’t know what to do. I will add more later.

Internationalization in TS

Reference:
angular6 Using message prompt box toast_angular4 message prompt box-CSDN blog
ngx-toastr - npm (npmjs.com)
https://angular.cn/guide/i18n-common-prepare

grammar:

In component code, translation source text and metadata are surrounded by backtick (`) characters. Mark strings for translation in your code using $localize tagged message strings.

$localize `string_to_translate`;

Insert image description here

reference

Angular internationalization

Super detailed Angular internationalization solution - Nuggets (juejin.cn)

Angular uses built-in i18n internationalization to configure Angular internationalization_angular i18n-CSDN blog

angular internationalization (i18n) - Jianshu (jianshu.com)

Problems encountered when upgrading Angular8 to Angular13_package ‘@angular/core’ is not a dependency.-CSDN Blog

angular6 uses the message prompt box toast_angular4 message prompt box-CSDN Blog

ngx-toastr - npm (npmjs.com)

Experience: For newbies, it feels too abstract and difficult to read the official documents alone, so I combined it with other blogs to understand. Then I found that some configurations of other blogs had been abandoned, and then I went to the official documentation. In this process, I filtered out a lot of information that I couldn’t understand (…) and summarized a lot of high-quality blogs that I could understand (!). I slowly learned it! Thanks to everyone who blogged and saved my life. .

Guess you like

Origin blog.csdn.net/karshey/article/details/133774499