forked from nushell/nushell.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n.nu
148 lines (135 loc) · 5.05 KB
/
i18n.nu
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
cd ($env.CURRENT_FILE | path dirname | path dirname)
let META_FILE = 'tools/i18n-meta.json'
if ($META_FILE | path exists) == false {
echo '[]' | save -r $META_FILE
}
let meta = (open $META_FILE)
# Check if a git repo has the specified ref: could be a branch or tag, etc.
def 'has-ref' [
ref: string # The git ref to check
] {
let parse = (git rev-parse --verify -q $ref)
if ($parse | is-empty) { false } else { true }
}
# Update issue contents for https://github.com/nushell/nushell.github.io/issues/261
def update-i18n-status [] {
print "The following table holds the overview of the Nushell docs’ writing and translation status. We welcome translations and translation updates. When making changes please keep the `i18n-meta.json` file up-to-date."
print $'(char nl)---(char nl)'
let status = (
ls -s book/*.md
| where type == file
| select name
| upsert en {|elt| get-cell $elt.name en }
| upsert zh-CN {|elt| get-cell $elt.name zh-CN }
| upsert de {|elt| get-cell $elt.name de }
| upsert tr {|elt| get-cell $elt.name tr }
| upsert ja {|elt| get-cell $elt.name ja }
| upsert es {|elt| get-cell $elt.name es }
| upsert pt-BR {|elt| get-cell $elt.name pt-BR }
| upsert ru {|elt| get-cell $elt.name ru }
| to md --pretty
)
print $status
print $'(char nl)Possible status values: `-`,`Completed`,`In Progress`,`translated by @ABC`,`commit_id@author`,`commit_id`.(char nl)'
}
def get-cell [
name: string
lng: string
] {
let match = ($meta | where name == $name)
let cellDefault = if ($lng == 'en') { 'In progress' } else { '-' }
# For newly added docs
if ($match | length) == 0 {
$cellDefault
} else { # For existing docs
let val = ($match | get $lng | get 0)
if ($val | is-empty) {
$cellDefault
# Handle data like: "c13a71d11@hustcer"
} else if ($val | str contains '@') {
let commit = ($val | split row '@')
let id = ($commit | get 0)
if ($commit | length) > 1 and (has-ref $id) {
$'($id)@($commit | get 1)'
} else {
$val
}
} else if (has-ref $val) {
$'Translate to ($val)'
} else {
$val
}
}
}
# Generate or update meta data for docs' translation status
def gen-i18n-meta [] {
ls -s book/*.md
| where type == file
| select name
| upsert en {|elt| get-cell $elt.name en }
| upsert zh-CN {|elt| get-cell $elt.name zh-CN }
| upsert de {|elt| get-cell $elt.name de }
| upsert tr {|elt| get-cell $elt.name tr }
| upsert ja {|elt| get-cell $elt.name ja }
| upsert es {|elt| get-cell $elt.name es }
| upsert pt-BR {|elt| get-cell $elt.name pt-BR }
| upsert ru {|elt| get-cell $elt.name ru }
| to json -i 2
| save -rf $META_FILE
}
def has-change [
name: string, # The doc file name to check
commit: string, # The ending commit id
] {
let diff = (git diff $commit $'book/($name)' | str trim)
if ($diff | is-empty) { $'(ansi g)No(ansi reset)' } else { $'(ansi r)Yes(ansi reset)' }
}
def check-outdated-translation [
lng: string # The locale to check outdated
] {
let columns = { 'zh-cn': 'zh-CN', 'pt-br': 'pt-BR' }
let locale = if ($lng in $columns) { $columns | get $lng } else { $lng }
open $META_FILE | select name $locale | insert outdated { |row|
let val = ($row | get $locale)
if ($val | is-empty) or $val == '-' {
'-'
# Handle data like: "c13a71d11@hustcer"
} else if ($val | str contains '@') {
let commit = ($val | split row '@')
let id = ($commit | get 0)
if ($commit | length) > 1 and (has-ref $id) {
has-change $row.name $id
} else {
'N/A'
}
} else if (has-ref $val) {
has-change $row.name $val
} else {
'N/A'
}
} | sort-by outdated name
}
# Use `nu ./i18n.nu outdated zh-CN` to check outdated translations for zh-CN
# Some helper commands for i18n related tasks
def main [
task: string # Available task: `gen`, `update`, `outdated`
lng?: string # The locale to check outdated: zh-CN, de, etc.
] {
let locales = ['zh-cn', 'de', 'tr', 'ja', 'es', 'pt-br', 'ru']
match $task {
'gen' => { gen-i18n-meta },
'update' => { update-i18n-status },
'outdated' => {
if ($lng | is-empty) {
print $'(ansi r)A locale code required, available locales: ($locales), Please try again!(ansi reset)(char nl)'
exit
}
let available = ($lng | str downcase) in $locales
if (not $available) {
print $'(ansi r)Unsupported locale, available locales: ($locales), Please try again!(ansi reset)(char nl)'
exit
}
check-outdated-translation $lng
}
}
}