You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
1.8 KiB
Python
73 lines
1.8 KiB
Python
#!/bin/python3
|
|
import re
|
|
|
|
OUTPUT_FILE = "./ts/localization/locales.ts"
|
|
|
|
|
|
def wrapValue(value):
|
|
"""
|
|
Wraps the given value in single quotes if it contains any characters other than letters, digits, or underscores.
|
|
|
|
Args:
|
|
value (str): The value to be wrapped.
|
|
|
|
Returns:
|
|
str: The wrapped value if it contains any special characters, otherwise the original value.
|
|
"""
|
|
if re.search(r"[^a-zA-Z0-9_]", value):
|
|
return f"'{value}'"
|
|
return value
|
|
|
|
|
|
def parseValue(value):
|
|
"""
|
|
Parses the given value by replacing single quotes with escaped single quotes.
|
|
|
|
Args:
|
|
value (str): The value to be parsed.
|
|
|
|
Returns:
|
|
str: The parsed value with escaped single quotes.
|
|
"""
|
|
return value.replace("'", "\\'")
|
|
|
|
|
|
def generate_js_object(data):
|
|
"""
|
|
Generate a JavaScript object from a dictionary.
|
|
|
|
Args:
|
|
data (dict): The dictionary containing key-value pairs.
|
|
|
|
Returns:
|
|
str: A string representation of the JavaScript object.
|
|
"""
|
|
js_object = "{\n"
|
|
for key, value in data.items():
|
|
js_object += f" {wrapValue(key)}: '{parseValue(value)}',\n"
|
|
js_object += "}"
|
|
return js_object
|
|
|
|
|
|
DISCLAIMER = """
|
|
// This file was generated by a script. Do not modify this file manually.
|
|
// To make changes, modify the corresponding JSON file and re-run the script.
|
|
|
|
"""
|
|
|
|
|
|
def generateLocalesType(locale):
|
|
"""
|
|
Generate the locales type and write it to a file.
|
|
|
|
Args:
|
|
locale: The locale dictionary containing the localization data.
|
|
"""
|
|
# write the locale_dict to a file
|
|
with open(OUTPUT_FILE, "w", encoding='utf-8') as ts_file:
|
|
ts_file.write(
|
|
f"{DISCLAIMER}"
|
|
f"export const en = {generate_js_object(locale)} as const;\nexport type Dictionary = typeof en;"
|
|
)
|
|
return f"Locales generated at: {OUTPUT_FILE}"
|