包裝

上一節產生的 JSON 檔案必須與 Fuchsia 程式組合在一起,才能在程式執行階段找到這個檔案。請參閱:為元件提供資料檔案

我們為封裝資源 (即本地化資產) 制定一些慣例。結構定義旨在擴充至其他資產類型,也能夠支援資產類型的「組合」,這在表示裝置和語言代碼之間較複雜的關係時 (有時需要用到希伯來圖示版本,用於 200dpi 顯示)。下列所有路徑都與套件的資料目錄相關,且可在運作中系統的 /pkg/data 中找到。

路徑 品名
assets/ 儲存所有資產。這與 meta/ 目錄包含套件資訊清單和其他中繼資料的方式類似。日後,這個目錄可能包含傳統索引。
assets/locales 專門儲存語言代碼的資料
assets/locales/fr-fr 儲存特定語言代碼的資料。語言代碼名稱是 BCP47 格式的個別目錄。每個程式都會為這個名為 program.json 的目錄提供單一 JSON 檔案,其中名稱的 program 部分是由作者選擇。在某些情況下,我們可能必須確保這裡檔案的套件和程式庫名稱並未相互衝突。此外,由於 Fuchsia 採用的包裝策略,為了方便更新,可能還需要支付多個較小的檔案儲存翻譯,而非大型檔案。

路徑結構的完整性,以及妥善包裝本地化資源,您仍需手動完成。

封裝本地化資源

封裝本地化資源的最佳說明就是範例。首先,讓我們從 BUILD.gn 檔案開始,之後再將重點放在特定區段。

# Copyright 2020 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//build/components.gni")

import("//build/intl/intl_strings.gni")


group("example") {
  testonly = true
  deps = [ ":pkg" ]
}


executable("src_intl_example_bin") {
  sources = [ "main.cc" ]
  output_name = "src_intl_example"

  deps = [
    "//sdk/lib/syslog/cpp",
    "//src/lib/intl/lookup/cpp:lib",
    "//third_party/googletest:gtest_prod",

    # This is the underlying generated FIDL code, must be included so that
    # its settings make it to the -I flag for compiler invocation.
    # Generate all needed intermediate resources.
    ":fuchsia.intl.l10n_hlcpp",
    ":l10n",
  ]

}


fuchsia_package_with_single_component("pkg") {
  package_name = "src-intl-example"
  component_name = package_name
  manifest = "meta/src-intl-example.cml"

  deps = [
    # The binary to bundle in the package
    ":src_intl_example_bin",
  ]

  deps += [
    # These declaration works, but need to be maintained manually.  Work is
    # underway to make the "package" target collect and apply the locale files
    # automatically.
    ":en-strings",
    ":es-strings",
    ":fr-strings",

    # Contains the necessary ICU data.
    "//src/intl:icudtl",
  ]

}

resource("en-strings") {
  data_deps = [ ":l10n" ]
  sources = [ "$target_gen_dir/en/l10n.json" ]
  outputs = [ "data/assets/locales/en/l10n.json" ]
}

resource("fr-strings") {
  data_deps = [ ":l10n" ]
  sources = [ "$target_gen_dir/fr/l10n.json" ]
  outputs = [ "data/assets/locales/fr/l10n.json" ]
}

resource("es-strings") {
  data_deps = [ ":l10n" ]
  sources = [ "$target_gen_dir/es/l10n.json" ]
  outputs = [ "data/assets/locales/es/l10n.json" ]
}


intl_strings("l10n") {
  source = "strings.xml"
  source_locale = "en"
  output_locales = [
    "en",
    "fr",
    "es",
  ]
  library = "fuchsia.intl.l10n"
}

產生本地化資源的建立規則

本地化資源是以檔案系統中的檔案為基礎。//src/lib/intl/example 的程式範例說明如何建構及部署包含本地化訊息的 Fuchsia 程式。

intl_strings("l10n") {
  source = "strings.xml"
  source_locale = "en"
  output_locales = [
    "en",
    "fr",
    "es",
  ]
  library = "fuchsia.intl.l10n"
}

建構規則 intl_strings 會指示建構系統處理包含字串的 XML 檔案。如要進一步瞭解翻譯工作流程,請參閱訊息翻譯一節。翻譯後的訊息可在這項規則的預設建構輸出目錄中取得。行 source = "strings.xml" 會指向包含訊息來源的檔案。由於這類訊息一律使用口說語言撰寫,而且沒有任何特定語言比其他語言更相關,因此您也必須讓系統知道預設 strings.xml 使用哪種語言。

如要將預設語言設為英文,請在 BUILD.gn 中加入 source_locale = "en"。輸出語言代碼宣告可讓您指定要產生的語言代碼資源確切清單。這是 output_locales 的用途。這類明確聲明的原因如下:

  • 您想要明確宣告在執行階段中,須提供程式的語言代碼清單。

  • 建構系統需要基於建構正確性的原因,明確宣告輸入和輸出檔案。由於輸入和輸出檔案名稱是根據語言代碼產生,因此語言代碼宣告就足以處理這個問題。

舉例來說,如果原始檔案名稱為 strings.xml,語言代碼 fr 列於 output_locales 中,則建構系統必須使用名為 strings_fr.xml 的檔案。這些資源需要與二進位檔封裝至 Fuchsia 套件中。

deps += [
  # These declaration works, but need to be maintained manually.  Work is
  # underway to make the "package" target collect and apply the locale files
  # automatically.
  ":en-strings",
  ":es-strings",
  ":fr-strings",

  # Contains the necessary ICU data.
  "//src/intl:icudtl",
]

請務必將 JSON 資源套件至正確的目錄。翻譯訊息的正確目錄路徑會是 assets/locales/fr/l10n.json,但適用於翻譯後的訊息。您也必須在 icudtl.dat 區段中定義 ICU 資料。