#!/usr/bin/env python3
#
# Copyright (c) 2024 Stormux
# Copyright (c) 2010-2012 The Orca Team
# Copyright (c) 2012 Igalia, S.L.
# Copyright (c) 2005-2010 Sun Microsystems Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., Franklin Street, Fifth Floor,
# Boston MA  02110-1301 USA.
#
# Forked from Orca screen reader.
# Cthulhu project: https://git.stormux.org/storm/cthulhu

import gi, os, locale, gettext
from gi.repository import GObject
import gettext

from cthulhu import cthulhu_i18n
from cthulhu import translation_context

class TranslationManager():
    def __init__(self, app):
        self.app = app
        self.translatioContextnDict = {}
    def initTranslation(self, name, domain = None, localeDir = None, language = None, fallbackToCthulhuTranslation = True):
        translationContext = translation_context.TranslationContext(self.app)
        if os.path.isdir(localeDir):
            translationContext.setDomain(domain)
            translationContext.setLocaleDir(localeDir)
        translationContext.setLanguage(language)
        translationContext.setFallbackToCthulhuTranslation(fallbackToCthulhuTranslation)
        translationContext.updateTranslation()
        self.addTranslationDict(name, translationContext)
        return translationContext
    def addTranslationDict(self, name, translationContext):
        self.translatioContextnDict[name] = translationContext
    def getTranslationContextByName(name):
        try:
            return self.translatioContextnDict[name]
        except KeyError:
            return self.translatioContextnDict['cthulhu']
    #def getTranslationsInstance(self, domain='cthulhu'):
        """ Gets the gettext translation instance for this add-on.
        <addon-path>\\locale will be used to find .mo files, if exists.
        If a translation file is not found the default fallback null translation is returned.
        @param domain: the translation domain to retrieve. The 'Cthulhu' default should be used in most cases.
        @returns: the gettext translation class.
        """
     #   localedir = os.path.join(self.path, "locale")
     #   return gettext.translation(domain, localedir=localedir, languages=[languageHandler.getLanguage()], fallback=True)
"""
    def initTranslation():
        addon = getCodeAddon(frameDist=2)
        translations = addon.getTranslationsInstance()
        # Point _ to the translation object in the globals namespace of the caller frame
        # FIXME: should we retrieve the caller module object explicitly?
        try:
            callerFrame = inspect.currentframe().f_back
            callerFrame.f_globals['_'] = translations.gettext
            # Install our pgettext function.
            callerFrame.f_globals['pgettext'] = languageHandler.makePgettext(translations)
        finally:
            del callerFrame # Avoid reference problems with frames (per python docs)
"""

