{"id":3695,"date":"2020-05-05T22:39:28","date_gmt":"2020-05-05T14:39:28","guid":{"rendered":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/?p=3695"},"modified":"2020-05-05T22:39:30","modified_gmt":"2020-05-05T14:39:30","slug":"angular-i18n","status":"publish","type":"post","link":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/2020\/05\/angular-i18n\/","title":{"rendered":"Angular i18n"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Angular \u5b98\u65b9\u63d0\u4f9b\u7684\u5957\u4ef6\u4ee5\u53ca\u6559\u5b78\uff1a<a href=\"https:\/\/angular.io\/guide\/i18n\">Angular Internalization (i18n)<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6587\u7ae0\u88e1\u4e00\u5927\u5806\uff0c\u7c21\u55ae\u6574\u7406\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u5b89\u88dd\uff1ang add @angular\/localize<ul><li>\u9019\u500b\u6b65\u9a5f\u6703\u5e6b\u4f60\u5728 polyfills.ts \u88e1\u52a0\u5165\u5fc5\u8981\u7684 import<\/li><\/ul><\/li><li>\u4f7f\u7528\uff0c\u9019\u90e8\u4efd\u5206\u70ba HTML \u8ddf\u7a0b\u5f0f<ul><li>HTML\uff0c\u5728\u9700\u8981\u591a\u570b\u8a9e\u8a00\u7684\u6a19\u7c64\u52a0\u5165 i18n \u7684\u5c6c\u6027<ul><li>&lt;span i18n>Hello world&lt;\/span><\/li><li>&lt;span i18n=\u201c@@span_hello\u201d>Hello world&lt;\/span> \uff0c\u7528 i18n=\u201c@@span_hello\u201d \u7684\u597d\u8655\u662f\uff0c\u7522\u51fa\u7684\u7ffb\u8b6f\u6a94\u88e1\u4e0d\u6703\u662f\u4e00\u500b\u96a8\u610f\u7684\u6578\u5b57\uff0c\u800c\u662f\u4e00\u500b\u5c0d\u958b\u767c\u8005\u4f86\u8aaa\u6bd4\u8f03\u660e\u78ba\u7684\u540d\u7a31\u3002<\/li><li>&lt;input i18n-placeholder> \u9019\u500b\u662f\u6709\u4e9b\u5c6c\u6027\u672c\u8eab\u9700\u8981\u591a\u570b\u8a9e\u8a00\u7684\uff0c\u5c31\u5728\u524d\u9762\u52a0\u4e0a &#8220;i18n-&#8220;<\/li><\/ul><\/li><li>\u7a0b\u5f0f\uff0c\u5b57\u4e32\u524d\u65b9\u52a0\u4e0a $localize \uff0c\u4e26\u4e14\u6539\u7528 back quote\uff0c\u4f8b\u5982\uff1a$localize`hello world`<\/li><\/ul><\/li><li>\u8403\u53d6\uff0c\u7528 ng xi18n &#8211;output-path src\/i18n \u5c31\u53ef\u4ee5\u628a HTML \u88e1\u6709\u6a19 i18n \u7684\u5b57\u4e32\u8403\u53d6\u5230 src\/i18n\/messages.xlf \u88e1<\/li><li>\u7ffb\u8b6f\uff0c\u5148\u628a\u4e0a\u500b\u6b65\u9a5f\u53d6\u5f97\u7684 messages.xlf \u8907\u88fd\u70ba messages.zh_Hant.xlf \uff0c\u518d\u53bb\u7de8\u8f2f\u3002\u9019\u908a\u63d0\u4f9b\u4e00\u500b\u7c21\u6613\u7684\u7db2\u9801\u5de5\u5177 &#8211; <a href=\"https:\/\/martinroob.github.io\/tiny-translator\">tiny-translator<\/a>\uff0c\u5728\u8655\u7406\u4e0a\u6703\u65b9\u4fbf\u5f88\u591a\u3002\u958b\u555f\u4ee5\u5f8c\uff0c\u8981\u5148\u5efa\u7acb\u5c08\u6848\uff0c\u7136\u5f8c\u4e0a\u50b3 .xlf \u6a94\u6848\uff0c\u63a5\u8457\u5c31\u53ef\u4ee5\u9032\u884c\u7ffb\u8b6f\u4e86\u3002\u7ffb\u8b6f\u597d\uff0c\u518d\u4e0b\u8f09\u4e0b\u4f86\u5373\u53ef\u3002<\/li><li>\u5408\u4f75\uff0c\u7a0b\u5f0f\u958b\u767c\u4e2d\u96e3\u514d\u6703\u6709\u589e\u522a\uff0c\u6bcf\u6b21\u7528 xi18n \u57fa\u672c\u4e0a\u90fd\u662f\u91cd\u65b0\u8403\u53d6\u4e00\u6b21\uff0c\u7b49\u65bc\u662f\u53c8\u8981\u518d\u641e\u4e00\u6b21\u5408\u4f75\u7684\u529f\u592b\uff0c\u9019\u592a\u7d2f\u3002<a href=\"https:\/\/martinroob.github.io\/tiny-translator\">tiny-translator<\/a>\u00a0\u7684\u4f5c\u8005\u6709\u63d0\u4f9b\u53e6\u5916\u4e00\u500b\u5de5\u5177 &#8211; <a href=\"https:\/\/github.com\/martinroob\/ngx-i18nsupport\/tree\/master\/projects\/xliffmerge\">xliffmerge<\/a>\u00a0<ul><li>\u5b89\u88dd\uff1anpm install -g ngx-i18nsupport<\/li><li>\u5728 package.json \u7684 scripts \u88e1\u52a0\u5165\u00a0&#8220;extract-i18n&#8221;: &#8220;ng xi18n &#8211;output-path src\/i18n &amp;&amp; xliffmerge &#8211;profile xliffmerge.json en de\u201d \uff0c\u88e1\u9762\u7684 en, de \u7b49 locale \u8acb\u4f9d\u7167\u81ea\u5df1\u7684\u9700\u6c42\u4f5c\u8abf\u6574<\/li><li>\u65b0\u589e xliffmerge.json \uff0c\u9019\u500b\u6a94\u6848\u8acb\u53c3\u8003\u5f8c\u9762\u3002<\/li><li>\u4f7f\u7528 npm run extract-i18n \u5c31\u53ef\u4ee5\u81ea\u52d5\u8403\u53d6\u5b57\u4e32\u4e26\u4e14\u4f5c\u5408\u4f75\u4e86\u3002<\/li><\/ul><\/li><li>\u5c08\u6848\u7684\u5efa\u7f6e\uff0c\u4e3b\u8981\u662f\u4fee\u6539 angular.json\uff0c\u6709\u4e09\u500b\u90e8\u5206\uff1a<ul><li>projects \/ your_project_name \u52a0\u5165 \u201ci18n\u201d: {\u201csourceLocale\u201d: \u201cen\u201d, \u201clocales\u201d: {\u201cde\u201d: \u201csrc\/i18n\/messages.de.xlf\u201d}}<\/li><li>projects \/ your_project_name \/ architect \/ build \/ configurations \u88e1\u52a0\u5165 \u201cde\u201d: {\u201clocalize\u201d: [\u201cde\u201d]}<\/li><li>projects \/ your_project_name \/ architect \/ serve \/ configurations \u88e1\u52a0\u5165 \u201cde\u201d: {\u201cbrowserTarget\u201d: \u201cng-hosting:build:de\u201d}<\/li><\/ul><\/li><li>\u8981\u57f7\u884c ng build \/ ng serve \u6642\uff0c\u5c31\u53ef\u4ee5\u7528<ul><li>ng build &#8211;configuration=production,de<\/li><li>ng serve &#8211;configuration=de<\/li><\/ul><\/li><li>\u88dc\u5145\u4e00\u500b\u6211\u89ba\u5f97\u5f88\u91cd\u8981\u7684\u90e8\u5206\uff0c\u5c31\u662f\u4e00\u500b\u8a9e\u8a00\u8981\u5efa\u7f6e\u4e00\u6b21\uff0c\u6240\u4ee5\u4e00\u822c\u7684\u4f48\u7f72\u6703\u662f\u9019\u6a23\u7684\uff0c\u5efa\u7f6e\u597d zh \uff0c\u653e\u5728 zh\/ \u76ee\u9304\u4e0b\uff0c\u5efa\u7f6e\u597d de\uff0c\u653e\u5728 de\/ \u76ee\u9304\u4e0b\uff0c\u7136\u5f8c\u5728 nginx\/apache \u7684\u8a2d\u5b9a\u88e1\u53bb\u4f9d\u7167 header \u7684 language \u53bb\u5c0e\u5411\u5230\u5c0d\u61c9\u7684\u76ee\u9304\u53bb\u3002\u9019\u7bc7 <a href=\"https:\/\/dev.to\/angular\/deploying-an-i18n-angular-app-with-angular-cli-2fb9\">Deploying an i18n Angular app with angular-cli<\/a> \u7684\u5f8c\u9762\u6709\u6559\u600e\u9ebc\u53bb\u8a2d\u5b9a apache \/ nginx\u3002<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u770b\u5230\u9019\u908a\uff0c\u4f60\u53ef\u80fd\u6703\u60f3\uff0c\u90a3\u7a0b\u5f0f\u88e1\u6a19\u4e0a $localize \u7684\u5b57\u4e32\u5462\uff1f\u55ef\uff0cng xi18n \u4e26\u4e0d\u6703\u628a\u9019\u4e9b\u5b57\u4e32\u8403\u53d6\u51fa\u4f86 (<a href=\"https:\/\/github.com\/angular\/angular\/issues\/35912\">issue<\/a>)\uff0c\u6240\u4ee5\u9019\u90e8\u4efd\u5f97\u81ea\u5df1\u624b\u52d5\u8655\u7406 \ud83d\ude23\u00a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">P.S. \u7528 <a href=\"https:\/\/github.com\/martinroob\/ngx-i18nsupport\/tree\/master\/projects\/tooling\">ngx-i18nsupport \u7684 tooling<\/a> \u53ef\u4ee5\u628a\u4e0a\u9762\u8b1b\u7684\u7c21\u5316\u6389\uff0c\u50cf\u662f\u52a0\u5165 npm package\u3001\u5728 package.json \u52a0\u5165 extract-i18n \u3001\u5728 angular.json \u52a0\u5165\u8a2d\u5b9a\u7b49\u7b49\uff0c\u4e00\u6b21\u5c31\u641e\u5b9a\u4e86\uff0c\u6211\u662f\u5df2\u7d93\u7528\u4e86\u624d\u770b\u5230\u9019\u500b tooling \uff0c\u6709\u9ede\u76f8\u898b\u6068\u665a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ xliffmerge.json\n{\n  \"xliffmergeOptions\": {\n    \"srcDir\": \"src\/i18n\",\n    \"genDir\": \"src\/i18n\",\n    \"i18nFile\": \"messages.xlf\",\n    \"i18nBaseFile\": \"messages\",\n    \"i18nFormat\": \"xlf\",\n    \"encoding\": \"UTF-8\",\n    \"defaultLanguage\": \"en\",\n    \"languages\": [\"en\", \"de\"],\n    \"removeUnusedIds\": true,\n    \"supportNgxTranslate\": false,\n    \"ngxTranslateExtractionPattern\": \"@@|ngx-translate\",\n    \"useSourceAsTarget\": true,\n    \"targetPraefix\": \"\",\n    \"targetSuffix\": \"\",\n    \"beautifyOutput\": false,\n    \"preserveOrder\": true,\n    \"allowIdChange\": false,\n    \"autotranslate\": false,\n    \"apikey\": \"\",\n    \"apikeyfile\": \"\",\n    \"verbose\": false,\n    \"quiet\": false\n  }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Angular \u5b98\u65b9\u63d0\u4f9b\u7684\u5957\u4ef6\u4ee5\u53ca\u6559\u5b78\uff1aAngular Internalization (i18n) \u6587\u7ae0\u88e1\u4e00\u5927\u5806\uff0c\u7c21\u55ae\u6574\u7406\u5982\u4e0b\uff1a \u5b89\u88dd\uff1ang add @angular\/localize \u9019\u500b\u6b65\u9a5f\u6703\u5e6b\u4f60\u5728 polyfills.ts \u88e1\u52a0\u5165\u5fc5\u8981\u7684 import \u4f7f\u7528\uff0c\u9019\u90e8\u4efd\u5206\u70ba HTML \u8ddf\u7a0b\u5f0f HTML\uff0c\u5728\u9700\u8981\u591a\u570b\u8a9e\u8a00\u7684\u6a19\u7c64\u52a0\u5165 i18n \u7684\u5c6c\u6027 &lt;span i18n>Hello world&lt;\/span> &lt;span i18n=\u201c@@span_hello\u201d>Hello world&lt;\/span> \uff0c\u7528 i18n=\u201c@@span_hello\u201d \u7684\u597d\u8655\u662f\uff0c\u7522\u51fa\u7684\u7ffb\u8b6f\u6a94\u88e1\u4e0d\u6703\u662f\u4e00\u500b\u96a8\u610f\u7684\u6578\u5b57\uff0c\u800c\u662f\u4e00\u500b\u5c0d\u958b\u767c\u8005\u4f86\u8aaa\u6bd4\u8f03\u660e\u78ba\u7684\u540d\u7a31\u3002 &lt;input i18n-placeholder> \u9019\u500b\u662f\u6709\u4e9b\u5c6c\u6027\u672c\u8eab\u9700\u8981\u591a\u570b\u8a9e\u8a00\u7684\uff0c\u5c31\u5728\u524d\u9762\u52a0\u4e0a &#8220;i18n-&#8220; \u7a0b\u5f0f\uff0c\u5b57\u4e32\u524d\u65b9\u52a0\u4e0a $localize \uff0c\u4e26\u4e14\u6539\u7528 back quote\uff0c\u4f8b\u5982\uff1a$localize`hello world` \u8403\u53d6\uff0c\u7528 ng xi18n &#8211;output-path src\/i18n \u5c31\u53ef\u4ee5\u628a HTML \u88e1\u6709\u6a19 i18n \u7684\u5b57\u4e32\u8403\u53d6\u5230 src\/i18n\/messages.xlf \u88e1 \u7ffb\u8b6f\uff0c\u5148\u628a\u4e0a\u500b\u6b65\u9a5f\u53d6\u5f97\u7684 messages.xlf \u8907\u88fd\u70ba messages.zh_Hant.xlf \uff0c\u518d\u53bb\u7de8\u8f2f\u3002\u9019\u908a\u63d0\u4f9b\u4e00\u500b\u7c21\u6613\u7684\u7db2\u9801\u5de5\u5177 &#8211; &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/2020\/05\/angular-i18n\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Angular i18n&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[5],"tags":[167],"class_list":["post-3695","post","type-post","status-publish","format-standard","hentry","category-idea","tag-angular"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2MOxp-XB","_links":{"self":[{"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/posts\/3695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/comments?post=3695"}],"version-history":[{"count":1,"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/posts\/3695\/revisions"}],"predecessor-version":[{"id":3696,"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/posts\/3695\/revisions\/3696"}],"wp:attachment":[{"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/media?parent=3695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/categories?post=3695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ellery.no-ip.info\/wp\/thinkingmore\/wp-json\/wp\/v2\/tags?post=3695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}