---
metadata:
  - name: generator
    content: Diplodoc Platform v5.66.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: https://yandex.com/dev/games/doc/zh/sdk/defold/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 -->

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

要创建翻译，请创建一个 `i18n` 文件夹。其中将存储 JSON 格式的翻译文件。

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

![文件结构截图：可见部分文件夹列表「fronts」、「i18n」、「input」和「ysdk」。 在「i18n」文件夹中有「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.key(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", "button")
end

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

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

![黑色屏幕中央有文字「Cool」，稍下方有一个带有「Button」文字的白色按钮。](../../_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/sdk/button-git.md -->
<a href="https://github.com/yandex-games-plugins/defold">
  <span class="button">存储库</span>
</a>
<!-- endsource: zh/_includes/sdk/button-git.md -->
