retro-go/tools/rg_locate_str.py
Raphael Texier 7b4a99cba8
Adding Localization support (#159)
* Innitial commit

Localization for retro-go using a simple 0(n) lookup function called rg_gettext()

* adding language settings in options menu

* adding more gettext()

* new lookup function

* adding "For these changes to take effect you must restart your device." gui alert + fixing gettext() function

* modifying the gui dialog

* updating struct syntax

* update struct syntax (again)

* creating the python tool for localization

* updating tool + adding missing translations

* moving stuff to libs + starting writing readme

* adding missing "libs/localization" folder import in cmakelist + added the "fixme for rg_system"

* synthax adjust + moving back stuff from libs to retro-go

* removing trailing spaces

* adding the enum for language ids

* updating documentation according to the latest changes

* small tweaks

* Moved LOCALIZATION.md to the root folder

Whilst it is mostly relevant to libretro-go, it really is project-wide documentation.

* rg_localization: Got rid of the switch, made GUI dynamic

This makes adding a language more straightforward.

I kept the *msg *fr *en for now to avoid updating translations.h, but it could be replaced by the GCC extension as such:

[RG_LANG_EN] = "...",
[RG_LANG_FR] = "...",

So that adding a language is really just updating the enum...

* rg_localization: translations is const, we can use RG_COUNT

* rg_gui: Fixed language selection

* rg_localization: No need to validate rg_language in rg_gettext

It should always be valid, there's no need to validate it.

* rg_gui: Show language name in the log

* rg_localization: Got rid of the Translation struct

I am not 100% positive this is a good move...

Benefits:
- One less thing to change when adding a language
- Less code is always better

Cons:
- It doesn't make it clear what the "key" is (the english text)
- If in the future we need to add things like flags it will have to be returned to a struct

* updated python tool + updating translations

* added missing translations

* audio filter wrong translation

* fix : "a propose de retro-go"

---------

Co-authored-by: Alex Duchesne <ducalex007@gmail.com>
2024-11-16 13:04:50 -05:00

91 lines
3.5 KiB
Python

import os
def scan_folder_for_strings(folder_path):
# List to store found strings in this file
file_strings = []
# Walk through the directory and subdirectories
for dirpath, _, filenames in os.walk(folder_path):
for filename in filenames:
file_path = os.path.join(dirpath, filename)
if file_path.endswith(".c"): # scanning all .c files
with open(file_path, 'r', encoding='utf-8') as file:
content = file.readlines()
# Search for the _(" pattern in each line
for line in content:
start = 0
while start < len(line):
# Find the occurrence of _(" in the line
start = line.find('_("', start)
if start == -1:
break # No more occurrences in this line
# Find the closing ")
end = line.find('")', start + 3) # search after _(
if end != -1:
# Extract the string between _(" and ")
extracted_string = line[start + 3:end]
file_strings.append(extracted_string)
start = end + 2 # Move past the last found string
else:
# If no closing, break out of loop to avoid infinite loop
break
return file_strings
def scan_file_for_msg_strings(file_path):
found_strings = []
try:
# Open the file and read it line by line
with open(file_path, 'r', encoding='utf-8') as file:
content = file.readlines()
# Search for the .msg = "pattern" in each line
for line in content:
start = 0
while start < len(line):
# Find the occurrence of .msg =
start = line.find('[RG_LANG_EN] = "', start)
if start == -1:
break # No more occurrences in this line
# Find the closing "
start_quote = start + len('[RG_LANG_EN] = "')
end_quote = line.find('"', start_quote)
if end_quote != -1:
# Extract the string between " and "
extracted_string = line[start_quote:end_quote]
found_strings.append(extracted_string)
start = end_quote + 1 # Move past the last found string
else:
# If no closing ", break out of the loop
break
except FileNotFoundError:
print(f"The file '{file_path}' does not exist.")
return found_strings
# Scan the project's folders for strings to translate _("string")
found_strings_in_files = scan_folder_for_strings(os.getcwd())
# removing any dupicates :
found_strings_in_files = list(dict.fromkeys(found_strings_in_files))
# Scan the file 'retro-go/localization.c'
translated = scan_file_for_msg_strings('components/retro-go/translations.h')
file = open("missing_translation.txt", "w")
for string in found_strings_in_files:
if string not in translated:
print("missing translation", '"'+string+'"')
file.write('{\n\t[RG_LANG_EN] = "'+string+'",\n\t[RG_LANG_FR] = \"\",\n},\n')
# file output :
#{
# [RG_LANG_EN] = "missing string",
# [RG_LANG_FR] = "",
#},
file.close()