|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import subprocess |
| 5 | + |
| 6 | + |
| 7 | +def getLocalesFromLocalazy(): |
| 8 | + command = subprocess.run( |
| 9 | + ["localazy languages --read-key a7876306080832595063-aa37154bb3772f6146890fca868d155b2228b492c56c91f67abdcdfb74d6142d --csv"], |
| 10 | + shell=True, |
| 11 | + capture_output=True, |
| 12 | + text=True, |
| 13 | + ) |
| 14 | + data = command.stdout |
| 15 | + result = [] |
| 16 | + for line in data.split("\n"): |
| 17 | + if line: |
| 18 | + line = line.split(",") |
| 19 | + if (line[6] == "true"): |
| 20 | + result.append(line[0]) |
| 21 | + return sorted(result) |
| 22 | + |
| 23 | + |
| 24 | +def normalizeForResourceConfigurations(locale): |
| 25 | + match locale: |
| 26 | + case "id": |
| 27 | + return "in" |
| 28 | + case "zh_TW#Hant": |
| 29 | + return "zh-rTW" |
| 30 | + case _: |
| 31 | + return locale |
| 32 | + |
| 33 | + |
| 34 | +def normalizeForLocalConfig(locale): |
| 35 | + match locale: |
| 36 | + case "id": |
| 37 | + return "in" |
| 38 | + case "zh_TW#Hant": |
| 39 | + return "zh-TW" |
| 40 | + case _: |
| 41 | + return locale |
| 42 | + |
| 43 | + |
| 44 | +def generateLocaleFile(locales, file): |
| 45 | + with open("plugins/src/main/kotlin/extension/locales.kt", "w") as f: |
| 46 | + f.write("// File generated by " + file + ", do not edit\n\n") |
| 47 | + f.write("package extension\n\n") |
| 48 | + f.write("val locales = setOf(\n") |
| 49 | + for locale in locales: |
| 50 | + f.write(" \"" + normalizeForResourceConfigurations(locale) + "\",\n") |
| 51 | + f.write(")\n") |
| 52 | + |
| 53 | + |
| 54 | +def generateLocalesConfigFile(locales, file): |
| 55 | + with open("app/src/main/res/xml/locales_config.xml", "w") as f: |
| 56 | + f.write("<!-- File generated by " + file + ", do not edit -->\n") |
| 57 | + f.write('<locale-config xmlns:android="http://schemas.android.com/apk/res/android">\n') |
| 58 | + for locale in locales: |
| 59 | + f.write(" <locale android:name=\"" + normalizeForLocalConfig(locale) + "\"/>\n") |
| 60 | + f.write("</locale-config>\n") |
| 61 | + |
| 62 | + |
| 63 | +def main(): |
| 64 | + file = os.path.basename(__file__) |
| 65 | + locales = getLocalesFromLocalazy() |
| 66 | + generateLocaleFile(locales, file) |
| 67 | + generateLocalesConfigFile(locales, file) |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == "__main__": |
| 71 | + main() |
0 commit comments