---
metadata:
  - name: generator
    content: Diplodoc Platform v5.52.0
alternate:
  - https://yandex.com/dev/games/doc/en/sdk/defold/localization.md
  - https://yandex.com/dev/games/doc/hi/sdk/defold/localization.md
  - https://yandex.com/dev/games/doc/ko/sdk/defold/localization.md
  - https://yandex.com/dev/games/doc/ru/sdk/defold/localization.md
  - https://yandex.com/dev/games/doc/tr/sdk/defold/localization.md
  - https://yandex.com/dev/games/doc/vi/sdk/defold/localization.md
  - https://yandex.com/dev/games/doc/zh/sdk/defold/localization.md
  - href: zh/sdk/defold/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 -->

## 创建翻译 {#create-translation}

首先，创建一个名为 `i18n` 的文件夹，用于存放 JSON 格式的翻译文件，你的所有翻译内容都将保存在这些文件中。

例如，我们可以创建 `en.json` 和 `ru.json` 文件，分别对应英文和俄语。

![](../../_images/defold/localization/files-create.webp)

现在，我们可以为这些文件添加一些内容：

{% list tabs %}

- en.json

  ```json showLineNumbers
  {
    "game": {
      "title": "Cool Game"
    },
    "button": "Button"
  }
  ```

- ru.json

  ```json showLineNumbers
  {
    "game": {
      "title": "Крутая игра"
    },
    "button": "Кнопка"
  }
  ```

{% endlist %}

## 获取翻译 {#get-translation}

要获取某个键对应的值，可以使用 `i18n.localize(key)` 方法。

```lua
print( i18n.key("game.title") ) -- "Cool Game"
```

也有一个辅助方法 `i18n.gui(node_id, key)`，它可以替换用户界面中选定节点的内容。

通过使用 `i18n.gui()` 我们可以简化：

```lua
gui.set_text(gui.get_node("Title Label"), i18n.key("game.title"))
```

前:

```lua
i18n.gui("title_label", "game.title")
```

### 游戏本地化示例 {#localization-example}

```lua showLineNumbers
local i18n = require('ysdk.i18n')

local function on_localize()
    i18n.gui("title_label", "game.title")
    i18n.gui("button_label", "game.title")
end

function init(self)
    i18n.on(on_localize)
end

function final(self)
    i18n.off(on_localize)
end
```

![](../../_images/defold/localization/example.webp)

## 切换翻译语言 {#lang-change}

{% note info %}

i18n 模块会自动根据[环境变量](https://yandex.com/dev/games/doc/zh/sdk/defold/environment.md)初始化为用户的语言。

建议仅在需要根据用户的选择手动切换语言时，才使用此方法。

{% endnote %}

要切换翻译语言，可以使用方法 `i18n.set_language(code: string)`。

```lua showLineNumbers
function settings.change_language(code)
    i18n.set_language(code)
end
```

---


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