Generate desktop and AppData files with python script

This commit is contained in:
Benau 2021-02-26 16:08:15 +08:00
parent 16c52a693e
commit 460c95fb66
2 changed files with 228 additions and 185 deletions

View File

@ -0,0 +1,222 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import shutil
import sys
def traslate_po(po, translation):
search = 'msgid "' + translation + '"'
idx = po.find(search)
if idx == -1:
return ''
# 9 characters after msgid "xxx" for the translated message
begin = idx + len(search) + 9
# Search next " with newline
end = po.find('"\n', begin)
if end == -1:
return ''
return po[begin : end]
STK_DESCRIPTION = 'A 3D open-source kart racing game'
STK_DESKTOP_FILE_P1 = """[Desktop Entry]
"""
# Split it to avoid SuperTuxKart being translated
STK_DESKTOP_FILE_P2 = """Name=SuperTuxKart
Icon=supertuxkart
"""
STK_DESKTOP_FILE_P3 = """#I18N: Generic name in desktop file entry, summary in AppData and short description in Google Play
GenericName=""" + STK_DESCRIPTION + """
Exec=supertuxkart
Terminal=false
StartupNotify=false
Type=Application
Categories=Game;ArcadeGame;
#I18N: Keywords in desktop entry, translators please keep it separated with semicolons
Keywords=tux;game;race;
PrefersNonDefaultGPU=true
"""
desktop_file = open('supertuxkart.desktop', 'w')
desktop_file.write(STK_DESKTOP_FILE_P1 + STK_DESKTOP_FILE_P3)
desktop_file.close()
STK_APPDATA_P1 = 'Karts. Nitro. Action! SuperTuxKart is a 3D open-source arcade racer \
with a variety of characters, tracks, and modes to play. \
Our aim is to create a game that is more fun than realistic, \
and provide an enjoyable experience for all ages.'
STK_APPDATA_P2 = 'Discover the mystery of an underwater world, \
or drive through the jungles of Val Verde and visit the famous Cocoa Temple. \
Race underground or in a spaceship, through a rural farmland or a strange alien planet. \
Or rest under the palm trees on the beach, watching the other karts overtake you. \
But don\'t eat the bananas! Watch for bowling balls, plungers, bubble gum, \
and cakes thrown by your opponents.'
STK_APPDATA_P3 = 'You can do a single race against other karts, \
compete in one of several Grand Prix, \
try to beat the high score in time trials on your own, \
play battle mode against the computer or your friends, \
and more! For a greater challenge, race online against players from all over the world \
and prove your racing skills!'
# Used in google play only for now
STK_APPDATA_P4 = 'This game is free and without ads.'
# Used in google play beta only for now
STK_APPDATA_P5 = 'This is an unstable version of SuperTuxKart that contains latest improvements. \
It is released mainly for testing, to make stable STK as good as possible.'
STK_APPDATA_P6 = 'This version can be installed in parallel with the stable version on the device.'
STK_APPDATA_P7 = 'If you need more stability, consider using the stable version: %s'
STK_STABLE_URL = 'https://play.google.com/store/apps/details?id=org.supertuxkart.stk'
STK_APPDATA_FILE_1 = """<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<component type=\"desktop\">
<id>supertuxkart.desktop</id>
<metadata_license>CC0-1.0</metadata_license>
<project_license>GPL-3.0+</project_license>
"""
# Split it to avoid SuperTuxKart being translated
STK_APPDATA_FILE_2 = """ <name>SuperTuxKart</name>
"""
STK_APPDATA_FILE_3 = """ <summary>""" + STK_DESCRIPTION + """</summary>
<description>
<p>
""" + STK_APPDATA_P1 + """
</p>
<p>
""" + STK_APPDATA_P2 + """
</p>
<p>
""" + STK_APPDATA_P3 + """
</p>
"""
STK_APPDATA_FILE_4 = """ <p>
""" + STK_APPDATA_P4 + """
</p>
<p>
""" + STK_APPDATA_P5 + """
</p>
<p>
""" + STK_APPDATA_P6 + """
</p>
<p>
""" + STK_APPDATA_P7 + """
</p>
"""
STK_APPDATA_FILE_5 = """ </description>
<screenshots>
<screenshot type=\"default\">
<image>https://supertuxkart.net/images/8/83/Supertuxkart-0.9.2-screenshot-3.jpg</image>
<caption>Normal Race</caption>
</screenshot>
<screenshot>
<image>https://supertuxkart.net/images/1/1f/Supertuxkart-0.9.2-screenshot-1.jpg</image>
<caption>Battle</caption>
</screenshot>
<screenshot>
<image>https://supertuxkart.net/images/2/2a/Supertuxkart-0.9.2-screenshot-2.jpg</image>
<caption>Soccer</caption>
</screenshot>
</screenshots>
<developer_name>SuperTuxKart Team</developer_name>
<update_contact>supertuxkart-devel@lists.sourceforge.net</update_contact>
<url type=\"homepage\">https://supertuxkart.net</url>
<url type=\"bugtracker\">https://github.com/supertuxkart/stk-code/issues</url>
<url type=\"donation\">https://supertuxkart.net/Donate</url>
<url type=\"help\">https://supertuxkart.net/Community</url>
<url type=\"translate\">https://supertuxkart.net/Translating_STK</url>
<content_rating type=\"oars-1.1\">
<content_attribute id=\"violence-cartoon\">mild</content_attribute>
<content_attribute id=\"social-chat\">intense</content_attribute>
</content_rating>
<languages>
"""
STK_APPDATA_FILE_6 = """ </languages>
</component>
"""
appdata_file = open('supertuxkart.appdata.xml', 'w')
appdata_file.write(STK_APPDATA_FILE_1 + STK_APPDATA_FILE_3 + STK_APPDATA_FILE_4 \
+ STK_APPDATA_FILE_5 + STK_APPDATA_FILE_6)
appdata_file.close()
os.system('xgettext -j -d supertuxkart --add-comments=\"I18N:\" \
-p ./data/po -o supertuxkart.pot \
--package-name=supertuxkart supertuxkart.desktop supertuxkart.appdata.xml')
desktop_file = open('supertuxkart.desktop', 'w')
desktop_file.write(STK_DESKTOP_FILE_P1 + STK_DESKTOP_FILE_P2 + STK_DESKTOP_FILE_P3)
desktop_file.close()
appdata = STK_APPDATA_FILE_1 + STK_APPDATA_FILE_2 + STK_APPDATA_FILE_3
# Skip google play message
appdata += STK_APPDATA_FILE_5
# Manually copy zh_TW to zh_HK for fallback
shutil.copyfile('./data/po/zh_TW.po', './data/po/zh_HK.po')
shutil.rmtree('./google_play_msg', ignore_errors = True)
lingas = open('./data/po/LINGUAS', 'w')
po_list = [f for f in os.listdir('./data/po/') if f.endswith('.po')]
po_list.sort(reverse = False);
fr_percentage = 0
for po_filename in po_list:
po_file = open('./data/po/' + po_filename, 'r')
po = po_file.read()
po_file.close()
# Remove all newlines in msgid
po = po.replace('"\n"', '')
cur_lang = po_filename.removesuffix('.po')
if cur_lang != 'en':
lingas.write(cur_lang + '\n')
total_str = po.count('msgid "')
untranslated_str = po.count('msgstr ""')
translated_str = total_str - untranslated_str
percentage = int(translated_str / total_str * 100.0)
# Special handling for fr_CA, list has been sorted
if cur_lang == 'fr':
fr_percentage = percentage
elif cur_lang == 'fr_CA':
percentage = fr_percentage
if percentage == 0:
continue
elif percentage != 100:
appdata += ' <lang percentage="' + str(percentage) + '">' + cur_lang +'</lang>\n'
else:
appdata += ' <lang>' + cur_lang +'</lang>\n'
if cur_lang != 'fr_CA' and len(sys.argv) == 2 and sys.argv[1] == '--generate-google-play-msg':
desc = traslate_po(po, STK_DESCRIPTION)
p1 = traslate_po(po, STK_APPDATA_P1)
p2 = traslate_po(po, STK_APPDATA_P2)
p3 = traslate_po(po, STK_APPDATA_P3)
p4 = traslate_po(po, STK_APPDATA_P4)
p5 = traslate_po(po, STK_APPDATA_P5)
p6 = traslate_po(po, STK_APPDATA_P6)
p7 = traslate_po(po, STK_APPDATA_P7)
if desc and p1 and p2 and p3 and p4 and p5 and p6 and p7:
os.makedirs('./google_play_msg/' + cur_lang)
p7 = p7.replace('%s', STK_STABLE_URL)
short = open('./google_play_msg/' + cur_lang + '/short.txt', 'w')
short.write(desc)
short.close()
full = open('./google_play_msg/' + cur_lang + '/full.txt', 'w')
full.write(p1 + '\n\n' + p2 + '\n\n' + p3 + '\n\n' + p4)
full.close()
full_beta = open('./google_play_msg/' + cur_lang + '/full_beta.txt', 'w')
full_beta.write(p1 + '\n\n' + p2 + '\n\n' + p3 + '\n\n' + p4 +
'\n\n---\n\n' + p5 + '\n\n' + p6 + '\n\n' + p7)
full_beta.close()
lingas.close()
appdata += STK_APPDATA_FILE_6
appdata_file = open('supertuxkart.appdata.xml', 'w')
appdata_file.write(appdata)
appdata_file.close()
os.system('msgfmt --desktop -d data/po --template supertuxkart.desktop -o data/supertuxkart.desktop')
os.system('msgfmt --xml -d data/po --template supertuxkart.appdata.xml -o data/supertuxkart.appdata.xml')
os.remove('./supertuxkart.desktop')
os.remove('./supertuxkart.appdata.xml')
os.remove('./data/po/LINGUAS')
os.remove('./data/po/zh_HK.po')

