詳解使用angular-cli發(fā)布i18n多國語言Angular應用
在模板html標簽中增加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>
復制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
這個命令執(zhí)行完畢后,生成了en和zh兩種語言版本。http://localhost:4200/en訪問英文版本,http://localhost:4200/zh訪問中文版本。--bh指定默認版本,http://localhost:4200訪問時,跳轉(zhuǎ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多個語言版本了。
windows用戶命令
> 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"
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
將angular.js項目整合到.net mvc中的方法詳解
這篇文章主要給大家介紹了將angular.js項目整合到.net mvc中的相關(guān)資料,文中通過示例代碼將實現(xiàn)的過程介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面跟著小編來一起看看吧。2017-06-06
Angular ng-animate和ng-cookies用法詳解
本文講一下Angular中動畫應用的部分。這篇文章主要介紹了Angular ng-animate和ng-cookies用法詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
深入學習JavaScript的AngularJS框架中指令的使用方法
這篇文章主要介紹了深入學習JavaScript的AngularJS框架中指令的使用方法,指令的使用是Angular入門學習中的基礎知識,需要的朋友可以參考下2016-03-03
ionic3實戰(zhàn)教程之隨機布局瀑布流的實現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于ionic3實戰(zhàn)教程之隨機布局瀑布流的實現(xiàn)方法,文中通過示例代碼和圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2017-12-12

