---
metadata:
  - name: generator
    content: Diplodoc Platform v5.54.4
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: zh/sdk/cocos/localization.md
    type: text/markdown
    title: Markdown version
  - href: ../../llms.txt
    type: text/markdown
    title: llms.txt
---
> **Documentation Index:** Fetch the complete configuration index at https://yandex.com/dev/games/doc/zh/llms.txt

# 本地化

<!-- 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 游戏 SDK 部分点击 **Localization Editor** 选项。

起初，编辑器看起来会很空。这是因为您需要创建首个本地化，可以通过点击左上角的 **Create** 按钮来实现。

![](../../_images/cocos/localization/create-loc.webp)

会弹出一个选择语言的模态窗口。完成选择后，点击 **Create JSON file**。

![](../../_images/cocos/localization/create-json.webp)

例如，我们选择 _English (en)_，然后在编辑器中输入以下内容。

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

点击 **Save** 按钮以保存您的本地化设置并在游戏中进行测试。

![](../../_images/cocos/localization/save-loc.webp)

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

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

#### l10n.t 方法 {#ll0nt-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 组件的节点后，可以将其本地化，通过将组件拖动到节点中。

![](../../_images/cocos/localization/label.gif)

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

如果您不想重新发明轮子，插件中有一个现成的组件，可以直接在编辑器中显示键。无需编译项目即可查看更改，因为它在翻译编辑器中进行更改时会自动更新。

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

![](../../_images/cocos/localization/l10n-label.gif)

现在，您可以在 **Key** 字段中输入您的键并享受结果。无论您如何更改本地化，更改都会立即在此处可见。

![](../../_images/cocos/localization/enter-key.gif)


---


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