def get_detected_langs(self):
        store = self.main_controller.store_controller.store
        if not store:
            return None

        if not self.lang_identifier:
            from translate.lang.identify import LanguageIdentifier
            self.lang_identifier = LanguageIdentifier()
        srccode = self.lang_identifier.identify_source_lang(store.get_units())
        tgtcode = self.lang_identifier.identify_target_lang(store.get_units())
        srclang = tgtlang = None
        if srccode:
            srclang = LanguageModel(srccode)
        if tgtcode:
            tgtlang = LanguageModel(tgtcode)

        return srclang, tgtlang
Ejemplo n.º 2
0
class TestLanguageIdentifier(object):
    def setup_class(self):
        self.langident = LanguageIdentifier()

    def test_identify_lang(self):
        assert self.langident.identify_lang('') == None
        assert self.langident.identify_lang(TEXT) == 'de'

    def test_identify_store(self):
        langlist = [TranslationUnit(string) for string in TEXT_LIST]
        assert self.langident.identify_source_lang(langlist) == 'de'
        for i, unit in enumerate(langlist):
            unit.target = TEXT_LIST[i]
        assert self.langident.identify_target_lang(langlist) == 'de'

    def test_bad_init_data(self):
        """Test __init__ with bad conf files and data dirs"""
        assert raises(ValueError, LanguageIdentifier, model_dir='missing')
        assert raises(ValueError, LanguageIdentifier, conf_file='missing')
Ejemplo n.º 3
0
class TestLanguageIdentifier(object):

    def setup_class(self):
        self.langident = LanguageIdentifier()

    def test_identify_lang(self):
        assert self.langident.identify_lang('') == None
        assert self.langident.identify_lang(TEXT) == 'de'

    def test_identify_store(self):
        langlist = [TranslationUnit(string) for string in TEXT_LIST]
        assert self.langident.identify_source_lang(langlist) == 'de'
        for i, unit in enumerate(langlist):
            unit.target = TEXT_LIST[i]
        assert self.langident.identify_target_lang(langlist) == 'de'

    def test_bad_init_data(self):
        """Test __init__ with bad conf files and data dirs"""
        assert raises(ValueError, LanguageIdentifier, model_dir='missing')
        assert raises(ValueError, LanguageIdentifier, conf_file='missing')
Ejemplo n.º 4
0
class TestLanguageIdentifier:
    def setup_class(self):
        self.langident = LanguageIdentifier()

    def test_identify_lang(self):
        assert self.langident.identify_lang("") is None
        assert self.langident.identify_lang(TEXT) == "de"

    def test_identify_store(self):
        langlist = [TranslationUnit(string) for string in TEXT_LIST]
        assert self.langident.identify_source_lang(langlist) == "de"
        for i, unit in enumerate(langlist):
            unit.target = TEXT_LIST[i]
        assert self.langident.identify_target_lang(langlist) == "de"

    def test_bad_init_data(self):
        """Test __init__ with bad conf files and data dirs"""
        with raises(ValueError):
            LanguageIdentifier(model_dir="missing")
        with raises(ValueError):
            LanguageIdentifier(conf_file="missing")
    def __init__(self, main_controller):
        GObjectWrapper.__init__(self)

        self.main_controller = main_controller
        self.main_controller.lang_controller = self
        self.lang_identifier = LanguageIdentifier(self.MODEL_DIR, self.CONF_FILE)
        self.new_langs = []
        self._init_langs()
        self.recent_pairs = self._load_recent()

        self.main_controller.store_controller.connect('store-loaded', self._on_store_loaded)
        self.main_controller.connect('quit', self._on_quit)
        self.connect('source-lang-changed', self._on_lang_changed)
        self.connect('target-lang-changed', self._on_lang_changed)

        self.view = LanguageView(self)
        self.view.show()
