---
metadata:
  - name: generator
    content: Diplodoc Platform v5.56.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: vi/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/vi/llms.txt

# Đa ngôn ngữ hóa

## Tạo bản dịch

Trước tiên, hãy tạo thư mục `i18n`, nơi lưu trữ các tệp định dạng JSON chứa nội dung bản dịch của bạn.

Ví dụ, chúng ta sẽ tạo các tệp `en.json` và `ru.json`, tương ứng với tiếng Anh và tiếng Nga.

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

Giờ hãy thêm nội dung vào các tệp này:

{% list tabs %}

- en.json

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

- ru.json

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

{% endlist %}

## Lấy bản dịch

Để lấy giá trị của một khóa cụ thể, bạn có thể sử dụng phương thức `i18n.localize(key)`.

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

Ngoài ra còn có phương thức hỗ trợ `i18n.gui(node_id, key)`, dùng để thay thế nội dung của một nút đã chọn trong giao diện người dùng.

Với `i18n.gui()`, chúng ta có thể rút ngắn:

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

Trước:

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

### Ví dụ về việc bản địa hóa trò chơi

```lua
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.png)

## Thay đổi ngôn ngữ bản dịch

{% note info "Ghi chú" %}

Module i18n sẽ tự động khởi tạo ngôn ngữ phù hợp với ngôn ngữ của người dùng lấy từ [biến môi trường](https://yandex.com/dev/games/doc/vi/sdk/defold/environment.md).

Bạn chỉ nên sử dụng phương pháp này nếu muốn thay đổi ngôn ngữ thủ công theo mong muốn của người dùng.

{% endnote %}

Để thay đổi ngôn ngữ bản dịch, bạn có thể dùng phương thức `i18n.set_language(code: string)`.

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

---


<!-- source: vi/_includes/button-git-defold.md -->
<a href="https://github.com/yandex-games-plugins/defold">
  <span class="button">Kho lưu trữ</span>
</a>
<!-- endsource: vi/_includes/button-git-defold.md -->
