fish-station/Tools/_sunrise/yamlextractor.py
Rinary 3fc5d92b2d
Фиксы/Перевод (#296)
* upd

* upd

* upd

* add localization tools

* upd

* upd

* upd

* upd

* upd

* Update book-salvage.ftl

* Update book-authorbooks.ftl

* Update implant.ftl

* fix

* fix

* fix
2024-08-18 00:28:18 +03:00

107 lines
4.7 KiB
Python

import os
from fluent.syntax.parser import FluentParser
from fluent.syntax.serializer import FluentSerializer
from file import YAMLFile, FluentFile
from fluentast import FluentSerializedMessage, FluentAstAttributeFactory
from fluentformatter import FluentFormatter
from project import Project
import logging
######################################### Class defifitions ############################################################
class YAMLExtractor:
def __init__(self, yaml_files):
self.yaml_files = yaml_files
def execute(self):
for yaml_file in yaml_files:
yaml_elements = yaml_file.get_elements(yaml_file.parse_data(yaml_file.read_data()))
if not len(yaml_elements):
continue
fluent_file_serialized = self.get_serialized_fluent_from_yaml_elements(yaml_elements)
if not fluent_file_serialized:
continue
pretty_fluent_file_serialized = formatter.format_serialized_file_data(fluent_file_serialized)
relative_parent_dir = yaml_file.get_relative_parent_dir(project.prototypes_dir_path).lower()
file_name = yaml_file.get_name()
en_fluent_file_path = self.create_en_fluent_file(relative_parent_dir, file_name, pretty_fluent_file_serialized)
ru_fluent_file_path = self.create_ru_fluent_file(en_fluent_file_path)
def get_serialized_fluent_from_yaml_elements(self, yaml_elements):
fluent_serialized_messages = list(
map(lambda el: FluentSerializedMessage.from_yaml_element(el.id, el.name, FluentAstAttributeFactory.from_yaml_element(el), el.parent_id), yaml_elements)
)
fluent_exist_serialized_messages = list(filter(lambda m: m, fluent_serialized_messages))
if not len(fluent_exist_serialized_messages):
return None
return '\n'.join(fluent_exist_serialized_messages)
def create_en_fluent_file(self, relative_parent_dir, file_name, file_data):
# Проверяем, если информация берется из директории _Sunrise
if '_sunrise' in relative_parent_dir:
# Создаем путь для сохранения в en-US/_sunrise/_prototypes
en_new_dir_path = os.path.join(project.en_locale_dir_path, '_sunrise', '_prototypes', relative_parent_dir.replace('_sunrise', '').strip(os.sep))
else:
# Обычный путь сохранения
en_new_dir_path = os.path.join(project.en_locale_prototypes_dir_path, relative_parent_dir)
# Убедимся, что директория существует
os.makedirs(en_new_dir_path, exist_ok=True)
# Формируем полный путь к файлу
en_fluent_file_path = os.path.join(en_new_dir_path, f'{file_name}.ftl')
# Сохраняем файл
en_fluent_file = FluentFile(en_fluent_file_path)
en_fluent_file.save_data(file_data)
logging.info(f'Актуализирован файл английской локализации {en_fluent_file.full_path}')
return en_fluent_file.full_path
def create_ru_fluent_file(self, en_analog_file_path):
if '_sunrise' in en_analog_file_path.lower():
# Если уже есть _sunrise, просто заменяем en-US на ru-RU
ru_file_full_path = en_analog_file_path.replace('en-US', 'ru-RU')
else:
# Обычный путь сохранения
ru_file_full_path = en_analog_file_path.replace('en-US', 'ru-RU')
# Убедимся, что директория существует
os.makedirs(os.path.dirname(ru_file_full_path), exist_ok=True)
if os.path.isfile(ru_file_full_path):
return
else:
en_file = FluentFile(f'{en_analog_file_path}')
file = FluentFile(f'{ru_file_full_path}')
file.save_data(en_file.read_data())
logging.info(f'Создан файл русской локализации {ru_file_full_path}')
return ru_file_full_path
######################################## Var definitions ###############################################################
logging.basicConfig(level = logging.INFO)
project = Project()
serializer = FluentSerializer()
parser = FluentParser()
formatter = FluentFormatter()
yaml_files_paths = project.get_files_paths_by_dir(project.prototypes_dir_path, 'yml')
yaml_files = list(map(lambda yaml_file_path: YAMLFile(yaml_file_path), yaml_files_paths))
########################################################################################################################
logging.info(f'Поиск yaml-файлов ...')
YAMLExtractor(yaml_files).execute()