Ejemplo n.º 6
0
    def handlefile(self, filename, verbose):
        if verbose:
            print("Importing %s" % filename)

        try:
            store = factory.getobject(filename)
            source_lang = self.source_lang or store.getsourcelanguage()
            target_lang = self.target_lang or store.gettargetlanguage()
            project_style = self.project_style or store.getprojectstyle()

            if not target_lang:
                short = os.path.splitext(os.path.split(filename)[1])[0]
                if langcode_ire.match(short):
                    target_lang = short
                else:
                    long, short = os.path.split(os.path.split(filename)[0])
                    if langcode_ire.match(short) and short not in ('po', 'www', 'gtk'):
                        target_lang = short
                    else:
                        short = os.path.split(long)[1]
                        if langcode_ire.match(short) and short not in ('po', 'www', 'gtk'):
                            detected = LanguageIdentifier().identify_target_lang(store.units)
                            if short == detected:
                                target_lang = short
                                print('Guessing language "%s" for %s' % (short, filename))

            if not source_lang or not target_lang:
                logging.error("Missing source or target language. Won't "
                              "import %s", filename)
                return
        except ValueError as e:
            if "Unknown filetype" not in str(e):
                logging.exception("Error while handling: %s", filename)
            return
        except Exception:
            logging.exception("Error while processing: %s", filename)
            return

        # Do something useful with the store and the database.
        try:
            logging.info("Importing strings from: %s", filename)
            current_app.tmdb.add_store(store, source_lang, target_lang,
                                       project_style, commit=True)
        except Exception:
            logging.exception("Error importing strings from: %s", filename)
            raise
Ejemplo n.º 7
0
    def get_detected_langs(self):
        store = self.main_controller.store_controller.store
        if not store:
            return None

        if not self.lang_identifier:
            from translate.lang.identify import LanguageIdentifier
            self.lang_identifier = LanguageIdentifier()
        srccode = self.lang_identifier.identify_source_lang(store.get_units())
        tgtcode = self.lang_identifier.identify_target_lang(store.get_units())
        srclang = tgtlang = None
        if srccode:
            srclang = LanguageModel(srccode)
        if tgtcode:
            tgtlang = LanguageModel(tgtcode)

        return srclang, tgtlang
Ejemplo n.º 8
0
 def test_bad_init_data(self):
     """Test __init__ with bad conf files and data dirs"""
     with raises(ValueError):
         LanguageIdentifier(model_dir='missing')
     with raises(ValueError):
         LanguageIdentifier(conf_file='missing')
Ejemplo n.º 9
0
 def setup_class(self):
     self.langident = LanguageIdentifier()
Ejemplo n.º 10
0
 def setup_class(self):
     self.langident = LanguageIdentifier()
