Skip to content

Commit

Permalink
Merge pull request #66 from OmarAI2003/improve-android-convert-str-to…
Browse files Browse the repository at this point in the history
…-json

Improve android convert str to json
  • Loading branch information
andrewtavis authored Oct 9, 2024
2 parents a3eeda1 + b774027 commit 45bce87
Showing 1 changed file with 41 additions and 10 deletions.
51 changes: 41 additions & 10 deletions Scribe-i18n/scripts/Android/convert_strings_to_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,58 @@ def unescape_special_characters(string):


directory = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
dir_list = os.listdir(os.path.join(directory, "jsons"))

# Define the path to the "jsons" folder.
jsons_folder = os.path.join(directory, "jsons")

if not os.path.exists(jsons_folder):
print(f"Error: The folder '{jsons_folder}' does not exist. Please ensure the path is correct.")
exit(1)

dir_list = os.listdir(jsons_folder)
languages = sorted(
[file.replace(".json", "") for file in dir_list if file.endswith(".json")]
)
regex = re.compile(r'<string name="(.*?)">(.*?)</string>', re.DOTALL)

values_directory = os.path.join(directory, "values")
if not os.path.exists(values_directory):
print(f"Error: The folder '{values_directory}' does not exist. Please ensure the path is correct.")
exit(1)

for lang in languages:
path = os.path.join(values_directory, lang)
with open(f"{path}/string.xml", "r") as file:
content = file.read()
try:
with open(f"{path}/string.xml", "r") as file:
content = file.read()

except FileNotFoundError:
print(f"Error: {path}/string.xml file not found.")
exit(1)

except Exception as e:
print(f"Error: An unexpected error occurred while writing to ' {path}/string.xml: {e}")
exit(1)

matches = regex.findall(content)
result = dict(matches)
result = {key: unescape_special_characters(value) for key, value in result.items()}
with open(
os.path.join(os.path.join(directory, "jsons"), f"{lang}.json"),
"w",
encoding="utf-8",
) as file:
json.dump(result, file, indent=2, ensure_ascii=False)
file.write("\n")
try:
with open(
os.path.join(jsons_folder, f"{lang}.json"),
"w",
encoding="utf-8",
) as file:
json.dump(result, file, indent=2, ensure_ascii=False)
file.write("\n")

except FileNotFoundError:
print(f"Error: The folder '{jsons_folder}' does not exist or cannot be accessed for writing.")
exit(1)

except Exception as e:
print(f"Error: An unexpected error occurred while writing to '{jsons_folder}/{lang}.json: {e}")
exit(1)

print(
"Scribe-i18n localization strings files successfully converted to the JSON files."
Expand Down

0 comments on commit 45bce87

Please sign in to comment.