Định dạng thông báo bản địa hoá

Mỗi tiện ích hoặc ứng dụng quốc tế hoá đều có ít nhất một tệp có tên là messages.json cung cấp các chuỗi dành riêng cho từng ngôn ngữ. Trang này mô tả định dạng của tệp messages.json. Để biết thông tin về cách quốc tế hoá và bản địa hoá, hãy xem trang Quốc tế hoá.

Tóm tắt trường

Đoạn mã sau đây cho thấy các trường được hỗ trợ cho messages.json. Chỉ các trường "name" và "message" là bắt buộc.

{
  "name": {
    "message": "Message text, with optional placeholders.",
    "description": "Translator-aimed description of the message.",
    "placeholders": {
      "placeholder_name": {
        "content": "A string to be placed within the message.",
        "example": "Translator-aimed example of the placeholder string."
      },
      ...
    }
  },
  ...
}

Ví dụ:

Sau đây là tệp messages.json xác định 3 thông báo có tên là "prompt_for_name", "hello" và "bye":

{
  "prompt_for_name": {
    "message": "What's your name?",
    "description": "Ask for the user's name"
  },
  "hello": {
    "message": "Hello, $USER$",
    "description": "Greet the user",
    "placeholders": {
      "user": {
        "content": "$1",
        "example": "Cira"
      }
    }
  },
  "bye": {
    "message": "Goodbye, $USER$. Come back to $OUR_SITE$ soon!",
    "description": "Say goodbye to the user",
    "placeholders": {
      "our_site": {
        "content": "Example.com",
      },
      "user": {
        "content": "$1",
        "example": "Cira"
      }
    }
  }
}

Thông tin chi tiết về trường

Phần này mô tả từng trường có thể xuất hiện trong tệp messages.json. Để biết thông tin chi tiết về cách sử dụng tệp thông báo (ví dụ: điều gì xảy ra khi một ngôn ngữ không xác định tất cả thông báo), hãy xem bài viết Quốc tế hoá.

tên

Trên thực tế, không có trường nào có tên là "name". Tên của trường này là tên của thông báo – cùng một name mà bạn thấy trong __MSG__name___ hoặc getMessage("_name_").

Tên là một khoá không phân biệt chữ hoa chữ thường, cho phép bạn truy xuất văn bản thông báo đã bản địa hoá. Tên có thể bao gồm các ký tự sau:

  • A-Z
  • a-z
  • 0-9
  • _ (dấu gạch dưới)
  • @
Lưu ý: Đừng xác định tên bắt đầu bằng "@@". Những tên đó được dành riêng cho các thông báo được xác định trước.

Dưới đây là 3 ví dụ về tên, lấy từ phần Ví dụ:

"prompt_for_name": {
  ...
},
"hello": {
  ...
},
"bye": {
  ...
}

Để xem thêm ví dụ về cách sử dụng tên, hãy xem trang Quốc tế hoá.

tin nhắn

Thông báo đã dịch, ở dạng một chuỗi có thể chứa các phần giữ chỗ. Sử dụng $_placeholder_name_$ (không phân biệt chữ hoa chữ thường) để tham chiếu đến một phần giữ chỗ cụ thể. Ví dụ: bạn có thể tham chiếu đến một phần giữ chỗ có tên là "our_site" dưới dạng $our_site$, $OUR_SITE$ hoặc $oUR_sITe$.

Dưới đây là 3 ví dụ về thông báo, lấy từ phần Ví dụ:

"message": "What's your name?"
...
"message": "Hello, $USER$"
...
"message": "Goodbye, $USER$. Come back to $OUR_SITE$ soon!"

Để đặt dấu đô la ($) vào chuỗi, hãy sử dụng $$. For example, use the following code to specify the message Amount (in $):

"message": "Amount (in $$)"

Although placeholders such as $USER$ are the preferred way of referring to substitution strings (strings specified using the substitutions parameter of i18n.getMessage) you can also refer to substitution strings directly within the message. For example, the following message refers to the first three substitution strings passed into getMessage():

"message": "Params: $1, $2, $3"

Despite that example, we recommend that you stick to using placeholders instead of $_n_ strings within your messages. Think of placeholders as good variable names. A week after you write your code, you'll probably forget what $1 refers to, but you'll know what your placeholders refer to. For more information on placeholders and substitution strings, see the placeholders section.

description

Optional. A description of the message, intended to give context or details to help the translator make the best possible translation.

Here are three examples of descriptions, taken from the Example section:

"description": "Ask for the user's name"
...
"description": "Greet the user"
...
"description": "Say goodbye to the user"

placeholders

Optional. Defines one or more substrings to be used within the message. Here are two reasons you might want to use a placeholder:

  • To define the text for a part of your message that shouldn't be translated. Examples: HTML code, trademarked names, formatting specifiers.
  • To refer to a substitution string passed into getMessage(). Example: $1.

Each placeholder has a name, a "content" item, and an optional "example" item. A placeholder's name is case-insensitive and can contain the same characters as a message name.

The "content" item's value is a string that can refer to substitution strings, which are specified using the i18n.getMessage method's substitutions parameter. The value of a "content" item is typically something like "Example.com" or "$1". If you refer to a substitution string that doesn't exist, you get an empty string. The following table shows how $_n_ strings correspond to strings specified by the substitutions parameter.

substitutions parameterValue of $1Value of $2Value of $3
userNamevalue of userName""""
["Cira", "Kathy"]"Cira""Kathy"""

The "example" item (optional, but highly recommended) helps translators by showing how the content appears to the end user. For example, a placeholder for a dollar amount should have an example like "$23.45".

The following snippet, taken from the Example section, shows a "placeholders" item that contains two placeholders named "our_site" and "user". The "our_site" placeholder has no "example" item because its value is obvious from the "content" field.

"placeholders": {
  "our_site": {
    "content": "Example.com",
  },
  "user": {
    "content": "$1",
    "example": "Cira"
  }
}