Mark Ku's Blog
首頁 關於我
透過 PowerShell 下載 google sheet,產生 i18n 多語系翻譯 Json 檔
PowerShell
透過 PowerShell 下載 google sheet,產生 i18n 多語系翻譯 Json 檔
Mark Ku
Mark Ku
June 18, 2021
1 min

透過 PowerShell 下載 google sheet,產生 i18n 多語系翻譯 Json 檔

解決問題

隨著企業系統架構越來越複雜,為解決開發過程中,不同角色或應用程式,需要維護同一份語系翻譯檔。

實作原理

透過 Google sheet 機制就可以達到多人同時維護一份翻譯檔,並透過匯出 csv 檔的功能,將檔案下載到本地,透過 Powershell 去讀取 csv,轉換成 i18n Json 物件並匯出。

首先,透過 google sheet 可以很輕鬆的達到多人共同協作

  1. 建立一個新的 google sheet
    https://docs.google.com/spreadsheets/d/192ji-2k1XHOdXYBu8fD7wNSAnz-eKcdQB57LR66quTg

  2. 開啟 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"  
}

執行結果

  1. 右鍵 > 用 Powershell 執行

  2. Powershell 顯示成功

  3. 此時 output 資料夾產生 i18n語系檔


Tags

Mark Ku

Mark Ku

Software Developer

擁有豐富網站開發經驗,直播系統、POS系統、電子商務、平台網站、SEO,專業的網站開發鐵三工程師。

Expertise

前端(React)
後端(C#)
網路管理

Social Media

facebook github website

Related Posts

Quick set up developer environment using chocolatey and powershell
Quick set up developer environment using chocolatey and powershell
January 29, 2023
1 min

Quick Links

關於我

Social Media