|
13 | 13 | # You should have received a copy of the GNU General Public License |
14 | 14 | # along with Korman. If not, see <http://www.gnu.org/licenses/>. |
15 | 15 |
|
| 16 | +from __future__ import annotations |
| 17 | + |
16 | 18 | import bpy |
17 | 19 | from PyHSPlasma import * |
18 | 20 |
|
|
21 | 23 | import itertools |
22 | 24 | from pathlib import Path |
23 | 25 | import re |
24 | | -from typing import NamedTuple, Union |
| 26 | +from typing import * |
25 | 27 | from xml.sax.saxutils import escape as xml_escape |
26 | 28 | import weakref |
27 | 29 |
|
@@ -91,6 +93,24 @@ def add_string(self, set_name, element_name, language, value): |
91 | 93 |
|
92 | 94 | self._strings[set_name][element_name][language] = value |
93 | 95 |
|
| 96 | + def get_localized_string(self, translations: Dict[str, str]): |
| 97 | + # If there's only an English translation, just output this string directly. |
| 98 | + if translations.keys() == {"English"}: |
| 99 | + return translations["English"] |
| 100 | + |
| 101 | + ignored_translations = frozenset(translations.keys()) - _SP_LANGUAGES |
| 102 | + if ignored_translations: |
| 103 | + self._report.warn( |
| 104 | + f"These translations are not supported in single player: " |
| 105 | + f"{', '.join(ignored_translations)}" |
| 106 | + ) |
| 107 | + |
| 108 | + return "".join( |
| 109 | + f"${lang[0:2]}${value}" |
| 110 | + for lang, value in translations.items() |
| 111 | + if lang in _SP_LANGUAGES |
| 112 | + ) |
| 113 | + |
94 | 114 | @contextmanager |
95 | 115 | def _generate_file(self, filename, **kwargs): |
96 | 116 | if self._exporter is not None: |
@@ -234,20 +254,27 @@ def run(self): |
234 | 254 | def _run_harvest_journals(self): |
235 | 255 | from ..properties.modifiers import TranslationMixin |
236 | 256 |
|
| 257 | + def iter_subclasses(cls): |
| 258 | + for i in cls.__subclasses__(): |
| 259 | + yield i |
| 260 | + if i.__subclasses__(): |
| 261 | + yield from iter_subclasses(i) |
| 262 | + |
| 263 | + |
237 | 264 | objects = bpy.context.scene.objects |
238 | 265 | self._report.progress_advance() |
239 | 266 | self._report.progress_range = len(objects) |
240 | 267 | inc_progress = self._report.progress_increment |
241 | 268 |
|
242 | 269 | for i in objects: |
243 | | - for mod_type in filter(None, (getattr(j, "pl_id", None) for j in TranslationMixin.__subclasses__())): |
| 270 | + for mod_type in filter(None, (getattr(j, "pl_id", None) for j in iter_subclasses(TranslationMixin))): |
244 | 271 | modifier = getattr(i.plasma_modifiers, mod_type) |
245 | 272 | if modifier.enabled: |
246 | | - translations = [j for j in modifier.translations if j.text_id is not None] |
| 273 | + translations = [j for j in modifier.translations if j.text] |
247 | 274 | if not translations: |
248 | 275 | self._report.error(f"'{i.name}': No content translations available. The localization will not be exported.") |
249 | 276 | for j in translations: |
250 | | - self.add_string(modifier.localization_set, modifier.key_name, j.language, j.text_id) |
| 277 | + self.add_string(modifier.localization_set, modifier.key_name, j.language, j.text) |
251 | 278 | inc_progress() |
252 | 279 |
|
253 | 280 | def _run_generate(self): |
|
0 commit comments