透過 PowerShell 下載 google sheet,產生 i18n 多語系翻譯 Json 檔
解決問題
隨著企業系統架構越來越複雜,為解決開發過程中,不同角色或應用程式,需要維護同一份語系翻譯檔。
實作原理
透過 Google sheet 機制就可以達到多人同時維護一份翻譯檔,並透過匯出 csv 檔的功能,將檔案下載到本地,透過 Powershell 去讀取 csv,轉換成 i18n Json 物件並匯出。
首先,透過 google sheet 可以很輕鬆的達到多人共同協作
-
建立一個新的 google sheet
https://docs.google.com/spreadsheets/d/192ji-2k1XHOdXYBu8fD7wNSAnz-eKcdQB57LR66quTg -
開啟 google sheet 允許知道連結的人擁有訪問存取權限
撰寫 powershell
$filepath = './Rawi18nFile.csv'
& {
(new-object net.webclient).downloadfile('https://docs.google.com/spreadsheets/d/192ji-2k1XHOdXYBu8fD7wNSAnz-eKcdQB57LR66quTg/export?format=csv', $filepath)
}
$translate = @{}
$NewCsvData= Import-CSV $filepath | Foreach-Object{
foreach ($property in $_.PSObject.Properties)
{
if($property.Name -eq 'Module' )
{
$module = $property.Value
continue
}
if($property.Name -eq 'Key')
{
$key = $property.Value
continue
}
$mainKey ="$module.$key";
if (!$translate.ContainsKey($property.Name)) {
$translate[$property.Name]= @{}
}
$translate[$property.Name][$mainKey] = $property.Value
}
}
forEach($lang in $translate.Keys){
$translate[$lang] | ConvertTo-Json | % { [System.Text.RegularExpressions.Regex]::Unescape($_) } | new-item -force "./output/$lang.json"
}
執行結果
-
右鍵 > 用 Powershell 執行
-
Powershell 顯示成功
-
此時 output 資料夾產生 i18n語系檔