在 window10 使用 powershell 生成 AngularDart 多语言国际化代码。
参考官方仓库后,发现 intl_translation 使用起来很麻烦,而且官方给的代码在 powershell 用不了,所以我自己撸了一个 win10 专用脚本。
如何使用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
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