langstrings.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:idealoom 作者: conversence 项目源码 文件源码
def identify_locale(self, locale_code, data, certainty=False):
        # A translation service proposes a data identification.
        # the information is deemed confirmed if it fits the initial
        # hypothesis given at LSE creation.
        changed = False
        old_locale_code = self.locale
        langstring = self.langstring or (
            LangString.get(self.langstring_id) if self.langstring_id else None)
        if self.is_machine_translated:
            raise RuntimeError("Why identify a machine-translated locale?")
        data = data or {}
        original = self.locale_identification_data_json.get("original", None)
        if not locale_code or locale_code == LocaleLabel.UNDEFINED:
            if not self.locale or self.locale == LocaleLabel.UNDEFINED:
                # replace id data with new one.
                if original:
                    data['original'] = original
                self.locale_identification_data_json = data
            return False
        elif original and locale_code == original:
            if locale_code != old_locale_code:
                self.locale = locale_code
                changed = True
            self.locale_identification_data_json = data
            self.locale_confirmed = True
        elif locale_code != old_locale_code:
            if self.locale_confirmed:
                if certainty:
                    raise RuntimeError("Conflict of certainty")
                # keep the old confirming data
                return False
            # compare data? replacing with new for now.
            if not original and self.locale_identification_data:
                original = LocaleLabel.UNDEFINED
            original = original or old_locale_code
            if original != locale_code and original != LocaleLabel.UNDEFINED:
                data["original"] = original
            self.locale = locale_code
            changed = True
            self.locale_identification_data_json = data
            self.locale_confirmed = certainty
        else:
            if original and original != locale_code:
                data['original'] = original
            self.locale_identification_data_json = data
            self.locale_confirmed = certainty or locale_code == original
        if changed:
            if langstring:
                langstring.remove_translations_of(self)
                # Re-adding to verify there's no conflict
                added = langstring.add_entry(self, certainty)
                if added is None:
                    # We identified an entry with something that existed
                    # as a known original. Not sure what to do now,
                    # reverting just in case.
                    self.locale_code = old_locale_code
                    changed = False
        return changed
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号