Ejemplo n.º 1
0
def translate(text, files):
    """Search a list of .lang files for a translation"""
    lang = fix_case(translation.get_language())

    tweaked_text = tweak_message(text)

    for file_ in files:
        key = "dotlang-%s-%s" % (lang, file_)
        rel_path = os.path.join('locale', lang, '%s.lang' % file_)

        trans = cache.get(key)
        if trans is None:
            path = os.path.join(settings.ROOT, rel_path)
            trans = parse(path)
            cache.set(key, trans, settings.DOTLANG_CACHE)

        if tweaked_text in trans:
            original = FORMAT_IDENTIFIER_RE.findall(text)
            translated = FORMAT_IDENTIFIER_RE.findall(trans[tweaked_text])
            if set(original) != set(translated):
                explanation = ('The translation has a different set of '
                               'replaced text (aka %s)')
                message = '%s\n\n%s\n%s' % (explanation, text,
                                            trans[tweaked_text])
                mail_error(rel_path, message)
                return Markup(text)
            return Markup(trans[tweaked_text])
    return Markup(text)
Ejemplo n.º 2
0
def translate(text, files):
    """Search a list of .lang files for a translation"""
    lang = fix_case(translation.get_language())

    tweaked_text = tweak_message(text)

    for file_ in files:
        key = "dotlang-%s-%s" % (lang, file_)

        trans = cache.get(key)
        if trans is None:
            path = os.path.join(settings.ROOT, 'locale', lang,
                                '%s.lang' % file_)
            trans = parse(path)
            cache.set(key, trans, settings.DOTLANG_CACHE)

        if tweaked_text in trans:
            original = FORMAT_IDENTIFIER_RE.findall(text)
            translated = FORMAT_IDENTIFIER_RE.findall(trans[tweaked_text])
            if original != translated:
                message = '%s\n%s' % (text, trans[tweaked_text])
                mail_error(file_, message)
                return text
            return trans[tweaked_text]
    return text
Ejemplo n.º 3
0
def translate(text, files):
    """Search a list of .lang files for a translation"""
    lang = fix_case(translation.get_language())

    tweaked_text = tweak_message(text)

    for file_ in files:
        key = "dotlang-%s-%s" % (lang, file_)
        rel_path = os.path.join('locale', lang, '%s.lang' % file_)

        trans = cache.get(key)
        if trans is None:
            path = os.path.join(settings.ROOT, rel_path)
            trans = parse(path)
            cache.set(key, trans, settings.DOTLANG_CACHE)

        if tweaked_text in trans:
            original = FORMAT_IDENTIFIER_RE.findall(text)
            translated = FORMAT_IDENTIFIER_RE.findall(trans[tweaked_text])
            if set(original) != set(translated):
                explanation = ('The translation has a different set of '
                               'replaced text (aka %s)')
                message = '%s\n\n%s\n%s' % (explanation, text,
                                            trans[tweaked_text])
                mail_error(rel_path, message)
                return Markup(text)
            return Markup(trans[tweaked_text])
    return Markup(text)
Ejemplo n.º 4
0
    def handle(self, *args, **options):
        try:
            apps = settings.DB_LOCALIZE
        except AttributeError:
            raise CommandError("DB_LOCALIZE setting is not defined!")

        strings = []
        # Oh god this is terrible
        for app, models in apps.items():
            for model, params in models.items():
                model_class = get_model(app, model)
                for item in model_class.objects.all().values():
                    for attr in params["attrs"]:
                        msg = {
                            "id": item[attr],
                            "auto_comments": ["DB: %s.%s.%s" % (app, model, attr)],
                            "user_comments": params["comments"],
                        }
                        strings.append(msg)

        po_dir = os.path.abspath(options.get("outputdir"))
        po_filename = os.path.join(po_dir, "%s.pot" % TEXT_DOMAIN)

        try:
            with open(po_filename, "r") as f:
                catalog = read_po(f)
        except IOError:
            catalog = Catalog()

        for msg in strings:
            catalog.add(
                tweak_message(msg["id"]), auto_comments=msg["auto_comments"], user_comments=msg["user_comments"]
            )

        with open(po_filename, "w+") as f:
            write_po(f, catalog)