在window10 使用powershell 生成 AngularDart多语言国际化代码。


参考官方仓库后,发现intl_translation使用起来很麻烦,而且官方给的代码在powershell用不了,所以我自己撸了一个win10专用脚本。

如何使用:

1.拷贝下面的脚本到AngularDart工程根目录

2.运行powershell,输入0。

3.在运行一次,输入1。

代码:

script_extract_to_arb.ps1

function Get-Files($path, $extension ) {
    $result = [string](Get-ChildItem -Path $path -Recurse -Filter $extension | Resolve-Path -Relative)
    return $result + " ";
}

function Get-DartFiles ($project) {
    $path1 = Get-Files "lib" "*.dart"
    $path2 = Get-Files ".dart_tool\build\generated\$project\lib\" "*.dart"
    $path3 = $path1 + $path2;
    return $path3
}

function Get-ArbFiles($dir) {
    $result = Get-Files $dir "*.arb"
    return $result + " ";
}

function Get-SupportCultures {
    return "en_US", "zh_CN" 
}

function Extract_To_Arb ($project, $outputDir) {
    If (!(test-path $outputDir)) {
        New-Item -ItemType Directory -Force -Path $outputDir
    }

    $path = Get-DartFiles($project)    
    Get-SupportCultures | ForEach-Object { 
        $cmd = "pub run intl_translation:extract_to_arb  --locale $_ --output-file $_.arb  --output-dir $outputDir $path" 
        Invoke-Expression $cmd 
    }
    # Invoke-Expression ("pub run intl_translation:extract_to_arb  --locale zh_CN --output-file zh_CN.arb  --output-dir messages " + $path )
}

function Generate_From_Arb ($project, $arbDir, $codeDir) {
    If (!(test-path $codeDir)) {
        New-Item -ItemType Directory -Force -Path $codeDir
    }
    $dart_files = Get-DartFiles($project)    
    $arb_files = Get-ArbFiles($arbDir)
    $cmd = " pub run intl_translation:generate_from_arb --output-dir $codeDir $arb_files $dart_files"
    Invoke-Expression $cmd 

    # pub run intl_translation:generate_from_arb --output-dir lib/messages messages/intl_messages.arb lib/app_component.dart .dart_tool/build/generated/examples.i18n/lib/app_component.template.dart
}

$choice = Read-Host("
 Choice 0 to extract_to_arb
 Choice 1 to generate_from_arb
 ");

$proj = (Get-Content .\pubspec.yaml)[0].Replace("name:", "").Trim()
$arbDir = "messages"
$codeDir = "lib/messages"
if ($choice.Equals("0")) {
    Extract_To_Arb $proj $arbDir
}
else {
    Generate_From_Arb $proj $arbDir $codeDir;
}

gist地址:https://gist.github.com/DaZiYuan/434fa8a13e231d89848800755012a909