Skip to content

Commit d72462d

Browse files
authored
Improve update-locales script and fix locale processing bug (#23240)
The locales of Gitea has been broken for long time, till now, it's still not fully fixed. One of the root problems is that the `ini` library is quite quirky and the `update-locales` script doesn't work well for all cases. This PR fixes the `update-locales` script to make it satisfy `ini` library and the crowdin. See the comments for more details. The `locale_zh-CN.ini` is an example, it comes from crowdin and is processed by the new `update-locales.sh`. Especially see the `feed_of`: https://github.com/go-gitea/gitea/pull/23240/files#diff-321f6ca4eae1096eba230e93c4740f9903708afe8d79cf2e57f4299786c4528bR268
1 parent ce73492 commit d72462d

File tree

2 files changed

+225
-13
lines changed

2 files changed

+225
-13
lines changed

build/update-locales.sh

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,49 @@
1-
#!/bin/sh
1+
#!/bin/bash
2+
3+
set -e
4+
5+
SED=sed
6+
7+
if [[ $OSTYPE == 'darwin'* ]]; then
8+
# for macOS developers, use "brew install gnu-sed"
9+
SED=gsed
10+
fi
11+
12+
if [ ! -f ./options/locale/locale_en-US.ini ]; then
13+
echo "please run this script in the root directory of the project"
14+
exit 1
15+
fi
216

317
mv ./options/locale/locale_en-US.ini ./options/
418

5-
# Make sure to only change lines that have the translation enclosed between quotes
6-
sed -i -r -e '/^[a-zA-Z0-9_.-]+[ ]*=[ ]*".*"$/ {
7-
s/^([a-zA-Z0-9_.-]+)[ ]*="/\1=/
8-
s/\\"/"/g
19+
# the "ini" library for locale has many quirks
20+
# * `a="xx"` gets `xx` (no quote)
21+
# * `a=x\"y` gets `x\"y` (no unescaping)
22+
# * `a="x\"y"` gets `"x\"y"` (no unescaping, the quotes are still there)
23+
# * `a='x\"y'` gets `x\"y` (no unescaping, no quote)
24+
# * `a="foo` gets `"foo` (although the quote is not closed)
25+
# * 'a=`foo`' works like single-quote
26+
# crowdin needs the strings to be quoted correctly and doesn't like incomplete quotes
27+
# crowdin always outputs quoted strings if there are quotes in the strings.
28+
29+
# this script helps to unquote the crowdin outputs for the quirky ini library
30+
# * find all `key="...\"..."` lines
31+
# * remove the leading quote
32+
# * remove the trailing quote
33+
# * unescape the quotes
34+
# * eg: key="...\"..." => key=..."...
35+
$SED -i -r -e '/^[-.A-Za-z0-9_]+[ ]*=[ ]*".*"$/ {
36+
s/^([-.A-Za-z0-9_]+)[ ]*=[ ]*"/\1=/
937
s/"$//
38+
s/\\"/"/g
1039
}' ./options/locale/*.ini
1140

41+
# * if the escaped line is incomplete like `key="...` or `key=..."`, quote it with backticks
42+
# * eg: key="... => key=`"...`
43+
# * eg: key=..." => key=`..."`
44+
$SED -i -r -e 's/^([-.A-Za-z0-9_]+)[ ]*=[ ]*(".*[^"])$/\1=`\2`/' ./options/locale/*.ini
45+
$SED -i -r -e 's/^([-.A-Za-z0-9_]+)[ ]*=[ ]*([^"].*")$/\1=`\2`/' ./options/locale/*.ini
46+
1247
# Remove translation under 25% of en_us
1348
baselines=$(wc -l "./options/locale_en-US.ini" | cut -d" " -f1)
1449
baselines=$((baselines / 4))

0 commit comments

Comments
 (0)