---
title: "获取语言包"

doc_type: static
doc_category: static
doc_version: v20270301
source_url:
  html: 'https://developer.shopline.com/zh-hans-cn/docs/online-store-3-0-themes/best-practice/fetching-language-packs'
  md: 'https://developer.shopline.com/zh-hans-cn/docs/online-store-3-0-themes/best-practice/fetching-language-packs.md'
---
# 获取语言包

语言包是一种文本翻译文件，用于适应不同地区的语言展示要求。该文件以 JSON 格式存储，包含针对特定语言环境的文本翻译内容。

[Bottle](https://developer.shopline.com/zh-hans-cn/docs/online-store-3-0-themes/bottle.md) 主题在全局对象 `window.Shopline` 下注入多语言包相关的方法，用于多语言包的初始化和多语言文案的获取。本文将详细介绍如何调用相关方法以获取语言包和多语言文案。

***

## 获取主题当前语种语言包

### window\.Shopline.i18nInit

该方法用于初始化并加载主题中当前语种的语言包。调用此方法将触发一个异步请求，从远程服务器拉取语言包。

#### 方法类型定义

`i18nInit(): Promise<any>`

#### 参数说明

无

#### 返回值说明

`Promise<any>`：一个 `Promise` 对象。解析成功后，将返回一个包含当前语种文案键值对的 JSON 对象。

#### 代码示例

以下是获取主题当前语种语言包的代码示例：

```javascript
const themeI18nData = await window.Shopline.i18nInit();

console.log(themeI18nData) // 打印多语言包数据
```

### window\.Shopline.t

在主题中完成当前语种语言包的初始化和加载后，可调用该方法获取指定键名对应的文案。

#### 方法类型定义

`t(path: string, hash?: Record<string, any>) => string`

#### 参数说明

| **参数名** | **参数类型**             | **必填** | **描述**                                                                                                                              |
| ------- | -------------------- | ------ | ----------------------------------------------------------------------------------------------------------------------------------- |
| path    | string               | 是      | 键名，具体规范可参考 [多语言键名命名规范](https://developer.shopline.com/zh-hans-cn/docs/online-store-3-0-themes/theme-structure/i-18-n.md#oZbFS)。       |
| hash    | Record\<string, any> | 否      | 变量插值，可动态插入变量，具体使用方法参考 [变量插值](https://developer.shopline.com/zh-hans-cn/docs/online-store-3-0-themes/theme-structure/i-18-n.md#EclGx)。 |

#### 返回值说明

`string`：多语言翻译文案

#### 代码示例

在 Bottle 主题的搜索页面上，用于展示搜索结果数量的英语文案对应的 JSON 文件结构如下：

```json
// i18n/en.json

{
  "search": {
    "count_results": "{{count}} Results"
  }
}
```

其中，`{{count}} Results` 中插入了变量 `count`。你可以通过以下方式获取 `search.count_results` 键名的英语文案：

```javascript
let totalCount = 10;

let countText = window.Shopline.t("search.count_results", { count: totalCount });

console.log(countText) // 输出 “10 Results”
```

***

## 获取应用当前语种语言包

[主题应用扩展](https://developer.shopline.com/zh-hans-cn/docs/online-store-3-0-themes/integrate-apps-with-themes/theme-app-extension/overview.md) 允许上传应用专属自己的多语言文件，当你需要获取这些文件内容时，可调用以下方法。

### window\.Shopline.extension.i18nInit

该方法用于初始化并加载应用中当前语种的语言包。调用此方法将触发一个异步请求，从远程服务器拉取语言包。

#### 方法类型定义

`i18nInit(extensionUuid: string): Promise<any>`

#### 参数说明

| **参数名**       | **参数类型** | **必填** | **描述**      |
| ------------- | -------- | ------ | ----------- |
| extensionUuid | string   | 是      | 应用的唯一标识符 ID |

#### 返回值说明

`Promise<any>`：一个 `Promise` 对象。解析成功后，将返回一个包含当前语种文案键值对的 JSON 对象。

#### 代码示例

以下是获取应用当前语种语言包的代码示例：

```javascript
const extensionI18nData = await window.Shopline.extension.i18nInit("xxxxxx");

console.log(extensionI18nData) // 打印多语言包数据
```

### window\.Shopline.extension.t

在应用中完成当前语种语言包的初始化和加载后，可调用该方法获取指定键名对应的文案。

#### 方法类型定义

`t(extensionUuid: string, path: string, hash?: Record<string, any>) => string`

#### 参数说明

| **参数名**       | **参数类型**             | **必填** | **描述**                                                                                                                              |
| ------------- | -------------------- | ------ | ----------------------------------------------------------------------------------------------------------------------------------- |
| extensionUuid | string               | 是      | 应用的唯一标识符 ID。                                                                                                                        |
| path          | string               | 是      | 键名，具体规范可参考 [多语言键名命名规范](https://developer.shopline.com/zh-hans-cn/docs/online-store-3-0-themes/theme-structure/i-18-n.md#oZbFS)。       |
| hash          | Record\<string, any> | 否      | 变量插值，可动态插入变量，具体使用方法参考 [变量插值](https://developer.shopline.com/zh-hans-cn/docs/online-store-3-0-themes/theme-structure/i-18-n.md#EclGx)。 |

#### 返回值说明

`string`：多语言翻译文案

#### 代码示例

在某个应用的商品列表中，展示价格的英语文案对应的 JSON 文件结构如下：

```json
// en.json

{
  "product_list": {
    "from": "From {{price}}"
  }
}
```

其中，`From {{price}}` 中插入了变量 `price`，你可以通过以下方式获取 `product_list.from` 键名的多语言文案：

```javascript
let money = 10;

let priceText = window.Shopline.extension.t("xxxxxx", "product_list.from", { price: money });

console.log(priceText) // 输出 “From 10”
```
