forked from nushell/nushell.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n.nu
143 lines (129 loc) · 4.82 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
let META_FILE = '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. Welcome to participate in the translation of the docs. And please update the `i18n-meta.json` file after you have finished writing or translating the doc. Thanks"
print $'(char nl)---(char nl)'
ls -s book/*.md
| where type == file
| select name
| upsert en {|it| get-cell $it.name en }
| upsert zh-CN {|it| get-cell $it.name zh-CN }
| upsert de {|it| get-cell $it.name de }
| upsert tr {|it| get-cell $it.name tr }
| upsert ja {|it| get-cell $it.name ja }
| upsert es {|it| get-cell $it.name es }
| upsert pt-BR {|it| get-cell $it.name pt-BR }
| to md --pretty
print $'(char nl)Possible status values: `-`,`Completed`,`In Progress`,`Being translated by @ABC`, `commit_id@author` or simply a COMMIT ID indicate your translation end to.(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 && (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 {|it| get-cell $it.name en }
| upsert zh-CN {|it| get-cell $it.name zh-CN }
| upsert de {|it| get-cell $it.name de }
| upsert tr {|it| get-cell $it.name tr }
| upsert ja {|it| get-cell $it.name ja }
| upsert es {|it| get-cell $it.name es }
| upsert pt-BR {|it| get-cell $it.name pt-BR }
| to json -i 2
| save -r $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 { |it|
let val = ($it | get $locale)
if ($val | is-empty) || $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 && (has-ref $id) {
has-change $it.name $id
} else {
'N/A'
}
} else if (has-ref $val) {
has-change $it.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 # Avaliable 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']
if $task == 'gen' {
gen-i18n-meta
} else if $task == 'update' {
update-i18n-status
} else if $task == 'outdated' {
if ($lng | is-empty) {
$'(ansi r)A locale code required, available locales: ($locales), Please try again!(ansi reset)(char nl)'
exit --now
}
let available = ($lng | str downcase) in $locales
if (not $available) {
$'(ansi r)Unsupported locale, available locales: ($locales), Please try again!(ansi reset)(char nl)'
exit --now
}
check-outdated-translation $lng
}
}