- Angular 教程
- Angular - 首頁
- Angular - 概述
- Angular - 功能
- Angular - 優勢與劣勢
- Angular 基礎
- Angular - 環境搭建
- Angular - 第一個應用
- Angular - MVC 架構
- Angular 元件
- Angular - 元件
- Angular - 元件生命週期
- Angular - 檢視封裝
- Angular - 元件互動
- Angular - 元件樣式
- Angular - 巢狀元件
- Angular - 內容投影
- Angular - 動態元件
- Angular - 元素
- Angular 模板
- Angular - 模板
- Angular - 文字插值
- Angular - 模板語句
- Angular - 模板中的變數
- Angular - SVG 作為模板
- Angular 資料繫結
- Angular - 資料繫結及其型別
- Angular - 資料繫結
- Angular - 事件繫結
- Angular - 屬性繫結
- Angular - 屬性繫結
- Angular - 類和樣式繫結
- Angular 指令
- Angular - 指令
- Angular - 內建指令
- Angular 管道
- Angular - 管道
- Angular - 使用管道轉換資料
- Angular 依賴注入
- Angular - 依賴注入
- Angular HTTP 客戶端程式設計
- Angular - 服務
- Angular - HTTP 客戶端
- Angular - 請求
- Angular - 響應
- Angular - GET 請求
- Angular - PUT 請求
- Angular - DELETE 請求
- Angular - JSON-P
- Angular - 使用 HTTP 進行 CRUD 操作
- Angular 路由
- Angular - 路由
- Angular - 導航
- Angular - Angular Material
- Angular 動畫
- Angular - 動畫
- Angular 表單
- Angular - 表單
- Angular - 表單驗證
- Angular Service Workers & PWA
- Angular - Service Workers & PWA
- Angular 測試
- Angular - 測試概述
- Angular NgModule
- Angular - 模組介紹
- Angular 高階
- Angular - 身份驗證與授權
- Angular 國際化
- Angular - 可訪問性
- Angular - Web Workers
- Angular - 伺服器端渲染
- Angular - Ivy 編譯器
- Angular - 使用 Bazel 構建
- Angular - 向後相容性
- Angular - 響應式程式設計
- Angular - 指令和元件之間的資料共享
- Angular 工具
- Angular - CLI
- Angular 其他
- Angular - 第三方控制元件
- Angular - 配置
- Angular - 資料顯示
- Angular - 裝飾器和元資料
- Angular - 基本示例
- Angular - 錯誤處理
- Angular - 測試和構建專案
- Angular - 生命週期鉤子
- Angular - 使用者輸入
- Angular - 新特性?
- Angular 有用資源
- Angular - 快速指南
- Angular - 有用資源
- Angular - 討論
Angular 國際化
國際化是任何現代 Web 應用必不可少的特性。國際化使應用程式能夠面向全球任何語言。本地化是國際化的一部分,它使應用程式能夠以目標本地語言呈現。Angular 提供了對國際化和本地化功能的全面支援。
讓我們學習如何用不同的語言建立一個簡單的“Hello World”應用程式。
使用以下命令建立一個新的 Angular 應用程式:
cd /go/to/workspace ng new i18n-sample
使用以下命令執行應用程式:
cd i18n-sample npm run start
按如下所示更改AppComponent的模板:
<h1>{{ title }}</h1>
<div>Hello</div>
<div>The Current time is {{ currentDate | date : 'medium' }}</div>
使用以下命令新增本地化模組:
ng add @angular/localize
重啟應用程式。
LOCALE_ID是 Angular 變數,用於引用當前區域設定。預設情況下,它設定為 en_US。讓我們在 AppModule 中的 provider 中使用它來更改區域設定。
import { BrowserModule } from '@angular/platform-browser';
import { LOCALE_ID, NgModule } from '@angular/core';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule
],
providers: [ { provide: LOCALE_ID, useValue: 'hi' } ],
bootstrap: [AppComponent]
})
export class AppModule { }
這裡:
- LOCALE_ID 從 @angular/core 匯入。
- 透過 provider 將 LOCALE_ID 設定為 hi,以便在應用程式的任何地方都可以使用 LOCALE_ID。
從 @angular/common/locales/hi 匯入區域設定資料,然後使用 registerLocaleData 方法註冊它,如下所示:
import { Component } from '@angular/core';
import { registerLocaleData } from '@angular/common';
import localeHi from '@angular/common/locales/hi';
registerLocaleData(localeHi);
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
title = 'Internationzation Sample';
}
建立一個區域性變數 CurrentDate 並使用 Date.now() 設定當前時間。
export class AppComponent {
title = 'Internationzation Sample';
currentDate: number = Date.now();
}
更改 AppComponent 的模板內容幷包含 currentDate,如下所示:
<h1>{{ title }}</h1>
<div>Hello</div>
<div>The Current time is {{ currentDate | date : 'medium' }}</div>
檢查結果,您將看到日期使用的是 hi 區域設定。
我們已將日期更改為當前區域設定。讓我們也更改其他內容。為此,請在相關標籤中包含i18n屬性,格式為title|description@@id。
<h1>{{ title }}</h1>
<h1 i18n="greeting|Greeting a person@@greeting">Hello</h1>
<div>
<span i18n="time|Specifiy the current time@@currentTime">
The Current time is {{ currentDate | date : 'medium' }}
</span>
</div>
這裡:
- hello 是簡單的翻譯格式,因為它包含要翻譯的完整文字。
- Time 稍微複雜一些,因為它也包含動態內容。文字的格式應遵循 ICU 訊息格式進行翻譯。
我們可以使用以下命令提取要翻譯的資料:
ng xi18n --output-path src/locale
命令生成包含以下內容的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="greeting" datatype="html">
<source>Hello</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/app.component.html</context>
<context context-type="linenumber">3</context>
</context-group>
<note priority="1" from="description">Greeting a person</note>
<note priority="1" from="meaning">greeting</note>
</trans-unit>
<trans-unit id="currentTime" datatype="html">
<source>
The Current time is <x id="INTERPOLATION" equiv-text="{{ currentDate | date : 'medium' }}"/>
</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/app.component.html</context>
<context context-type="linenumber">5</context>
</context-group>
<note priority="1" from="description">Specifiy the current time</note>
<note priority="1" from="meaning">time</note>
</trans-unit>
</body>
</file>
</xliff>
複製檔案並將其重新命名為messages.hi.xlf
使用 Unicode 文字編輯器開啟檔案。找到source標籤並使用target標籤複製它,然後將內容更改為hi區域設定。使用谷歌翻譯查詢匹配的文字。更改後的內容如下:
開啟angular.json並在build -> configuration下放置以下配置:
"hi": {
"aot": true,
"outputPath": "dist/hi/",
"i18nFile": "src/locale/messages.hi.xlf",
"i18nFormat": "xlf",
"i18nLocale": "hi",
"i18nMissingTranslation": "error",
"baseHref": "/hi/"
},
"en": {
"aot": true,
"outputPath": "dist/en/",
"i18nFile": "src/locale/messages.xlf",
"i18nFormat": "xlf",
"i18nLocale": "en",
"i18nMissingTranslation": "error",
"baseHref": "/en/"
}
這裡:
我們對hi和en區域設定使用了單獨的設定。
在serve -> configuration下設定以下內容。
"hi": {
"browserTarget": "i18n-sample:build:hi"
},
"en": {
"browserTarget": "i18n-sample:build:en"
}
我們添加了必要的配置。停止應用程式並執行以下命令:
npm run start -- --configuration=hi
這裡:
我們指定了必須使用 hi 配置。
導航到 https://:4200/hi,您將看到印地語本地化內容。
最後,我們建立了一個 Angular 本地化應用程式。