class LanguageController(BaseController):
    """
    The logic behind language management in Virtaal.
    """

    __gtype_name__ = 'LanguageController'
    __gsignals__ = {
        'source-lang-changed': (SIGNAL_RUN_FIRST, None, (str,)),
        'target-lang-changed': (SIGNAL_RUN_FIRST, None, (str,)),
    }

    MODEL_DIR = LanguageIdentifier.MODEL_DIR
    CONF_FILE = LanguageIdentifier.CONF_FILE
    NUM_RECENT = 5
    """The number of recent language pairs to save/display."""

    # INITIALIZERS #
    def __init__(self, main_controller):
        GObjectWrapper.__init__(self)

        self.main_controller = main_controller
        self.main_controller.lang_controller = self
        self.lang_identifier = LanguageIdentifier(self.MODEL_DIR, self.CONF_FILE)
        self.new_langs = []
        self._init_langs()
        self.recent_pairs = self._load_recent()

        self.main_controller.store_controller.connect('store-loaded', self._on_store_loaded)
        self.main_controller.connect('quit', self._on_quit)
        self.connect('source-lang-changed', self._on_lang_changed)
        self.connect('target-lang-changed', self._on_lang_changed)

        self.view = LanguageView(self)
        self.view.show()

    def _init_langs(self):
        try:
            self._source_lang = LanguageModel(pan_app.settings.language['sourcelang'])
        except Exception:
            self._source_lang = None

        try:
            self._target_lang = LanguageModel(pan_app.settings.language['targetlang'])
        except Exception:
            self._target_lang = None

        # Load previously-saved (new) languages
        filename = os.path.join(pan_app.get_config_dir(), 'langs.ini')
        if os.path.isfile(filename):
            languages = pan_app.load_config(filename)
            for code in languages:
                languages[code] = (
                    languages[code]['name'],
                    int(languages[code]['nplurals']),
                    languages[code]['plural']
                )
            LanguageModel.languages.update(languages)


    # ACCESSORS #
    def _get_source_lang(self):
        return self._source_lang
    def _set_source_lang(self, lang):
        if isinstance(lang, basestring):
            lang = LanguageModel(lang)
        if not lang or lang == self._source_lang:
            return
        self._source_lang = lang
        self.emit('source-lang-changed', self._source_lang.code)
    source_lang = property(_get_source_lang, _set_source_lang)

    def _get_target_lang(self):
        return self._target_lang
    def _set_target_lang(self, lang):
        if isinstance(lang, basestring):
            lang = LanguageModel(lang)
        if not lang or lang == self._target_lang:
            return
        self._target_lang = lang
        self.emit('target-lang-changed', self._target_lang.code)
    target_lang = property(_get_target_lang, _set_target_lang)

    def set_language_pair(self, srclang, tgtlang):
        if isinstance(srclang, basestring):
            srclang = LanguageModel(srclang)
        if isinstance(tgtlang, basestring):
            tgtlang = LanguageModel(tgtlang)

        pair = (srclang, tgtlang)
        if pair in self.recent_pairs:
            self.recent_pairs.remove(pair)

        self.recent_pairs.insert(0, pair)
        self.recent_pairs = self.recent_pairs[:self.NUM_RECENT]

        self.source_lang = srclang
        self.target_lang = tgtlang
        self.view.update_recent_pairs()


    # METHODS #
    def get_detected_langs(self):
        store = self.main_controller.store_controller.store
        if not store:
            return None

        srccode = self.lang_identifier.identify_source_lang(store.get_units())
        tgtcode = self.lang_identifier.identify_target_lang(store.get_units())
        srclang = tgtlang = None
        if srccode:
            srclang = LanguageModel(srccode)
        if tgtcode:
            tgtlang = LanguageModel(tgtcode)

        return srclang, tgtlang

    def _load_recent(self):
        code_pairs = pan_app.settings.language['recentlangs'].split('|')
        codes = [pair.split(',') for pair in code_pairs]
        if codes == [['']]:
            return []

        recent_pairs = []
        for srccode, tgtcode in codes:
            srclang = LanguageModel(srccode)
            tgtlang = LanguageModel(tgtcode)
            recent_pairs.append((srclang, tgtlang))

        return recent_pairs

    def save_recent(self):
        pairs = [','.join([src.code, tgt.code]) for (src, tgt) in self.recent_pairs]
        recent = '|'.join(pairs)
        pan_app.settings.language['recentlangs'] = recent


    # EVENT HANDLERS #
    def _on_lang_changed(self, sender, code):
        self.save_recent()
        if self.source_lang == self.target_lang:
            self.view.notify_same_langs()
        else:
            self.view.notify_diff_langs()

    def _on_quit(self, main_controller):
        pan_app.settings.language['sourcelang'] = self.source_lang.code
        pan_app.settings.language['targetlang'] = self.target_lang.code

        if not self.new_langs:
            return

        langs = {}
        filename = os.path.join(pan_app.get_config_dir(), 'langs.ini')
        if os.path.isfile(filename):
            langs = pan_app.load_config(filename)

        newlangdict = {}
        for code in self.new_langs:
            newlangdict[code] = {}
            newlangdict[code]['name'] = LanguageModel.languages[code][0]
            newlangdict[code]['nplurals'] = LanguageModel.languages[code][1]
            newlangdict[code]['plural'] = LanguageModel.languages[code][2]
        langs.update(newlangdict)

        pan_app.save_config(filename, langs)

    def _on_store_loaded(self, store_controller):
        srclang = store_controller.store.get_source_language() or self.source_lang.code
        tgtlang = store_controller.store.get_target_language() or self.target_lang.code
        self.set_language_pair(srclang, tgtlang)
        self.target_lang.nplurals = self.target_lang.nplurals or store_controller.get_nplurals()
