#!/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.
#
# Fork of Orca Screen Reader (GNOME)
# Original source: https://gitlab.gnome.org/GNOME/orca

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

from cthulhu import cthulhu_i18n

class TranslationContext():
    def __init__(self, app):
        self.app = app
        self.localeDir = cthulhu_i18n.localedir
        self.domain = 'cthulhu'
        self.language = 'en'
        self.fallbackToCthulhuTranslation = True
        self.domainTranslation = None
        self.cthulhuMainTranslation = None

    def setDomain(self, domain):
        self.domain = domain
    def setLanguage(self, language):
        self.language = language
    def setLocaleDir(self, localeDir):
        self.localeDir = localeDir
    def getCurrentDefaultLocale(self):
        return locale.getdefaultlocale()[0]
    def setFallbackToCthulhuTranslation(self, fallback):
        self.fallback = fallback
    def getFallbackToCthulhuTranslation(self):
        return self.fallback
    def getLocaleDir(self):
        return self.localeDir
    def getLanguage(self):
        return self.language
    def getDomain(self):
        return self.domain
    def getDomainTranslation(self):
        return self.domainTranslation
    def getCthulhuMainTranslation(self):
        return self.cthulhuMainTranslation
    def setDomainTranslation(self, domainTranslation):
        self.domainTranslation = domainTranslation
    def setCthulhuMainTranslation(self, cthulhuMainTranslation):
        self.cthulhuMainTranslation = cthulhuMainTranslation
    def updateTranslation(self):
        self.setDomainTranslation(None)
        self.setCthulhuMainTranslation(None)

        try:
            self.setLanguage(self.getCurrentDefaultLocale())
        except:
            self.setLanguage('en')
            print(e)
        if self.getFallbackToCthulhuTranslation():
            cthulhuaMainTranslation = cthulhu_i18n
            self.setCthulhuMainTranslation(cthulhuaMainTranslation)
        try:
            domainTranslation = gettext.translation(self.getDomain(), self.getLocaleDir(), languages=[self.getLanguage()])
            self.setDomainTranslation(domainTranslation)
        except Exception as e:
            print(e)

    def gettext(self, text):
        translatedText = text
        if self.getDomainTranslation() != None:
            try:
                translatedText = self.getDomainTranslation().gettext(text)
            except Exception as e:
                print(e)
        if translatedText == text:
            if self.getFallbackToCthulhuTranslation() or self.getDomainTranslation() == None:
                if self.getCthulhuMainTranslation() != None:
                    try:
                        translatedText = self.getCthulhuMainTranslation().cgettext(text) # gettext from cthulhu_i18n
                    except Exception as e:
                        print(e)
        return translatedText

    def ngettext(self, singular, plural, n):
        translatedText = singular
        if n > 1:
            translatedText = plural
        if self.getDomainTranslation() != None:
            try:
                translatedText = self.getDomainTranslation().ngettext(singular, plural, n)
            except:
                pass
        if translatedText in [singular, plural]: # not translated
            if self.getFallbackToCthulhuTranslation() or self.getDomainTranslation() == None:
                if self.getCthulhuMainTranslation() != None:
                    try:
                        translatedText = self.getCthulhuMainTranslation().ngettext(singular, plural, n)
                    except:
                        pass
        return translatedText
