Convert a Google Spreadsheet to a localization file

一、下载安装Node.js 并配置好环境变量

1.https://nodejs.org/zh-cn/download/

二、下载localize-with-spreadsheet-2

https://github.com/NeverwinterMoon/localize-with-spreadsheet-2

1.下载后 在当前文件夹下执行 npm install localize-with-spreadsheet-2
成功后会多出这三个文件:

2.在当前文件夹底下创建 update-localization.js 文件
内容:

const Localize = require('localize-with-spreadsheet-2')
Localize.fromGoogleSpreadsheet('[api-key]', '[spreadsheet-key]', '*')
  .then(localizer => {
    localizer.setKeyCol('KEY') // name of the column containing the translation key

    Array.from(['en', 'de']).forEach(language => localizer.save(
      `project-name/resource/${language}.lproj/Localizable.strings`,
    //`project-name/resource/values-${language}/strings.xml`,
      { valueCol: language, format: 'ios' } // format can also be 'android' or 'json'
    ))
  })

二、申请api key 及权限

文档:https://theoephraim.github.io/node-google-spreadsheet/#/getting-started/authentication?id=api-key
1.首先需要启用Google Sheet api ,成功后会在控制台看到

2.创建api



成功后


这个API密钥就是js文件 中的[api-key]

三、创建一个google 在线文档

例如:https://docs.google.com/spreadsheets/d/1IUQnoVbkUYMl36mAm3ts27pQxg0D1WKASM9wy0s_tNI/edit#gid=0
1IUQnoVbkUYMl36mAm3ts27pQxg0D1WKASM9wy0s_tNI 就是js文件 中的[spreadsheet-key]
1.将此文档发布


2.设置权限 设置成所有人拿到链接都可访问

四、执行js文件

node update-localization.js
成功后会生成你想要的的东西

五、趟坑


报错400 是因为api key 填写错误,或者是你的账号并没有开启Google Sheet Api 功能


报错403 是因为第三步的第2步 没有给到权限


报错 value.replace is not a function 解决方案


我的最终的js 文件仅供参考

const Localize = require('localize-with-spreadsheet-2')

Localize.fromGoogleSpreadsheet('AIzaS************pLrDwz9udWA', '1f6Dke6*********JTvyjSSxja2_wlP2MawVWsnHEak', '*')
  .then(localizer => {
    localizer.setKeyCol('key') // name of the column containing the translation key

    Array.from(['zh-chs','en', 'pt','es']).forEach(language => localizer.save(
      `project-name/resource/values-${language}/strings.xml`,
      { valueCol: language, format: 'android' } // format can also be 'android' or 'json'
    ))
  })
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章