如何将WPML翻译内容导出为Shopify兼容格式?
从WPML XLIFF转Shopify兼容CSV翻译文件的实操步骤
1. 解析XLIFF提取核心翻译数据
打开你从WPML导出的XLIFF文件,提取以下关键信息:
- 源语言原始内容(对应Shopify CSV的
Source列) - 目标语言翻译内容(对应
Translation列) - WPML标记的内容类型(比如
product_title:123、page_body:456,用来生成Shopify的Key列)
注意:WPML的XLIFF会带WooCommerce专属的上下文ID,需要把这类标记转换成Shopify能识别的键格式,比如把product_title:123改成t:products/[product-handle]/title([product-handle]后续替换为Shopify产品的实际路径标识)。
2. 对齐Shopify翻译CSV的固定格式
Shopify的翻译CSV必须包含以下必填列,顺序可调整但字段名不能错:
Key:内容的唯一标识,格式为t:[内容类型]/[具体路径],示例:- 产品标题:
t:products/awesome-coffee-mug/title - 页面内容:
t:pages/about-us/body
- 产品标题:
Source:源语言的原始文本Translation:目标语言的翻译文本Locale:目标语言的标准代码(比如zh-CN、fr,必须和Shopify后台启用的语言代码完全一致)Namespace:固定填shopify
3. 批量转换XLIFF到CSV
小站点手动处理
把提取的内容填入Excel或Google Sheets,按Shopify的列格式整理后,直接导出为UTF-8编码的CSV文件。
大站点脚本批量处理
用Python脚本自动解析XLIFF并生成符合格式的CSV,示例代码:
import xml.etree.ElementTree as ET import csv # 加载WPML导出的XLIFF文件 tree = ET.parse('wpml-translations.xlf') root = tree.getroot() # 定义Shopify CSV的列名 csv_fields = ['Key', 'Source', 'Translation', 'Locale', 'Namespace'] output_rows = [] # 遍历XLIFF中的所有翻译单元 for trans_unit in root.findall('.//{urn:oasis:names:tc:xliff:document:1.2}trans-unit'): source_text = trans_unit.find('.//{urn:oasis:names:tc:xliff:document:1.2}source').text target_text = trans_unit.find('.//{urn:oasis:names:tc:xliff:document:1.2}target').text wpml_id = trans_unit.get('id') # 转换WPML的ID为Shopify兼容的Key格式 if 'product_title' in wpml_id: shopify_key = 't:products/[product-handle]/title' elif 'product_description' in wpml_id: shopify_key = 't:products/[product-handle]/body' elif 'page_title' in wpml_id: shopify_key = 't:pages/[page-handle]/title' elif 'page_content' in wpml_id: shopify_key = 't:pages/[page-handle]/body' else: shopify_key = wpml_id # 未匹配的类型暂时保留,后续手动调整 output_rows.append({ 'Key': shopify_key, 'Source': source_text, 'Translation': target_text, 'Locale': 'zh-CN', # 替换为你的目标语言代码 'Namespace': 'shopify' }) # 写入CSV文件(UTF-8编码) with open('shopify-compatible-translations.csv', 'w', newline='', encoding='utf-8') as csv_file: writer = csv.DictWriter(csv_file, fieldnames=csv_fields) writer.writeheader() writer.writerows(output_rows)
注意:替换代码中的Locale值为你Shopify后台启用的语言代码,[product-handle]和[page-handle]需要后续替换为实际迁移后的产品/页面handle(可从Shopify导出的产品CSV中获取)。
4. 验证CSV格式并测试上传
- 确保CSV编码为UTF-8(Shopify不支持其他编码)
- 检查
Key列的格式是否完全符合Shopify规范,避免特殊字符 - 先上传少量测试行到Shopify后台(路径:设置→语言→导入翻译),验证翻译内容是否能正常关联到对应内容
5. 绑定已迁移的内容
如果已经完成WooCommerce产品/页面到Shopify的迁移,把CSV中所有[product-handle]和[page-handle]替换为Shopify后台的实际handle值,确保翻译内容和对应的产品/页面精准绑定。
内容的提问来源于stack exchange,提问作者kevinaskevin
相关产品推荐
相关产品推荐

