| 549 | |
| 550 | |
| 551 | class LocaleSettings: |
| 552 | _instance = None |
| 553 | DEFAULT = "en_US" |
| 554 | |
| 555 | defaults = { |
| 556 | 'locale': DEFAULT, |
| 557 | 'eos_locale': 'Auto' # flag for "Default" which is the same as the locale or, if not available, English |
| 558 | } |
| 559 | |
| 560 | def __init__(self): |
| 561 | self.settings = SettingsProvider.getInstance().getSettings('localeSettings', self.defaults) |
| 562 | |
| 563 | try: |
| 564 | with open(os.path.join(config.pyfaPath, 'locale', 'progress.json'), "r") as f: |
| 565 | self.progress_data = json.load(f) |
| 566 | except FileNotFoundError: |
| 567 | self.progress_data = {} |
| 568 | |
| 569 | @classmethod |
| 570 | def getInstance(cls): |
| 571 | if cls._instance is None: |
| 572 | cls._instance = LocaleSettings() |
| 573 | return cls._instance |
| 574 | |
| 575 | def get_progress(self, lang): |
| 576 | if not self.progress_data: |
| 577 | return None |
| 578 | if lang == self.defaults['locale']: |
| 579 | return None |
| 580 | return self.progress_data.get(lang) |
| 581 | |
| 582 | @classmethod |
| 583 | def supported_languages(cls): |
| 584 | """Requires the application to be initialized, otherwise wx.Translation isn't set.""" |
| 585 | pyfalog.info(f'using "{config.CATALOG}" to fetch languages, relatively base path "{os.getcwd()}"') |
| 586 | return {x: wx.Locale.FindLanguageInfo(x) for x in wx.Translations.Get().GetAvailableTranslations(config.CATALOG)} |
| 587 | |
| 588 | def get(self, key): |
| 589 | """gets the raw value fo the setting""" |
| 590 | return self.settings[key] |
| 591 | |
| 592 | def get_eos_locale(self): |
| 593 | """gets the effective value of the setting""" |
| 594 | val = self.settings['eos_locale'] |
| 595 | return val if val != self.defaults['eos_locale'] else self.settings['locale'].split("_")[0] |
| 596 | |
| 597 | def set(self, key, value): |
| 598 | if key == 'locale' and value not in self.supported_languages(): |
| 599 | self.settings[key] = self.DEFAULT |
| 600 | self.settings[key] = value |