Skip to content

Instantly share code, notes, and snippets.

@VIRUXE
Created May 9, 2024 13:26
Show Gist options
  • Save VIRUXE/7c155d6af7e1794d5b69c4d3bdff1439 to your computer and use it in GitHub Desktop.
Save VIRUXE/7c155d6af7e1794d5b69c4d3bdff1439 to your computer and use it in GitHub Desktop.
Checks missing and unused locale keys for a given language, used on the ESX framework (FiveM)
"""
Author: https://github.com/VIRUXE
This script is intended to scan actual script `.lua` files inside a FXServer (CFX/FiveM server) `resources` folder, looking for translation keys.
The ESX framework uses two functions called Translate and TranslateCap to retrieve translations from their respective locale files.
Once it finds all the keys used in the script files, it will scan the `pt.lua` file inside the `locales` folder.
In order to find out which keys are missing, it will compare the keys found in the script files with the keys found in the `pt.lua` file.
The script will then print the keys that are missing and/or unused from the `pt.lua` file
It should be obvious by now that this is hardcoded for the 'pt' locale. But it's fairly trivial to change to whatever language you need.
"""
import os
import re
import sys
from termcolor import colored
if len(sys.argv) != 2:
print(colored("Usage: py locale-cleanup.py <path_to_folder>", "red"))
exit()
Folder_path = sys.argv[1]
# Check if the folder path is valid
if not os.path.isdir(Folder_path):
print(colored("The provided path '{}' is not a valid folder.".format(Folder_path), "red"))
exit()
print(colored("Scanning folder for .lua files...", "blue"))
def extract_keys(file_path):
keys = set()
with open(file_path, 'r', encoding='utf8') as file:
for line in file:
keys.update(re.findall(r'Translate(?:Cap)?\([\'\"](.+?)[\'\"]\)', line)) # * Finds both Translate and TranslateCap
return keys
Used_keys = set()
for root, dirs, files in os.walk(Folder_path):
if 'locales' in root:
continue # Skip the locales folder. We don't care to scan the locale files
for file in files:
if file.endswith('.lua'):
file_path = os.path.join(root, file)
relative_path = os.path.relpath(file_path, Folder_path)
file_keys = extract_keys(file_path)
Used_keys.update(file_keys)
print(colored("Scanned {}: {}".format(relative_path, file_keys), "green"))
Pt_locale_file = os.path.join(Folder_path, 'locales', 'pt.lua')
Pt_locale_keys = set()
if os.path.exists(Pt_locale_file):
with open(Pt_locale_file, 'r', encoding='utf8') as file:
for line in file:
Pt_locale_keys.update(re.findall(r'\[["\']([^"\']+)["\']\]\s*=', line))
# Remove the 'pt' key from the set of unused keys, if present
# ? Don't ask me why this is a thing. Blame ESX for using this structure, I guess?
Pt_locale_keys.discard('pt')
# Determine missing and unused keys
# ? Yes, in case you're not used to Python and are scratching your head right now, this works. Python is actually great.
Missing_keys = Used_keys - Pt_locale_keys
Unused_keys = Pt_locale_keys - Used_keys
if Missing_keys:
print(colored('Missing keys in pt.lua: {}'.format(Missing_keys), 'yellow'))
if Unused_keys:
print(colored('Unused keys in pt.lua: {}'.format(Unused_keys), 'red'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment