主题国际化 - shopi8 中文建站教程

主题国际化

摘要

13 主题国际化

先判断问题出现在哪里

如果你的主题里写死了 <button>Add to Cart</button>,当一个法国商家使用你的主题时,他会绝望地发现这个按钮无法变成法语。

主题国际化的四项 Shopify 检查清单
主题国际化的四项 Shopify 检查清单

核心逻辑是:代码中绝对不能出现硬编码(Hardcoded)的静态文本。

所有的文本必须被提取为“翻译键(Translation Keys)”,存放在 locales/ 目录下的 JSON 文件中。在前端,通过 Liquid 的 t 过滤器(Translate Filter)动态调用。Shopify 会根据当前用户选择的语言环境,自动去对应的 JSON 文件中寻找翻译并渲染。

实战步骤

步骤 1:建立 locales/ 目录结构

操作路径在主题根目录的 locales 文件夹中创建 JSON

  1. 创建默认语言文件:en.default.json(英语)。
  2. 创建其他语言文件:fr.json(法语)、zh-CN.json(简体中文)。
  3. JSON 结构规范:按页面或组件进行层级嵌套。
    // en.default.json
    {
      "products": {
        "product": {
          "add_to_cart": "Add to cart",
          "sold_out": "Sold out"
        }
      }
    }

步骤 2:在 Liquid 中使用 t 过滤器

操作路径替换所有硬编码的文本

  1. 基础调用
    <button>{{ 'products.product.add_to_cart' | t }}</button>
  2. 带变量的动态翻译
    JSON 中:"cart_count": "You have {{ count }} items in your cart"
    Liquid 中:{{ 'cart.general.cart_count' | t: count: cart.item_count }}

步骤 3:翻译 Schema 中的后台配置项

操作路径在 {% schema %} 中使用 t 标签

  1. 不仅前台要翻译,后台给商家看的设置面板也要翻译!
  2. 在 Schema 中,不能使用 {{ 'key' | t }},必须使用 t: 前缀。
    "settings": [
      {
        "type": "text",
        "id": "title",
        "label": "t:sections.featured_collection.settings.title.label"
      }
    ]

步骤 4:实现多货币与多语言切换器 (Localization Form)

操作路径在 Header 或 Footer 中添加切换表单

  1. 使用 Shopify 原生的 {% form 'localization' %}
    {% form 'localization', id: 'FooterLanguageForm' %}
      <select name="locale_code" onchange="this.form.submit()">
        {% for language in localization.available_languages %}
          <option value="{{ language.iso_code }}" {% if language.iso_code == localization.language.iso_code %}selected{% endif %}>
            {{ language.endonym_name }}
          </option>
        {% endfor %}
      </select>
    {% endform %}
  2. 当用户选择新语言并提交表单时,Shopify 会自动刷新页面并切换到对应的语言环境。

常见误区与处理方法

误区一:在 JavaScript 文件中直接使用 t 过滤器

规避方法:这是前端新手最容易犯的错误。你写了一个 theme.js,在里面写了 alert("{{ 'general.success' | t }}");。结果浏览器直接报错,因为 .js 文件不会经过 Shopify 服务器的 Liquid 引擎解析,它只是一堆静态的文本!在外部 JS 文件中,绝对不能写 Liquid 代码。 正确的做法是:在 theme.liquid<head> 中,定义一个全局的 JavaScript 对象,将需要的翻译文本注入进去:

<script>
  window.themeStrings = {
    successMessage: {{ 'general.success' | t | json }}
  };
</script>
然后在你的 theme.js 中调用 alert(window.themeStrings.successMessage);

误区二:JSON 翻译文件缺少键值导致页面大面积报错

规避方法:你在 en.default.json 里加了一个新的翻译键 "new_feature": "New",但在 fr.json 里忘记加了。当法语用户访问时,页面上原本该显示文字的地方,会直接暴露出一长串丑陋的错误代码:Translation missing: fr.products.new_feature。这会让网站显得极其不专业。在发布主题前,必须使用 Shopify Theme Check 工具扫描代码。 它会自动对比所有 locales/ 下的 JSON 文件,揪出所有缺失的翻译键。

误区三:多货币切换后,Ajax 购物车里的价格没有更新

规避方法:用户在页脚把货币从 USD 切换到了 EUR。页面刷新了,产品页的价格变成了欧元。但当用户点击“加入购物车”弹出 Ajax 抽屉时,里面的价格居然还是美元!这是因为你的 Ajax 购物车是通过 JS 渲染的,而 JS 里缓存了旧的货币格式。在处理多货币时,前端 JS 必须监听货币切换事件,或者在每次 Ajax 请求购物车数据时,强制要求 Shopify 返回带 HTML 货币符号的格式化价格(Formatted Price),而不是自己用 JS 去拼接数字。

主题国际化从判断到验证的三步执行路径
主题国际化从判断到验证的三步执行路径

FAQ

主题国际化应该先检查什么?

先在测试主题或测试页面中操作,并保留修改前版本和验证记录。不要同时改很多位置,先记录当前页面和数据,再处理最明确的问题。

需要马上安装新的 Shopify App 吗?

不一定。先判断主题现有功能、后台字段和少量代码能否解决。只有需要持续同步数据或复杂自动化时,再评估 App 的费用、脚本负担和卸载影响。

修改后怎么验证是否有效?

记录修改日期、页面 URL 和改动内容,再用实际页面、移动端、Google Search Console、Bing Webmaster Tools 或 GA4 检查结果。技术修改还要保留测试记录和回滚版本。

哪些情况不建议马上修改?

数据量太少、追踪没有配置、问题还没有复现,或者正在进行大型主题更新时,不建议一次性重做。先把问题拆开,确认影响范围后再改。

下一步阅读

📢 Share this article

Any other questions?

Our professional team is ready to answer your questions.

Was this article helpful to me?

This article is suitable for all merchants and developers who want to learn about Shopify. Whether you are a beginner just starting out with Shopify or an advanced user looking to improve your skills, you will gain practical knowledge and techniques from it. The methods in this article have all been tested and proven in practice and can be directly applied to your projects.

How can we apply the methods described in the article?

Each step in this article comes with detailed instructions and code examples, which you can directly copy and use. It's recommended to try it in a test environment first to confirm the results before applying it to the production site. If you encounter any problems during implementation, feel free to leave a comment or join our discussion group for help; we and our community members will be happy to assist you.

Can the code in the article be used directly?

Yes! All the code examples we provide have been tested and can be used directly in your Shopify theme. Remember to adjust the parameters and styles according to your actual needs. If you encounter any problems, feel free to leave a message for discussion.

How often will new content be updated?

We publish 2-3 high-quality Shopify tutorials and operational tips every week. Follow our WeChat official account or join our discussion group to get the latest content and exclusive resources first.

Can I get help if I encounter a problem?

Of course! You can leave a comment below the article or join our WeChat group to connect with 1000+ Shopify merchants and developers. We'll get back to you as soon as possible.

Ready to get started?

Follow us to get the latest Shopify tutorials and operational tips.

Join the community Contact Us