-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtranslate_package.R
More file actions
288 lines (256 loc) · 10.9 KB
/
translate_package.R
File metadata and controls
288 lines (256 loc) · 10.9 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
translate_package = function(
dir = '.',
languages,
diagnostics = list(check_cracked_messages, check_untranslated_cat, check_untranslated_src),
custom_translation_functions = list(R = NULL, src = NULL),
max_translations = Inf,
use_base_rules = package %chin% .potools$base_package_names,
copyright = NULL,
bugs = '',
verbose = !is_testing()
) {
result <- check_potools_sys_reqs()
if (!isTRUE(result)) stop(result) # nocov
stopifnot(
'Only one package at a time' = length(dir) == 1L,
"'dir' must be a character" = is.character(dir),
"'languages' must be a character vector" = missing(languages) || is.character(languages),
"'diagnostics' should be empty, a function, or list of functions" =
is.null(diagnostics)
|| is.function(diagnostics)
|| (is.list(diagnostics) && all(vapply(diagnostics, is.function, logical(1L))))
)
# en-list singleton diagnostic for convenience
if (is.function(diagnostics)) diagnostics = list(diagnostics)
dir = get_directory(dir)
desc_data = get_desc_data(dir)
package <- desc_data['Package']
is_base <- package == 'base'
version <- desc_data['Version']
po_dir <- file.path(dir, 'po')
r_potfile <- file.path(po_dir, sprintf("R-%s.pot", package))
src_potfile <- file.path(po_dir, sprintf("%s.pot", if (is_base) 'R' else package))
update = file.exists(po_dir) && (file.exists(r_potfile) || file.exists(src_potfile))
if (verbose) {
if (update) {
# is it worthwhile to try and distinguish the creation time of the
# R pot file and the src pot file? probably not...
messagef(
"Updating translation template for package '%s' (last updated %s)",
package,
get_atime(r_potfile)
)
} else {
messagef("Starting translations for package '%s'", package)
}
}
if (!update) dir.create(po_dir, showWarnings = FALSE)
message_data = get_message_data(dir, custom_translation_functions, verbose=verbose)
if (!nrow(message_data)) {
if (verbose) message('No messages to translate; finishing')
return(invisible())
}
# for testing, we need a connection that stays open so that readLines(n=1L)
# reads successive lines. originally tried passing a test connection as
# an argument to prompt(), but that closed the connection each time -->
# only the first line is ever read.
set_prompt_conn()
on.exit(unset_prompt_conn())
if (verbose) message('Running message diagnostics...')
for (diagnostic in diagnostics) {
diagnostic <- match.fun(diagnostic)
result <- diagnostic(message_data)
if (!nrow(result)) next
show_diagnostic_results(result, diagnostic)
responded_yes = tolower(prompt('Exit now to repair any of these? [y/N]')) %chin% c('y', 'yes')
# length() required for use in batch mode
if (length(responded_yes) && responded_yes) return(invisible())
}
if (verbose) message('Generating .pot files...')
po_params = list(package = package, version = version, copyright = copyright, bugs = bugs)
write_po_files(message_data, po_dir, po_params, template = TRUE, use_base_rules = use_base_rules)
if (l10n_info()[["UTF-8"]]) {
# on UTF-8 machines we install the en@quot messages too
# TODO: streamline this -- en_quote is definitely doing some redundant stuff
message('Generating en@quot translations')
update_en_quot_mo_files(dir, verbose)
}
if (missing(languages)) {
if (verbose) message('No languages provided; finishing')
return(invisible())
}
for (language in languages) {
metadata = get_lang_metadata(language)
po_params$language = language
# overwrite any existing translations written in previous translation.
# set blank initially (rather than deleting the column) to allow
# for interrupting the translation -- if unset, write_po_files will
# fail if both these columns are not yet present.
message_data[type == 'singular', 'msgstr' := ""]
message_data[type == 'plural', 'msgstr_plural' := .(list(rep("", metadata$nplurals)))]
lang_file <- file.path(po_dir, sprintf("R-%s.po", language))
if (update && file.exists(lang_file)) {
if (verbose) {
messagef(
'Found existing R translations for %s (%s/%s) in ./po/%s. Running msgmerge...',
language, metadata$full_name_eng, metadata$full_name_native, basename(lang_file)
)
}
run_msgmerge(lang_file, r_potfile, verbose)
find_fuzzy_messages(message_data, lang_file)
} else {
message_data[message_source == "R", 'fuzzy' := 0L]
}
lang_file <- file.path(po_dir, sprintf("%s.po", language))
if (update && file.exists(lang_file)) {
if (verbose) {
messagef(
'Found existing src translations for %s (%s/%s) in ./po/%s. Running msgmerge...',
language, metadata$full_name_eng, metadata$full_name_native, basename(lang_file)
)
}
run_msgmerge(lang_file, src_potfile, verbose)
find_fuzzy_messages(message_data, lang_file)
} else {
message_data[message_source == "src", 'fuzzy' := 0L]
}
new_idx = message_data[
is_marked_for_translation & (
fuzzy == 1L
| (type == 'singular' & !nzchar(msgstr) & nzchar(msgid, keepNA = TRUE))
| (type == 'plural' & !vapply(msgstr_plural, function(x) all(nzchar(x)), logical(1L)))
),
which = TRUE
]
if (!length(new_idx)) {
if (verbose) messagef('Translations for %s are up to date! Skipping.', language)
next
}
if (verbose) {
messagef(
'Beginning new translations for %s (%s/%s); found %d untranslated messages',
language, metadata$full_name_eng, metadata$full_name_native, length(new_idx)
)
message("(To quit translating, press 'Esc'; progress will be saved)")
}
po_params$author = prompt('Thanks! Who should be credited with these translations?')
po_params$email = prompt('And what is their email?')
# on.exit this to allow ESC to quit mid-translation. the intent is for the
# on.exit command to be overwritten on each iteration over languages --
# only one partially-finished language should be written at a time.
INCOMPLETE = TRUE
on.exit({
if (INCOMPLETE) write_po_files(message_data, po_dir, po_params, use_base_rules = use_base_rules) # nocov
# since add=FALSE, we overwrite the above call; duplicate it here
unset_prompt_conn()
})
if (verbose) {
message(
"***************************\n",
"** BEGINNING TRANSLATION **\n",
"***************************\n\n",
"Some helpful reminders:\n",
" * You can skip a translation by entering nothing (just press RETURN)\n",
" * Special characters (like newlines, \\n, or tabs, \\t) should be written just like that (with an escape)\n",
" * Be sure to match message templates. The count of templates (%s, %d, etc.) must match in all languages, as must initial and terminal newlines (\\n)\n",
" * While the count of templates must match, the _order_ can be changed by using e.g. %2$s to mean 'use the second input as a string here'\n",
" * Whenever templates or escaping is happening in a string, these will be 'highlighted' by carets (^) in the line below"
)
}
# NB: loop over rows to facilitate quitting without losing progress
for (ii in head(new_idx, max_translations)) {
if (message_data$type[ii] == 'plural') {
translation = read_translation(
message_data$msgid_plural[[ii]][1L],
'plural',
message_data$file[ii],
message_data$call[ii],
message_data$fuzzy[ii],
message_data$msgstr_plural[[ii]],
metadata
)
set(message_data, ii, 'msgstr_plural', list(translation))
} else if (!message_data$is_repeat[ii]) {
translation = read_translation(
message_data$msgid[ii],
'singular',
message_data$file[ii],
message_data$call[ii],
message_data$fuzzy[ii],
message_data$msgstr[ii],
metadata
)
set(message_data, ii, 'msgstr', translation)
}
}
# set INCOMPLETE after write_po_files for the event of a process interruption
# between the loop finishing and the write_po_files command executing
write_po_files(message_data, po_dir, po_params, use_base_rules = use_base_rules)
INCOMPLETE = FALSE
}
if (verbose) message('"Installing" translations with msgfmt')
# TODO: reinstate source marker tags, at least for src .pot file & maybe for R .pot file too?
update_mo_files(dir, package, verbose = verbose)
return(invisible())
}
# just here to generate translations. comes from the PLURAL_RANGE_STRINGS csv
# cat(sprintf(
# "invisible({\n%s\n})\n",
# paste(
# sprintf(
# ' gettext("%s")',
# unique(sort(data.table::fread("inst/extdata/plurals_metadata.csv")$range))
# ),
# collapse = "\n"
# )
# ))
invisible({
gettext("independently of n")
gettext("when n = 0")
gettext("when n = 0, 5-20, 25-30, 35-40, ...")
gettext("when n = 0, 5-21, 25-31, 35-41, ...")
gettext("when n = 1")
gettext("when n = 1, 21, 31, 41, ...")
gettext("when n = 100-102, 200-202, 300-302, ...")
gettext("when n = 11-99, 111-199, 211-299, ...")
gettext("when n = 2")
gettext("when n = 2-4, 22-24, 32-34, ...")
gettext("when n = 3-10, 103-110, 203-210, ...")
gettext("when n is 0 or 1")
gettext("when n is at bigger than 1")
gettext("when n is not 1")
})
# take from those present in r-devel:
# ls -1 ~/svn/R-devel/src/library/*/po/*.po | \
# awk -F"[./]" '{print $10}' | \
# sed -r 's/^R(Gui)?-//g' | sort -u | \
# awk '{print " ", $1, " = ,"}'
# alternatively, a more complete list can be found on some websites:
# https://saimana.com/list-of-country-locale-code/
# nplurals,plural info from https://l10n.gnome.org/teams/<language>
# NB: looks may be deceiving for right-to-left scripts (e.g. Farsi), where the
# displayed below might not be in the order it is parsed.
# assign to .potools, not a package env, to keep more readily mutable inside update_metadata()
.potools$KNOWN_LANGUAGES = fread(system.file('extdata', 'language_metadata.csv', package='potools'), key='code')
# the 'plural' column above is designed for computers;
# translate that to something human-legible here.
# NB: 'plural' is 0-based (like in the .po file), but
# 'plural_index' is 1-based (to match the above R-level code).
# assign to .potools, not a package env, to keep more readily mutable inside update_metadata()
.potools$PLURAL_RANGE_STRINGS = fread(
system.file('extdata', 'plurals_metadata.csv', package='potools'),
key = c('plural', 'plural_index')
)
# for testing; unexported
# nocov start
reset_language_metadata = function() {
.potools$KNOWN_LANGUAGES = fread(
system.file('extdata', 'language_metadata.csv', package='potools'),
key='code'
)
.potools$PLURAL_RANGE_STRINGS = fread(
system.file('extdata', 'plurals_metadata.csv', package='potools'),
key = c('plural', 'plural_index')
)
}
# nocov end