View File

@ -70,191 +70,12 @@ xgettext -j -d supertuxkart --keyword="translate" --add-comments="I18N:" \
-p ./data/po -o supertuxkart.pot $ANGELSCRIPT_FILE_LIST \
--package-name=supertuxkart --language=c++
STK_DESCRIPTION="A 3D open-source kart racing game"
STK_DESKTOP_FILE_P1="[Desktop Entry]"
# Split it to avoid SuperTuxKart being translated
STK_DESKTOP_FILE_P2="Name=SuperTuxKart
Icon=supertuxkart"
STK_DESKTOP_FILE_P3="#I18N: Generic name in desktop file entry, \
summary in AppData and short description in Google Play
GenericName=$STK_DESCRIPTION
Exec=supertuxkart
Terminal=false
StartupNotify=false
Type=Application
Categories=Game;ArcadeGame;
#I18N: Keywords in desktop entry, translators please keep it separated with semicolons
Keywords=tux;game;race;
PrefersNonDefaultGPU=true"
echo "${STK_DESKTOP_FILE_P1}" > supertuxkart.desktop
echo "${STK_DESKTOP_FILE_P3}" >> supertuxkart.desktop
STK_APPDATA_P1="Karts. Nitro. Action! SuperTuxKart is a 3D open-source arcade racer \
with a variety of characters, tracks, and modes to play. \
Our aim is to create a game that is more fun than realistic, \
and provide an enjoyable experience for all ages."
STK_APPDATA_P2="Discover the mystery of an underwater world, \
or drive through the jungles of Val Verde and visit the famous Cocoa Temple. \
Race underground or in a spaceship, through a rural farmland or a strange alien planet. \
Or rest under the palm trees on the beach, watching the other karts overtake you. \
But don't eat the bananas! Watch for bowling balls, plungers, bubble gum, \
and cakes thrown by your opponents."
STK_APPDATA_P3="You can do a single race against other karts, \
compete in one of several Grand Prix, \
try to beat the high score in time trials on your own, \
play battle mode against the computer or your friends, \
and more! For a greater challenge, race online against players from all over the world \
and prove your racing skills!"
# Used in google play only for now
STK_APPDATA_P4="This game is free and without ads."
# Used in google play beta only for now
STK_APPDATA_P5="This is an unstable version of SuperTuxKart that contains latest improvements. \
It is released mainly for testing, to make stable STK as good as possible."
STK_APPDATA_P6="This version can be installed in parallel with the stable version on the device."
STK_APPDATA_P7="If you need more stability, consider using the stable version: %s"
STK_STABLE_URL="https://play.google.com/store/apps/details?id=org.supertuxkart.stk"
STK_APPDATA_FILE_1="<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<component type=\"desktop\">
<id>supertuxkart.desktop</id>
<metadata_license>CC0-1.0</metadata_license>
<project_license>GPL-3.0+</project_license>"
# Split it to avoid SuperTuxKart being translated
STK_APPDATA_FILE_2=" <name>SuperTuxKart</name>"
STK_APPDATA_FILE_3=" <summary>A 3D open-source kart racing game</summary>
<description>
<p>
"${STK_APPDATA_P1}"
</p>
<p>
"${STK_APPDATA_P2}"
</p>
<p>
"${STK_APPDATA_P3}"
</p>"
STK_APPDATA_FILE_4=" <p>
$STK_APPDATA_P4
</p>
<p>
$STK_APPDATA_P5
</p>
<p>
$STK_APPDATA_P6
</p>
<p>
$STK_APPDATA_P7
</p>"
STK_APPDATA_FILE_5=" </description>
<screenshots>
<screenshot type=\"default\">
<image>https://supertuxkart.net/images/8/83/Supertuxkart-0.9.2-screenshot-3.jpg</image>
<caption>Normal Race</caption>
</screenshot>
<screenshot>
<image>https://supertuxkart.net/images/1/1f/Supertuxkart-0.9.2-screenshot-1.jpg</image>
<caption>Battle</caption>
</screenshot>
<screenshot>
<image>https://supertuxkart.net/images/2/2a/Supertuxkart-0.9.2-screenshot-2.jpg</image>
<caption>Soccer</caption>
</screenshot>
</screenshots>
<developer_name>SuperTuxKart Team</developer_name>
<update_contact>supertuxkart-devel@lists.sourceforge.net</update_contact>
<url type=\"homepage\">https://supertuxkart.net</url>
<url type=\"bugtracker\">https://github.com/supertuxkart/stk-code/issues</url>
<url type=\"donation\">https://supertuxkart.net/Donate</url>
<url type=\"help\">https://supertuxkart.net/Community</url>
<url type=\"translate\">https://supertuxkart.net/Translating_STK</url>
<content_rating type=\"oars-1.1\">
<content_attribute id=\"violence-cartoon\">mild</content_attribute>
<content_attribute id=\"social-chat\">intense</content_attribute>
</content_rating>
<languages>"
STK_APPDATA_FILE_6=" </languages>
</component>"
echo "${STK_APPDATA_FILE_1}" > supertuxkart.appdata.xml
echo "${STK_APPDATA_FILE_3}" >> supertuxkart.appdata.xml
echo "${STK_APPDATA_FILE_4}" >> supertuxkart.appdata.xml
echo "${STK_APPDATA_FILE_5}" >> supertuxkart.appdata.xml
echo "${STK_APPDATA_FILE_6}" >> supertuxkart.appdata.xml
# Desktop and AppData entry
xgettext -j -d supertuxkart --add-comments="I18N:" \
-p ./data/po -o supertuxkart.pot \
--package-name=supertuxkart supertuxkart.desktop supertuxkart.appdata.xml
echo "${STK_DESKTOP_FILE_P1}" > supertuxkart.desktop
echo "${STK_DESKTOP_FILE_P2}" >> supertuxkart.desktop
echo "${STK_DESKTOP_FILE_P3}" >> supertuxkart.desktop
echo "${STK_APPDATA_FILE_1}" > supertuxkart.appdata.xml
echo "${STK_APPDATA_FILE_2}" >> supertuxkart.appdata.xml
echo "${STK_APPDATA_FILE_3}" >> supertuxkart.appdata.xml
# Skip google play message
echo "${STK_APPDATA_FILE_5}" >> supertuxkart.appdata.xml
# Manually copy zh_TW to zh_HK for fallback
cp data/po/zh_TW.po data/po/zh_HK.po
rm -rf ./google_play_msg
function translate_str()
{
# Remove newline in msgid of po file first
echo $(sed ':a;N;$!ba;s/\"\n\"//g' "$2" \
| grep -A 1 -e "msgid \"$1\"" | tail -n +2 | cut -c 9- |sed 's/.$//')
}
for PO in $(ls data/po/*.po); do
CUR_LANG=$(basename $PO .po)
if [ "$CUR_LANG" != "en" ]; then
printf "$CUR_LANG " >> data/po/LINGUAS
PO_NO_FALLBACK=$PO
if [ "$CUR_LANG" = "fr_CA" ]; then
PO_NO_FALLBACK="data/po/fr.po"
fi
TOTAL_STR=$(sed ':a;N;$!ba;s/\"\n\"//g' $PO_NO_FALLBACK | grep "msgid \"" | wc -l)
UNTRANSLATED_STR=$(sed ':a;N;$!ba;s/\"\n\"//g' $PO_NO_FALLBACK | grep "msgstr \"\"" | wc -l)
TRANSLATED_STR=$(expr $TOTAL_STR - $UNTRANSLATED_STR)
PERCENTAGE=$(python -c "print(int($TRANSLATED_STR / $TOTAL_STR * 100.0))")
if [ "$PERCENTAGE" = "0" ]; then
continue
elif [ "$PERCENTAGE" != "100" ]; then
printf " <lang percentage=\"$PERCENTAGE\">$CUR_LANG</lang>" >> supertuxkart.appdata.xml
else
printf " <lang>$CUR_LANG</lang>" >> supertuxkart.appdata.xml
fi
fi
if [ "$1" != "--generate-google-play-msg" ]; then
continue
fi
DESC=$(translate_str "$STK_DESCRIPTION" "$PO")
P1=$(translate_str "$STK_APPDATA_P1" "$PO")
P2=$(translate_str "$STK_APPDATA_P2" "$PO")
P3=$(translate_str "$STK_APPDATA_P3" "$PO")
P4=$(translate_str "$STK_APPDATA_P4" "$PO")
P5=$(translate_str "$STK_APPDATA_P5" "$PO")
P6=$(translate_str "$STK_APPDATA_P6" "$PO")
P7=$(translate_str "$STK_APPDATA_P7" "$PO")
if [ -n "$DESC" ] && [ -n "$P1" ] && [ -n "$P2" ] && [ -n "$P3" ] && \
[ -n "$P4" ] && [ -n "$P5" ] && [ -n "$P6" ] && [ -n "$P7" ]; then
mkdir -p ./google_play_msg/$CUR_LANG
P7=$(sed "s|%s|$STK_STABLE_URL|g" <<< $P7)
printf "$DESC" > google_play_msg/$CUR_LANG/short.txt
printf "$P1\n\n$P2\n\n$P3\n\n$P4" > google_play_msg/$CUR_LANG/full.txt
printf "$P1\n\n$P2\n\n$P3\n\n$P4\n\n---\n\n$P5\n\n$P6\n\n$P7" > google_play_msg/$CUR_LANG/full_beta.txt
fi
done
echo "${STK_APPDATA_FILE_6}" >> supertuxkart.appdata.xml
msgfmt --desktop -d data/po --template supertuxkart.desktop -o data/supertuxkart.desktop
msgfmt --xml -d data/po --template supertuxkart.appdata.xml -o data/supertuxkart.appdata.xml
rm -f ./supertuxkart.desktop
rm -f ./supertuxkart.appdata.xml
rm -f ./data/po/LINGUAS
rm -f ./data/po/zh_HK.po
# Desktop file and AppData
if [ "$1" = "--generate-google-play-msg" ]; then
data/po/update_desktop_file_appdata.py "--generate-google-play-msg"
else
data/po/update_desktop_file_appdata.py
fi
echo " Done"
echo "---------------------------"