|
| 1 | +# This script is used to find untranslated strings for a particular language. |
| 2 | +# |
| 3 | +# It takes three arguments: |
| 4 | +# 1. index_file.json - A JSON file containing the mapping of keys to files (usually the bmaindex.json file) |
| 5 | +# 2. english_strings.json - A JSON file containing the English strings. |
| 6 | +# 3. partial_translation.json - A JSON file containing the translated strings of a particular language |
| 7 | +# |
| 8 | +# The script has to be executed in the moodle-local_moodlemobile app repo, in the most recent langpack_X branch. |
| 9 | +# The script will output the untranslated strings to a JSON file (output/ directory) and also create PHP files for the untranslated strings. |
| 10 | +# Those PHP files can be used to be translated and then imported back to the Moodle language site. |
| 11 | + |
| 12 | +import json |
| 13 | +import sys |
| 14 | +import os |
| 15 | + |
| 16 | +def load_json(file_path): |
| 17 | + """Load a JSON file and return its content.""" |
| 18 | + try: |
| 19 | + with open(file_path, 'r', encoding='utf-8') as file: |
| 20 | + return json.load(file) |
| 21 | + except FileNotFoundError: |
| 22 | + print(f"Error: File not found - {file_path}") |
| 23 | + sys.exit(1) |
| 24 | + except json.JSONDecodeError: |
| 25 | + print(f"Error: Invalid JSON format in file - {file_path}") |
| 26 | + sys.exit(1) |
| 27 | + |
| 28 | +def save_json(data, output_path): |
| 29 | + """Save the data to a JSON file.""" |
| 30 | + try: |
| 31 | + with open(output_path, 'w', encoding='utf-8') as file: |
| 32 | + json.dump(data, file, ensure_ascii=False, indent=4) |
| 33 | + except Exception as e: |
| 34 | + print(f"Error: Unable to save the file - {output_path}\n{e}") |
| 35 | + sys.exit(1) |
| 36 | + |
| 37 | +def save_file(data, output_path): |
| 38 | + """Save the data to any file.""" |
| 39 | + try: |
| 40 | + with open(output_path, 'w', encoding='utf-8') as file: |
| 41 | + file.write(data) |
| 42 | + except Exception as e: |
| 43 | + print(f"Error: Unable to save the file - {output_path}\n{e}") |
| 44 | + sys.exit(1) |
| 45 | + |
| 46 | +def find_untranslated_strings(english_strings, translated_strings): |
| 47 | + """Find strings that are not translated.""" |
| 48 | + untranslated = {} |
| 49 | + for key, value in english_strings.items(): |
| 50 | + if key not in translated_strings or not translated_strings[key]: |
| 51 | + untranslated[key] = value |
| 52 | + return untranslated |
| 53 | + |
| 54 | +def process_translations(untranslated_strings, index_mapping): |
| 55 | + """Distribute untranslated strings into files based on the index mapping.""" |
| 56 | + file_contents = {} |
| 57 | + |
| 58 | + for key, value in untranslated_strings.items(): |
| 59 | + if key in index_mapping: |
| 60 | + |
| 61 | + target_file = index_mapping[key] |
| 62 | + # Check if value contains something like auth_email/pluginname. |
| 63 | + # If it does, split it and use the first part as the key. |
| 64 | + if "/" in target_file: |
| 65 | + target_file = target_file.split("/")[0] |
| 66 | + |
| 67 | + if target_file not in file_contents: |
| 68 | + file_contents[target_file] = "<?php\n\n" |
| 69 | + |
| 70 | + # Convert to format expected by the PHP file. |
| 71 | + # Value has to be escaped to avoid issues with special characters. |
| 72 | + value = value.replace("'", "\\'") |
| 73 | + |
| 74 | + if target_file != "local_moodlemobileapp": |
| 75 | + # Key in this case is the last part of the key. |
| 76 | + key = key.split(".")[-1] |
| 77 | + |
| 78 | + file_contents[target_file] += f"$string['{key}'] = '{value}';\n" |
| 79 | + else: |
| 80 | + print(f"Warning: Key '{key}' not found in index mapping.") |
| 81 | + |
| 82 | + return file_contents |
| 83 | + |
| 84 | +def main(): |
| 85 | + if len(sys.argv) != 4: |
| 86 | + print("Usage: python script.py <index_file.json> <english_strings.json> <partial_translation.json>") |
| 87 | + sys.exit(1) |
| 88 | + |
| 89 | + index_file = sys.argv[1] |
| 90 | + english_file = sys.argv[2] |
| 91 | + translation_file = sys.argv[3] |
| 92 | + |
| 93 | + # Load the JSON files |
| 94 | + index_mapping = load_json(index_file) |
| 95 | + english_strings = load_json(english_file) |
| 96 | + translated_strings = load_json(translation_file) |
| 97 | + |
| 98 | + # Find untranslated strings |
| 99 | + untranslated_strings = find_untranslated_strings(english_strings, translated_strings) |
| 100 | + |
| 101 | + # Process the untranslated strings |
| 102 | + file_contents = process_translations(untranslated_strings, index_mapping) |
| 103 | + |
| 104 | + for file_name, content in file_contents.items(): |
| 105 | + translation = translation_file.replace(".json", "") |
| 106 | + directory = f"output/{translation}" |
| 107 | + # Create the directory if it doesn't exist |
| 108 | + if not os.path.exists(directory): |
| 109 | + os.makedirs(directory) |
| 110 | + |
| 111 | + output_path = f"{directory}/{file_name}.php" |
| 112 | + save_file(content, output_path) |
| 113 | + |
| 114 | + print(f"Created {output_path} with {len(content)} untranslated strings.") |
| 115 | + |
| 116 | + # Save the untranslated strings to a JSON file |
| 117 | + output_path = f"output/{translation}/untranslated_strings.json" |
| 118 | + save_json(untranslated_strings, output_path) |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + main() |
0 commit comments