-
Notifications
You must be signed in to change notification settings - Fork 17
/
update_translations.sh
executable file
·76 lines (65 loc) · 4.13 KB
/
update_translations.sh
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
#!/bin/bash
# Creates required locale files for the extension project (firefox/chrome)
# Requirements:
# pybabel
# gettext
PROJECT_NAME=webpg
BUGSADDR=$PROJECT_NAME-translations@$PROJECT_NAME.org
ORGANIZATION=CURETHEITCH
AUTHOR="Kyle L. Huff"
SRCDIR=extension
PODIR=po
POTFILE=$PROJECT_NAME.pot
# Obtain the project version number and set the locale directory
PROJECT=$PROJECT_NAME
VERSION=`sed -n 's/.*\"version\"\:.\"\(.*\)\",.*/\1/p' extension/manifest.json`
LOCALES=('ar' 'bg' 'ca' 'ca' 'cs' 'da' 'de' 'el' 'en' 'en_GB' 'en_US' 'es' 'es_419' 'et' 'fi' 'fil' 'fr' 'he' 'hi' 'hr' 'hu' 'id' 'it' 'ja' 'ko' 'lt' 'lv' 'nl' 'np' 'pl' 'pt_BR' 'pt_PT' 'ro' 'ru' 'sk' 'sl' 'sr' 'sv' 'th' 'tr' 'uk' 'vi' 'zh_CN' 'zh_TW')
# Create the po directory if it does not exits
if [ ! -d "$PODIR" ]; then
mkdir $PODIR
fi
# Extract the translatable strings from the source files
pybabel extract -F $PROJECT_NAME-babel.cfg --copyright-holder=$ORGANIZATION --msgid-bugs-address=$BUGSADDR --project=$PROJECT --version=$VERSION $SRCDIR > $PODIR/$POTFILE
# Extract the translatable strings from the XUL files
gawk 'match($0, /label=\"&(.*)\;\"/, a) {if (!x[a[1]]++) printf("#: %s:%d\nmsgid \"%s\"\nmsgstr \"\"\n\n", FILENAME, NR, a[1])}' extension/XULContent/firefoxOverlay.xul extension/XULContent/thunderbird/composeOverlay.xul extension/XULContent/thunderbird/thunderbirdOverlay.xul >> $PODIR/$POTFILE
# Extract the translatable strings from the mainifes.json file
gawk 'match($0, /__MSG_(.*)__/, a) {printf("#: %s:%d\nmsgid \"%s\"\nmsgstr \"\"\n\n", FILENAME, NR, a[1])}' extension/manifest.json >> $PODIR/$POTFILE
# Iterate through the configured locales and create the necessary files
for locale in "${LOCALES[@]}"
do
if [ -f $PODIR/$locale.po ]; then
echo "Updating locale: $locale"
msgmerge --update --no-fuzzy-matching --backup=off $PODIR/$locale.po $PODIR/$POTFILE
else
echo "Creating new locale: $locale"
msginit --no-translator -l $locale -i $PODIR/$POTFILE -o $PODIR/$locale.po
fi
done
for locale in "${LOCALES[@]}"
do
if [ -f $PODIR/$locale.po ]; then
# Create the firefox i18n locale files in DTN format
LOCALEDIR="firefox/locale"
if [ ! -d "$LOCALEDIR/$locale" ]; then
mkdir -p $LOCALEDIR/$locale
fi
echo "Creating $LOCALEDIR/$locale/$PROJECT_NAME.dtd"
gawk 'BEGIN{ FS="\n"; RS="";} match($0, /msgid.*(\"(.*)\")$/, b) && match(b[0], /msgid.*(\"(.*)\")(\n)?msgstr.*(\".*\")(\n)?/, a) { if (a[2]!="") printf("<!ENTITY %s %s>\n", gensub(/([[:punct:]|[:space:]])/, "_", "g", a[2]), a[4]) }' $PODIR/$locale.po > $LOCALEDIR/$locale/$PROJECT_NAME.dtd
echo "Creating $LOCALEDIR/$locale/$PROJECT_NAME.properties"
gawk 'BEGIN{ FS="\n"; RS="";} match($0, /msgid.*(\"(.*)\")$/, b) && match(b[0], /msgid.*(\"(.*)\")(\n)?msgstr.*(\"(.*)\")(\n)?/, a) { if (a[2]!="") printf("%s=%s\n", gensub(/([[:punct:]|[:space:]])/, "_", "g", a[2]), a[5]) }' $PODIR/$locale.po > $LOCALEDIR/$locale/$PROJECT_NAME.properties
# Create the chrome i18n locale files in JSON format [i.e. _locales/en_US/messages.json]
LOCALEDIR="chrome/_locales"
if [ ! -d "$LOCALEDIR/$locale" ]; then
mkdir -p $LOCALEDIR/$locale
fi
echo "Creating $LOCALEDIR/$locale/messages.json"
echo -e "{" > $LOCALEDIR/$locale/messages.json
gawk 'BEGIN{ FS="\n"; RS=""; } match($0, /msgid.*(\"(.*)\")$/, b) && match(b[0], /msgid.*(\"(.*)\")(\n)?msgstr.*(\"(.*)\")(\n)?/, a) { if (a[2]!="") printf("\t\"%s\": {\n\t\t\"message\": %s\n\t},\n", gensub(/([[:punct:]|[:space:]])/, "_", "g", a[2]), a[4]) }' $PODIR/$locale.po >> $LOCALEDIR/$locale/messages.json
echo -e "\t\"empty_end\": {\n\t\t\"message\": \"N/A\",\n\t\t\"description\": \"Just an empty item to mark our end; No need to translate.\"\n\t}\n}" >> $LOCALEDIR/$locale/messages.json
fi
done
# Create the file POTFILES.in
echo "Creating POTFILES.in"
echo -e "# List of source files containing translatable strings." > $PODIR/POTFILES.in
gawk 'BEGIN{ FS="\n"; RS="";} match($1, /#: (.*[:])/, a) split(a[1], x, ":") {print x[1]}' $PODIR/$POTFILE | awk '!($0 in x){x[$0];print}' | sort >> $PODIR/POTFILES.in
echo ".. done."