詳解使用angular-cli發(fā)布i18n多國(guó)語(yǔ)言Angular應(yīng)用
在模板html標(biāo)簽中增加i18n
<h1 i18n>Hello world!</h1>
使用ng命令產(chǎn)生xlf格式的message.xlf文件
$ ng xi18n --output-path src/i18n
命令執(zhí)行后,生成 src/i18n/messages.xlf 文件
<?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="5816217f424111ae4c91dd72ee1db0ae252763b5" datatype="html"> <source>Hello World!</source> <target/> </trans-unit> </body> </file> </xliff>
復(fù)制message.xlf,message.en.xlf(英文版本) message.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="en" datatype="plaintext" original="ng2.template"> <body> <trans-unit id="5816217f424111ae4c91dd72ee1db0ae252763b5" datatype="html"> <source>Hello World!</source> <target>Hello World!</target> </trans-unit> </body> </file> </xliff>
<?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="5816217f424111ae4c91dd72ee1db0ae252763b5" datatype="html"> <source>Hello World!</source> <target>哈嘍,世界!</target> </trans-unit> </body> </file> </xliff>
$ ng serve --aot \ --i18n-file=src/i18n/messages.zh.xlf \ --locale=zh \ --i18n-format=xlf
現(xiàn)在瀏覽,顯示的是中文版本
$ for lang in en zh; do \ ng build --output-path=dist/$lang \ --aot \ -prod \ --bh /$lang/ \ --i18n-file=src/i18n/messages.$lang.xlf \ --i18n-format=xlf \ --locale=$lang; \ done
這個(gè)命令執(zhí)行完畢后,生成了en和zh兩種語(yǔ)言版本。http://localhost:4200/en訪(fǎng)問(wèn)英文版本,http://localhost:4200/zh訪(fǎng)問(wèn)中文版本。--bh指定默認(rèn)版本,http://localhost:4200訪(fǎng)問(wèn)時(shí),跳轉(zhuǎn)到默認(rèn)版本。
修改package.json文件,加入腳本
{ [...] "scripts": { [...] "build-i18n": "for lang in en zh; do ng build --output-path=dist/$lang --aot -prod --bh /$lang/ --i18n-file=src/i18n/messages.$lang.xlf --i18n-format=xlf --locale=$lang; done" } [...] }
這樣就可以執(zhí)行npm run build-i18n 命令,一次build多個(gè)語(yǔ)言版本了。
windows用戶(hù)命令
> ng build --output-path=dist/zh --aot -prod --bh /zh/ --i18n-file=src/i18n/messages.zh.xlf --i18n-format=xlf --locale=zh > ng build --output-path=dist/en --aot -prod --bh /en/ --i18n-file=src/i18n/messages.en.xlf --i18n-format=xlf --locale=en
package.json腳本
"scripts": { "build-i18n:es": "ng build --output-path=dist/zh --aot -prod --bh /zh/ --i18n-file=src/i18n/messages.zh.xlf --i18n-format=xlf --locale=zh", "build-i18n:en": "ng build --output-path=dist/en --aot -prod --bh /en/ --i18n-file=src/i18n/messages.en.xlf --i18n-format=xlf --locale=en", "build-i18n": "npm run build-i18n:en ; npm run build-i18n:zh" }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解AngularJS通過(guò)ocLazyLoad實(shí)現(xiàn)動(dòng)態(tài)(懶)加載模塊和依賴(lài)
本篇文章主要介紹了詳解AngularJS通過(guò)ocLazyLoad實(shí)現(xiàn)動(dòng)態(tài)(懶)加載模塊和依賴(lài) ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03AngularJS模態(tài)框模板ngDialog的使用詳解
這篇文章主要介紹了AngularJS模態(tài)框模板ngDialog的使用詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05AngularJs 動(dòng)態(tài)加載模塊和依賴(lài)
這篇文章主要介紹了AngularJs 動(dòng)態(tài)加載模塊和依賴(lài)方法的相關(guān)資料,需要的朋友可以參考下2016-09-09對(duì)angularJs中controller控制器scope父子集作用域的實(shí)例講解
今天小編就為大家分享一篇對(duì)angularJs中controller控制器scope父子集作用域的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10淺談Angular HttpClient簡(jiǎn)單入門(mén)
本篇文章主要介紹了淺談Angular HttpClient 簡(jiǎn)單入門(mén),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05angular6根據(jù)environments配置文件更改開(kāi)發(fā)所需要的環(huán)境的方法
這篇文章主要介紹了angular6根據(jù)environments配置文件更改開(kāi)發(fā)所需要的環(huán)境的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03AngularJS下對(duì)數(shù)組的對(duì)比分析
下面小編就為大家?guī)?lái)一篇AngularJS下對(duì)數(shù)組的對(duì)比分析。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08