class LanguageController(BaseController):
    """
    The logic behind language management in Virtaal.
    """

    __gtype_name__ = 'LanguageController'
    __gsignals__ = {
        'source-lang-changed': (SIGNAL_RUN_FIRST, None, (str, )),
        'target-lang-changed': (SIGNAL_RUN_FIRST, None, (str, )),
    }

    NUM_RECENT = 5
    """The number of recent language pairs to save/display."""

    # INITIALIZERS #
    def __init__(self, main_controller):
        GObjectWrapper.__init__(self)

        self.main_controller = main_controller
        self.main_controller.lang_controller = self
        self.lang_identifier = None
        self.new_langs = []
        self._init_langs()
        self.recent_pairs = self._load_recent()

        self.main_controller.store_controller.connect('store-loaded',
                                                      self._on_store_loaded)
        self.main_controller.connect('quit', self._on_quit)
        self.connect('source-lang-changed', self._on_lang_changed)
        self.connect('target-lang-changed', self._on_lang_changed)

        self.view = None

    def _init_langs(self):
        try:
            self._source_lang = LanguageModel(
                pan_app.settings.language['sourcelang'])
        except Exception:
            self._source_lang = None

        try:
            self._target_lang = LanguageModel(
                pan_app.settings.language['targetlang'])
        except Exception:
            self._target_lang = None

        # Load previously-saved (new) languages
        filename = os.path.join(pan_app.get_config_dir(), 'langs.ini')
        if os.path.isfile(filename):
            languages = pan_app.load_config(filename)
            for code in languages:
                languages[code] = (languages[code]['name'],
                                   int(languages[code]['nplurals']),
                                   languages[code]['plural'])
            LanguageModel.languages.update(languages)

    # ACCESSORS #
    def _get_source_lang(self):
        return self._source_lang

    def _set_source_lang(self, lang):
        if isinstance(lang, basestring):
            lang = LanguageModel(lang)
        if not lang or lang == self._source_lang:
            return
        self._source_lang = lang
        self.emit('source-lang-changed', self._source_lang.code)

    source_lang = property(_get_source_lang, _set_source_lang)

    def _get_target_lang(self):
        return self._target_lang

    def _set_target_lang(self, lang):
        if isinstance(lang, basestring):
            lang = LanguageModel(lang)
        if not lang or lang == self._target_lang:
            return
        self._target_lang = lang
        self.emit('target-lang-changed', self._target_lang.code)

    target_lang = property(_get_target_lang, _set_target_lang)

    def set_language_pair(self, srclang, tgtlang):
        if isinstance(srclang, basestring):
            srclang = LanguageModel(srclang)
        if isinstance(tgtlang, basestring):
            tgtlang = LanguageModel(tgtlang)

        pair = (srclang, tgtlang)
        if pair in self.recent_pairs:
            self.recent_pairs.remove(pair)

        self.recent_pairs.insert(0, pair)
        self.recent_pairs = self.recent_pairs[:self.NUM_RECENT]

        self.source_lang = srclang
        self.target_lang = tgtlang
        self.view.update_recent_pairs()
        if self.source_lang == self.target_lang:
            self.view.notify_same_langs()

    # METHODS #
    def get_detected_langs(self):
        store = self.main_controller.store_controller.store
        if not store:
            return None

        if not self.lang_identifier:
            from translate.lang.identify import LanguageIdentifier
            self.lang_identifier = LanguageIdentifier()
        srccode = self.lang_identifier.identify_source_lang(store.get_units())
        tgtcode = self.lang_identifier.identify_target_lang(store.get_units())
        srclang = tgtlang = None
        if srccode:
            srclang = LanguageModel(srccode)
        if tgtcode:
            tgtlang = LanguageModel(tgtcode)

        return srclang, tgtlang

    def _load_recent(self):
        code_pairs = pan_app.settings.language['recentlangs'].split('|')
        codes = [pair.split(',') for pair in code_pairs]
        if codes == [['']]:
            return []

        recent_pairs = []
        for srccode, tgtcode in codes:
            srclang = LanguageModel(srccode)
            tgtlang = LanguageModel(tgtcode)
            recent_pairs.append((srclang, tgtlang))

        return recent_pairs

    def save_recent(self):
        pairs = [
            ','.join([src.code, tgt.code]) for (src, tgt) in self.recent_pairs
        ]
        recent = '|'.join(pairs)
        pan_app.settings.language['recentlangs'] = recent

    # EVENT HANDLERS #
    def _on_lang_changed(self, sender, code):
        self.save_recent()
        if self.source_lang == self.target_lang:
            self.view.notify_same_langs()
        else:
            self.view.notify_diff_langs()

    def _on_quit(self, main_controller):
        pan_app.settings.language['sourcelang'] = self.source_lang.code
        pan_app.settings.language['targetlang'] = self.target_lang.code

        if not self.new_langs:
            return

        langs = {}
        filename = os.path.join(pan_app.get_config_dir(), 'langs.ini')
        if os.path.isfile(filename):
            langs = pan_app.load_config(filename)

        newlangdict = {}
        for code in self.new_langs:
            newlangdict[code] = {}
            newlangdict[code]['name'] = LanguageModel.languages[code][0]
            newlangdict[code]['nplurals'] = LanguageModel.languages[code][1]
            newlangdict[code]['plural'] = LanguageModel.languages[code][2]
        langs.update(newlangdict)

        pan_app.save_config(filename, langs)

    def _on_store_loaded(self, store_controller):
        if not self.view:
            from virtaal.views.langview import LanguageView
            self.view = LanguageView(self)
            self.view.show()
        srclang = store_controller.store.get_source_language(
        ) or self.source_lang.code
        tgtlang = store_controller.store.get_target_language(
        ) or self.target_lang.code
        self.set_language_pair(srclang, tgtlang)
        self.target_lang.nplurals = self.target_lang.nplurals or store_controller.get_nplurals(
        )