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'
    ))
  })
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章