---
metadata:
  - name: generator
    content: Diplodoc Platform v5.66.0
alternate:
  - https://yandex.com/dev/games/doc/en/sdk/cocos/localization.md
  - https://yandex.com/dev/games/doc/hi/sdk/cocos/localization.md
  - https://yandex.com/dev/games/doc/ko/sdk/cocos/localization.md
  - https://yandex.com/dev/games/doc/ru/sdk/cocos/localization.md
  - https://yandex.com/dev/games/doc/tr/sdk/cocos/localization.md
  - https://yandex.com/dev/games/doc/vi/sdk/cocos/localization.md
  - https://yandex.com/dev/games/doc/zh/sdk/cocos/localization.md
  - href: https://yandex.com/dev/games/doc/zh/sdk/cocos/localization.md
    type: text/markdown
    title: Markdown version
  - href: https://yandex.com/dev/games/doc/zh/llms.txt?revision=r21993288
    rel: describedby
---
> **Documentation Index:** Fetch the complete configuration index at https://yandex.com/dev/games/doc/zh/llms.txt?revision=r21993288

# 本地化

<!-- source: zh/_includes/script-common.md -->
<!-- source: zh/_includes/script/index-js.md -->

<!-- endsource: zh/_includes/script/index-js.md -->

<!-- source: zh/_includes/script/requirements-js.md -->

<!-- endsource: zh/_includes/script/requirements-js.md -->

<!-- source: zh/_includes/script/image-modal-js.md -->

<!-- endsource: zh/_includes/script/image-modal-js.md -->
<!-- endsource: zh/_includes/script-common.md -->

## 本地化编辑器 {#localization-editor}

翻译以JSON格式存储在项目内，我们建议使用该格式进行编辑。

安装插件后，在 Yandex Games SDK 部分点击 **Localization Editor** 选项。

要创建本地化，请在左上角点击 **Create** 按钮。

![Cocos Creator截图，"Localization Editor"窗口：指针悬停在左上角的"+ New"按钮上。 该窗口右侧是代码编辑器，第一行显示"{"，第二行显示"}"。](../../_images/cocos/localization/create-loc.webp)

您将看到一个模态窗口，可以在其中选择语言。 完成后，点击 **Create JSON file**。

![Cocos Creator截图，"Localization Editor"窗口上方弹出窗口。 窗口中央显示文本"Create new localization"，下方是标题"Language"，下拉列表中选择了语言"Afar (aa)"，再下方是"Create JSON file!"按钮。](../../_images/cocos/localization/create-json.webp)

例如，选择 _English (en)_ 并在编辑器中输入以下内容：

```json showLineNumbers
{
    "title": "best game in the world 2"
}
```

点击 **Save** 按钮以保存本地化并在游戏中测试。

![Cocos Creator截图，"Localization Editor"窗口（窗口名称以蓝色字体显示）。 左上角有三个按钮："+ New"、软盘图标和文本"Save"（指针悬停在此按钮上）、文件夹图标和文本"Open"。 下方是包含一个文件"EN"的目录。 该窗口右侧是代码编辑器，第一行显示"{"，第二行显示""title": "best game in the world 2""，第三行显示"}"。](../../_images/cocos/localization/save-loc.webp)

### 应用您的本地化 {#localization-use}

可以通过两种方式应用本地化：使用 `l10n.t()` 方法或通过智能本地化组件。

#### l10n.t方法 {#l10nt-method}

首先使用`l10n.t()`方法。 创建一个新组件并将以下内容粘贴到其中：

```tsx showLineNumbers
import { _decorator, Component, Label } from 'cc';
import { l10n } from 'db://yandex-games-sdk/ysdk';
const { ccclass, requireComponent } = _decorator;

@ccclass('ButtonLocalizationTest') // 组件名称。
@requireComponent(Label) // 依赖于 Label 组件。
export class ButtonLocalizationTest extends Component {
    onLoad() {
        const label = this.getComponent(Label); // 获取 Label 组件。
        label.string = l10n.t('title'); // 根据之前设置的键设置 Label 的文本。
    }
}
```

在场景中创建带有Label组件的节点后，您可以通过将组件拖到节点上来对其进行本地化。

![Cocos Creator设置视频：在"Hierarchy"窗口中展开了以"scene-2D"为根元素的结构。 其中包含"Canvas"元素，该元素中又包含"Camera"元素。 用户点击鼠标右键，选择"Create" → "2D objects" → "Label"。 出现"Label"对象，用户将其选中，以便在"Inspector"窗口中显示其属性。 在"Assets"窗口中，"scene-2D"元素旁边是"LabelLocalization Test"元素。 用户将此元素拖到cc.Label部分。 然后用户点击"预览"面板上的"Play"按钮（该按钮位于屏幕顶部中央）。 出现一个可交互的游戏弹出窗口，加载完成后屏幕上显示本地化文本"best game in the world 2"。](../../_images/cocos/localization/label.gif)

#### 现成的 L10nLabel 组件 {#l10nlabel}

插件中有一个现成的组件，可直接在编辑器中显示键。 无需编译项目即可查看更改，因为它在翻译编辑器中修改时会自动更新。

点击感兴趣节点的 **Add Component** 按钮，滚动到 **YandexGamesSDK** 组并选择 **L10nLabel**。

![Cocos Creator设置视频：用户点击组件列表末尾的"Add component"按钮。 在下拉列表中，用户选择"YandexGamesSDK" → "L10nLabel"。](../../_images/cocos/localization/l10n-label.gif)

为了让本地化更改立即显示在项目中而无需重新编译，请在 **Key** 字段中输入您的键。

![Cocos Creator设置视频：在"Localization Editor"窗口（窗口名称以蓝色字体显示）中，用户正在编辑文本。 他将文本""title": "best game in 3asd a asd a""改为""title": "best game in the world 3""。 然后，他在"Localization Editor"窗口左上角点击带有软盘图标和"Save"文本的按钮。 紧接着，"Scene"窗口中的文本从"best game in 3asd a asd a"变为"best game in the world 3"。](../../_images/cocos/localization/enter-key.gif)

---


<!-- source: zh/_includes/sdk/button-git.md -->
<a href="https://github.com/yandex-games-plugins/cocos">
  <span class="button">存储库</span>
</a>
<!-- endsource: zh/_includes/sdk/button-git.md -->
