一、下载安装Node.js 并配置好环境变量
1.https://nodejs.org/zh-cn/download/
![](https://img.haomeiwen.com/i8921247/0116f4808c82a5d0.png)
二、下载localize-with-spreadsheet-2
https://github.com/NeverwinterMoon/localize-with-spreadsheet-2
1.下载后 在当前文件夹下执行 npm install localize-with-spreadsheet-2
成功后会多出这三个文件:
![](https://img.haomeiwen.com/i8921247/21de4b8372cf8d73.png)
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 ,成功后会在控制台看到
![](https://img.haomeiwen.com/i8921247/8178f4e0c765969b.png)
2.创建api
![](https://img.haomeiwen.com/i8921247/861b8d404dceca30.png)
成功后
![](https://img.haomeiwen.com/i8921247/db2d6ea46c99d2c2.png)
这个API密钥就是js文件 中的[api-key]
三、创建一个google 在线文档
例如:https://docs.google.com/spreadsheets/d/1IUQnoVbkUYMl36mAm3ts27pQxg0D1WKASM9wy0s_tNI/edit#gid=0
1IUQnoVbkUYMl36mAm3ts27pQxg0D1WKASM9wy0s_tNI
就是js文件 中的[spreadsheet-key]
1.将此文档发布
![](https://img.haomeiwen.com/i8921247/c0b3edef08780d7a.png)
2.设置权限 设置成所有人拿到链接都可访问
![](https://img.haomeiwen.com/i8921247/49ea9649d793eafb.png)
四、执行js文件
node update-localization.js
成功后会生成你想要的的东西
![](https://img.haomeiwen.com/i8921247/96cfaab8babe6c87.png)
五、趟坑
![](https://img.haomeiwen.com/i8921247/18de594b0b6e866c.png)
报错400 是因为api key 填写错误,或者是你的账号并没有开启Google Sheet Api 功能
![](https://img.haomeiwen.com/i8921247/14c248dcdd79b7b5.png)
报错403 是因为第三步的第2步 没有给到权限
![](https://img.haomeiwen.com/i8921247/f2957658f1584d77.png)
报错 value.replace is not a function 解决方案
![](https://img.haomeiwen.com/i8921247/e01c3fc6669d9c87.png)
我的最终的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'
))
})
网友评论