2024-06-11 10:51:35 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- encoding: utf-8 -*-
|
2024-06-12 22:21:23 +02:00
|
|
|
#
|
|
|
|
# This script converts the resource strings in Android XML format to Kotlin files for Lyricist.
|
|
|
|
#
|
|
|
|
# Input files are expected to be inside the l10n folder in root of the project and are expected to
|
2024-06-22 13:09:14 +02:00
|
|
|
# be named as follows
|
|
|
|
# l10n/values-xxyy/strings.xml
|
|
|
|
# where xx is the language code and yy is the country code.
|
2024-06-12 22:21:23 +02:00
|
|
|
#
|
2024-06-22 13:09:14 +02:00
|
|
|
# Output files are saved in
|
2024-08-02 19:34:52 -04:00
|
|
|
# core/l10n/src/commonMain/kotlin/com/livefast.eattrash/raccoonforlemmy/core/l10n/messages
|
2024-06-12 22:21:23 +02:00
|
|
|
# and will have the XxYyStrings.kt naming scheme.
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
# python3 convert_xml_to_kt.py <lang_code> <country_code>
|
|
|
|
#
|
2024-06-11 10:51:35 +02:00
|
|
|
|
|
|
|
import sys
|
|
|
|
from bs4 import BeautifulSoup
|
2024-06-12 22:21:23 +02:00
|
|
|
import re
|
2024-06-11 10:51:35 +02:00
|
|
|
|
|
|
|
def read_l10n_from_file(input_path):
|
|
|
|
res = []
|
|
|
|
with open(input_path) as file_handle:
|
|
|
|
soup = BeautifulSoup(file_handle, "xml")
|
|
|
|
elements = soup.find_all("string")
|
|
|
|
for element in elements:
|
|
|
|
k = element["name"]
|
2024-06-12 22:21:23 +02:00
|
|
|
v = unescape(element.string)
|
2024-06-11 10:51:35 +02:00
|
|
|
res.append({"key": k, "value": v})
|
|
|
|
return res
|
|
|
|
|
2024-06-12 22:21:23 +02:00
|
|
|
def unescape(str_xml):
|
|
|
|
str_xml = re.sub(r"\n", "", str_xml)
|
|
|
|
str_xml = str_xml.replace("&", "&")
|
|
|
|
str_xml = str_xml.replace("<", "<")
|
|
|
|
str_xml = str_xml.replace(">", ">")
|
|
|
|
return str_xml
|
|
|
|
|
2024-06-11 14:13:10 +02:00
|
|
|
def write_l10n_to_file(lang_code, country_code, messages, output_path):
|
2024-06-11 10:51:35 +02:00
|
|
|
with open(output_path, "w") as file_handle:
|
2024-08-02 19:34:52 -04:00
|
|
|
file_handle.write("package com.livefast.eattrash.raccoonforlemmy.core.l10n.messages\n")
|
2024-06-11 10:51:35 +02:00
|
|
|
file_handle.write("\n")
|
2024-07-01 22:11:32 +02:00
|
|
|
if lang_code == "en" and len(country_code) == 0:
|
|
|
|
file_handle.write("internal open class DefaultStrings : Strings {\n")
|
|
|
|
for pair in messages:
|
|
|
|
file_handle.write(" override val {0} = \"{1}\"\n".format(pair["key"], pair["value"]))
|
|
|
|
file_handle.write("}\n")
|
|
|
|
else:
|
|
|
|
file_handle.write("internal val {0}{1}Strings =\n".format(lang_code.capitalize(), country_code.capitalize()))
|
|
|
|
file_handle.write(" object : DefaultStrings() {\n")
|
|
|
|
for pair in messages:
|
|
|
|
file_handle.write(" override val {0} = \"{1}\"\n".format(pair["key"], pair["value"]))
|
|
|
|
file_handle.write(" }\n")
|
2024-06-11 10:51:35 +02:00
|
|
|
|
2024-06-11 14:13:10 +02:00
|
|
|
def convert(lang_code, country_code, input_path, output_path):
|
2024-06-11 10:51:35 +02:00
|
|
|
messages = read_l10n_from_file(input_path)
|
2024-06-11 14:13:10 +02:00
|
|
|
write_l10n_to_file(lang_code, country_code, messages, output_path)
|
2024-06-11 10:51:35 +02:00
|
|
|
|
|
|
|
def main():
|
|
|
|
if len(sys.argv) < 2:
|
2024-06-11 14:13:10 +02:00
|
|
|
print("Usage: {0} lang_code country_code".format(sys.argv[0]))
|
2024-06-11 10:51:35 +02:00
|
|
|
return
|
2024-06-11 14:13:10 +02:00
|
|
|
lang_code = sys.argv[1]
|
|
|
|
country_code = ""
|
2024-06-12 22:21:23 +02:00
|
|
|
region_code = ""
|
2024-06-11 14:13:10 +02:00
|
|
|
if len(sys.argv) > 2:
|
|
|
|
country_code = sys.argv[2]
|
2024-06-22 13:09:14 +02:00
|
|
|
source_file = "../l10n/values-{0}{1}/strings.xml".format(lang_code, country_code)
|
2024-07-01 22:11:32 +02:00
|
|
|
if lang_code == "en" and len(country_code) == 0:
|
2024-08-02 19:34:52 -04:00
|
|
|
dest_file = "../core/l10n/src/commonMain/kotlin/com/livefast.eattrash/raccoonforlemmy/core/l10n/messages/DefaultStrings.kt"
|
2024-07-01 22:11:32 +02:00
|
|
|
else:
|
2024-07-14 23:33:23 +02:00
|
|
|
# add support for formats like zh-rCN
|
|
|
|
if country_code.startswith("-r"):
|
|
|
|
country_code = country_code[2:]
|
2024-08-02 19:34:52 -04:00
|
|
|
dest_file = "../core/l10n/src/commonMain/kotlin/com/livefast.eattrash/raccoonforlemmy/core/l10n/messages/{0}{1}Strings.kt".format(lang_code.capitalize(), country_code.capitalize())
|
2024-06-11 14:13:10 +02:00
|
|
|
convert(lang_code, country_code, source_file, dest_file)
|
2024-06-11 10:51:35 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|