-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlang.py
More file actions
executable file
·60 lines (53 loc) · 1.99 KB
/
lang.py
File metadata and controls
executable file
·60 lines (53 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
import re
import json
# Directories to search for files that contain translatable strings
search_directories = {
'blade': '../resources/views', # Assuming Blade templates are in the resources/views directory
'controller': '../app/Http/Controllers'
}
# Base language directory
lang_directory = './langExport'
# Files to generate
lang_files = ['en.json', 'ar.json']
# Pattern to match trans function
pattern = re.compile(r"trans\(['\"](.*?)['\"]\)")
def find_files(directory, extension):
"""
Recursively find files with a given extension in a directory.
"""
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(extension):
yield os.path.join(root, file)
def extract_translations(directories):
"""
Extract translations from specified directories and file types.
"""
translations = {}
for dir_type, dir_path in directories.items():
extension = '.blade.php' if dir_type == 'blade' else '.php'
for file_path in find_files(dir_path, extension):
with open(file_path, 'r', encoding='utf-8') as file:
contents = file.read()
matches = pattern.findall(contents)
for match in matches:
translations[match] = match
return translations
def write_translations_to_files(translations, lang_directory, lang_files):
"""
Write the extracted translations to specified language files.
"""
for lang_file in lang_files:
path = os.path.join(lang_directory, lang_file)
try:
with open(path, 'w', encoding='utf-8') as file:
json.dump(translations, file, ensure_ascii=False, indent=4)
except IOError as e:
print(f"Error writing to {lang_file}: {e}")
def main():
translations = extract_translations(search_directories)
write_translations_to_files(translations, lang_directory, lang_files)
print("Export complete.")
if __name__ == '__main__':
main()