填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫Home

遷移舊版本地化 ID

Migrating legacy localization IDs

為什麼需要進行遷移

Why this migration is necessary

Angular 的翻譯體系會根據訊息 ID 與另一種語言的譯後訊息進行匹配。建立這些 ID 的方法之一是讓翻譯體系為你產生它們。以前,這些 ID 的格式在發生細微更改時(例如,加減空格)是不穩定的。而新格式會更加穩健。

The Angular translation system works by matching a message ID to a translated message in a different language. One option for creating these IDs is to have the translation system generate them for you. Previously, the format for these IDs are not stable when there are insignificant changes, such as to whitespace. The new format is much more robust.

本主題描述瞭如何遷移舊的本地化 ID,來幫你提前適應完全停止支援舊格式的那一刻。

This topic describes how to migrate old localization IDs to help you future-proof your application in the event that the old format is removed.

此遷移的作用

What this migration does

Angular 11 版引入了一種用於產生本地化 ID 的新格式。這些新的 ID 比以前的舊格式更健壯。但是,在版本 11 之前建立的應用程式仍然將舊格式用作其 ID。

Angular version 11 introduced a new format for generating localization IDs. These new IDs are more robust than the previous legacy format. However, applications created before version 11 still used the legacy format for their IDs.

你無需將自訂本地化 ID 遷移到新 ID。

You do not need to migrate custom localization IDs to new IDs.

隨著 Angular 版本 12 的發佈,你現在可以使用工具將任何舊版的本地化 ID 遷移到基於最新演算法的 ID。

With the release of Angular version 12, you now have how tools available to help you migrate any legacy localization IDs to IDs that use the latest algorithms.

你有兩個用於遷移舊版 ID 的選項。第一種方法使用 Angular CLI 在你的應用程式中定位舊版 ID。第二種方法使用獨立指令碼 localize-extract 來定位舊版 ID。

You have two options for migrating legacy IDs. The first method uses the Angular CLI to locate legacy IDs in your application. The second method uses a standalone script, localize-extract, to locate the legacy IDs.

使用 CLI 遷移舊版 ID

Migrating legacy IDs using the CLI

要使用 CLI 遷移舊版本地化 ID,請執行以下操作:

To migrate legacy localization IDs using the CLI:

  1. 執行 ng extract-i18n 命令。

    Run the ng extract-i18n command.

          
          ng extract-i18n --format=legacy-migrate
        

    執行此命令後,你將擁有一個遷移檔案 messages.json,其中包含舊版本地化 ID 與新版 ID 之間的對映,你可以用它來更新應用程式。

    After running this command, you have a migration file, messages.json, containing a mapping between legacy localization IDs and new IDs that you can use to update your application.

  2. npx localize-migrate 命令更新應用程式中的 ID。

    Update the IDs in your application using the npx localize-migrate command.

          
          npx localize-migrate --files=*.xlf --mapFile=messages.json
        

    你還可以在 files 引數中指定其它格式,比如 *.xmb

    You can also specify other formats in the files parameter, such as *.xmb.

  3. 將更新後的檔案提交到你的原始碼管理系統。

    Commit the updated files to your source control system.

完成遷移後,請將 Angular Compiler 選項 enableI18nLegacyMessageIdFormat 設定為 false。有關此選項的更多資訊,請參見Angular 編譯器選項

After you complete the migration, set the Angular Compiler option, enableI18nLegacyMessageIdFormat, to false. For more information about this option, see Angular Compiler Options.

localize-extract 遷移舊版 ID

Migrate legacy IDs using localize-extract

如果你未使用 Angular CLI,則可以使用 localize-extract 來遷移舊版本地化 ID:

If you are not using the Angular CLI, you can migrate legacy localization IDs using localize-extract:

  1. 執行 npx localize-extract 命令。

    Run the npx localize-extract command.

          
          npx localize-extract --format=legacy-migrate --source=./path/to/bundles/**/*.js --outputPath=./messages.json
        

    在此命令中,./path/to/bundles/ 表示你的可分發檔案的路徑。你可以將 outputPath 引數設定為系統中的任何目錄。

    In this command, ./path/to/bundles/ represents the path to your distributable files. You can set the outputPath parameter to any directory in your system.

    一定不能翻譯這些可分發檔案(例如用 localize-translate),因為這樣做會剝離帶 $localize 標記的字串。

    Your distributable files must not have been translated, such as by using localize-translate, as doing so strips the $localize tagged strings that the extractor requires.

    執行此命令後,你將擁有一個遷移檔案 messages.json,其中包含舊版本地化 ID 與新版 ID 之間的對映,可用於更新應用程式。

    After running this command, you have a migration file, messages.json, containing a mapping between legacy localization IDs and new IDs that you can use to update your application.

  2. npx localize-migrate 命令更新應用程式中的 ID。

    Update the IDs in your application using the npx localize-migrate command.

          
          npx localize-migrate --files=*.xlf --mapFile=messages.json
        

    你還可以在 files 引數中指定其它格式,例如 *.xmb

    You can also specify other formats in the files parameter, such as *.xmb.

完成遷移後,將 Angular Compiler 選項 enableI18nLegacyMessageIdFormat 設定為 false。有關此選項的更多資訊,請參見Angular 編譯器選項

After you complete the migration, set the Angular Compiler option, enableI18nLegacyMessageIdFormat, to false. For more information about this option, see Angular Compiler Options.