예제 #1
0
 def test_singular(self):
     entry = POEntry()
     entry.msgid = 'A lovely day for a cup of tea.'
     expected = u'\xc0 l\xf6v\xe9ly d\xe4y f\xf6r \xe4 \xe7\xfcp \xf6f t\xe9\xe4. Lorem i#'
     self.converter.convert_msg(entry)
     self.assertEquals(entry.msgstr, expected)
예제 #2
0
def command_main_po(from_language, to_language, input, output=None, po=None, pot=None, pdf=False, undetected_strings=[], fake=None):
    extension=input.split(".")[1:][0]

    if output is None:
        output=f"{input}.{to_language}.{extension}"
    if po is None:
        po=f"{input}.{to_language}.po"
    if pot is None:
        pot=f"{input}.{to_language}.pot"
        
    logfile=f"{output}.log"
    log=open(logfile, "w")


    s=_(f"Translating '{input}' from '{from_language }' to '{to_language}'")
    print(s)
    log.write(s+"\n")
    s=_(f"  - Output: {output}")
    print(s)
    log.write(s+"\n")
    s=_(f"  - File catalog pot: {pot}")
    print(s)
    log.write(s+"\n")
    s=_(f"  - File catalog po: {po}")
    print(s)
    log.write(s+"\n")
    s=_(f"  - Translation log: {logfile}")
    print(s)
    log.write(s+"\n")
    
    original_xlf="original.xlf"
    if path.exists(original_xlf) is True:
        remove(original_xlf)
    
    doc=ODT(input)
    
    
    # Creating a list of ordered document strings
    run_check(["odf2xliff", input, original_xlf])
    ##Leemos sources
    mytree = ET.parse(original_xlf)
    myroot = mytree.getroot()
    file_=myroot[0]
    body=file_[0]
    sources=set()
    for e in body:
        if e[0].text is not None:#<source>
            s=innercontent(e[0])
            arr=removeTags(s)
            for t in arr:
                sources.add(t)
                
    for s in undetected_strings:
        sources.add(s)
    sources=list(sources)

    
    
    
    if path.exists(original_xlf) is True:
        remove(original_xlf)
    
    # Creating pot file
    file_pot = POFile()
    file_pot.metadata = {
        'Project-Id-Version': '1.0',
        'Report-Msgid-Bugs-To': '*****@*****.**',
        'POT-Creation-Date': '2007-10-18 14:00+0100',
        'PO-Revision-Date': '2007-10-18 14:00+0100',
        'Last-Translator': 'you <*****@*****.**>',
        'Language-Team': 'English <*****@*****.**>',
        'MIME-Version': '1.0',
        'Content-Type': 'text/plain; charset=utf-8',
        'Content-Transfer-Encoding': '8bit',
    }
    for i,  source in enumerate(sources):
        entry = POEntry(
            msgid=source,
            msgstr='', 
            occurrences=[('string', str(i)),]
        )
        file_pot.append(entry)
    file_pot.save(pot)
    
    #Merging pot with out po file
    if path.exists(po)==False:
        run_check(["msginit", "-i", pot,  "-o", po])
    run_check(["msgmerge","-N", "--no-wrap","-U", po, pot])
    
    # Creates a dictionary of translations
    dict_po={}
    file_po = pofile(po)
    for i, entry in enumerate(file_po):
        if fake is True:
            dict_po[entry.msgid]=f"{{{entry.msgid}}}"
        else:
            if entry.msgstr == "":
                dict_po[entry.msgid]=entry.msgid
            else:
                dict_po[entry.msgid]=entry.msgstr
        
    #Converts sources to entries (list of tuples)
    entries=[]
    for source in sources:
        entries.append((source, dict_po[source] ))
    entries=sorted(entries,  key=lambda item: len(item[0]), reverse=True)
    
    
    #Creating our translated output
    log.write ("\n\n==== TRANSLATION LOGS ====\n")
    warns=""
    for i, (find, replace) in enumerate(entries):
        number=doc.findall_and_replace(find,  replace)
        rs=replaced_entries_before(find,  i,  entries)
        s=f"""
* Entry {i}

    Original: {find}

    Translation set {number} times: {replace}
"""
        log.write(s)
        if len(rs)>0:
            warns=warns + s +"    WARNING: This replacement could overwrite before replacements. Perhaps you'll need to overwrite your result later with unogenerator.\n"
            for s in rs:
                warns =warns + f"        - '{s[0]}' ==> '{s[1]}'\n"
                
    if warns!="":
        log.write ("\n\n==== WARNINGS ====\n")
        log.write(warns)
            
    doc.save(output)
    if pdf is True:
        doc.export_pdf(output+".pdf")
    doc.close()
    print(f"{len(sources)} messages found. {len(file_po.translated_entries())} translated. {len(file_po.untranslated_entries())} untranslated.")
    
    log.close()
예제 #3
0
 def test_pass(self):
     entry = POEntry(msgid="Source", msgstr="Translation")
     status = Status()
     status.step(entry)
     self.assertTrue(no_location_validator(status))
예제 #4
0
 def test_first_entry(self):
     entry = POEntry(msgid="Source", msgstr="Translation")
     status = Status()
     status.step(entry)
     self.assertTrue(sort_validator(status))
예제 #5
0
 def test_fail_fuzzy(self):
     # Fuzzy translations are considered untranslated
     entry = POEntry(msgid="Source", msgstr="Translation", flags=['fuzzy'])
     status = Status()
     status.step(entry)
     self.assertFalse(untranslated_validator(status))
예제 #6
0
 def test_fail_obsolete(self):
     # Obsolete translations are considered untranslated
     entry = POEntry(msgid="Source", msgstr="Translation", obsolete=True)
     status = Status()
     status.step(entry)
     self.assertFalse(untranslated_validator(status))
예제 #7
0
 def test_fail(self):
     entry = POEntry(msgid="Source", msgstr="Translation", obsolete=True)
     status = Status()
     status.step(entry)
     self.assertFalse(obsolete_validator(status))
예제 #8
0
 def test_fail_missing(self):
     entry = POEntry(msgid="Source", msgstr="")
     status = Status()
     status.step(entry)
     self.assertFalse(untranslated_validator(status))
예제 #9
0
 def test_no_comment(self):
     self.assertRaises(AssertionError, test_comment, POEntry(msgid="c"))
예제 #10
0
 def test_fail_fuzzy(self):
     entry = POEntry(msgid="Source", msgstr="Translation", flags=['fuzzy'])
     status = Status()
     status.step(entry)
     self.assertFalse(fuzzy_validator(status))
예제 #11
0
 def test_unnecessary_markup(self):
     self.assertRaises(AssertionError, test_markup,
                       POEntry(msgid="<b>test string</b>"))
예제 #12
0
    def test_ok(self):
        # no markup
        test_markup(POEntry(msgid="test string"))

        # internal markup
        test_markup(POEntry(msgid="<b>test</b> string"))
예제 #13
0
def translate_single_relay_locations(country_translator, city_translator,
                                     countries, locale):
    """
  A helper function to generate the relay-locations.po for the given locale.

  The `countries` argument is an array value that's contained within the "countries" key of the
  relay location list.
  """

    po = POFile(encoding='utf-8', check_for_duplicates=True)
    po.metadata = {"Content-Type": "text/plain; charset=utf-8"}
    locale_out_dir = path.join(LOCALE_OUT_DIR, locale)
    output_path = path.join(locale_out_dir, RELAY_LOCATIONS_PO_FILENAME)

    hits = 0
    misses = 0

    if not path.exists(locale_out_dir):
        os.makedirs(locale_out_dir)

    for country in countries:
        country_name = country.get("name")
        country_code = country.get("code")

        translated_country_name = country_translator.translate(
            locale, country_code)
        found_country_translation = translated_country_name is not None
        # Default to empty string if no translation was found
        if found_country_translation:
            hits += 1
        else:
            translated_country_name = ""
            misses += 1

        log_message = "{} ({}) -> \"{}\"".format(country_name, country_code,
                                                 translated_country_name)
        if found_country_translation:
            print(c.green(log_message))
        else:
            print(c.orange(log_message))

        # translate country
        entry = POEntry(msgid=country_name,
                        msgstr=translated_country_name,
                        comment=country_code.upper())
        po.append(entry)

        # translate cities
        cities = country.get("cities")
        if cities is None:
            print(
                c.orange("Skip {} ({}) because no cities were found.".format(
                    country_name, country_code)))
            continue

        for city in cities:
            city_name = city.get("name")
            city_code = city.get("code")
            if city_name is None:
                raise ValueError("Missing the name field in city record.")

            # Make sure to append the US state back to the translated name of the city
            if country_code == "us":
                split = city_name.rsplit(",", 2)
                translated_name = city_translator.translate(
                    locale, split[0].strip())

                if translated_name is not None and len(split) > 1:
                    translated_name = "{}, {}".format(translated_name,
                                                      split[1].strip())
            else:
                translated_name = city_translator.translate(locale, city_name)

            # Default to empty string if no translation was found
            found_translation = translated_name is not None
            if found_translation:
                hits += 1
            else:
                translated_name = ""
                misses += 1

            log_message = "{} ({}) -> \"{}\"".format(city_name, city_code,
                                                     translated_name)
            if found_translation:
                print(c.green(log_message))
            else:
                print(c.orange(log_message))

            entry = POEntry(msgid=city_name,
                            msgstr=translated_name,
                            comment="{} {}".format(country_code.upper(),
                                                   city_code.upper()))

            try:
                po.append(entry)
            except ValueError as err:
                print(c.orange("Cannot add an entry: {}".format(err)))

    po.save(output_path)

    return (hits, misses)
예제 #14
0
def extract_cities_po():
    input_path = get_shape_path("ne_50m_populated_places")
    stats = []

    for locale in os.listdir(LOCALE_DIR):
        locale_dir = path.join(LOCALE_DIR, locale)
        locale_out_dir = path.join(LOCALE_OUT_DIR, locale)

        if os.path.isdir(locale_dir):
            po = POFile(encoding='utf-8', check_for_duplicates=True)
            po.metadata = {"Content-Type": "text/plain; charset=utf-8"}
            output_path = path.join(locale_out_dir, CITIES_PO_FILENAME)
            hits = 0
            misses = 0

            if not path.exists(locale_out_dir):
                os.makedirs(locale_out_dir)

            print("Generating {}".format(output_path))

            with fiona.open(input_path) as source:
                for feat in source:
                    props = lower_dict_keys(feat["properties"])

                    if props["pop_max"] >= POPULATION_MAX_FILTER:
                        name_key = "name_" + map_locale(locale)
                        name_fallback = "name"

                        if props.get(name_key) is not None:
                            translated_name = props.get(name_key)
                            hits += 1
                        elif props.get(name_fallback) is not None:
                            translated_name = props.get(name_fallback)
                            print(
                                c.orange("Missing translation for {}".format(
                                    translated_name)))
                            misses += 1
                        else:
                            raise ValueError(
                                "Cannot find the translation for {}. Probe keys: {}"
                                .format(locale, (name_key, name_fallback)))

                        entry = POEntry(msgid=props.get("name"),
                                        msgstr=translated_name)

                        try:
                            po.append(entry)
                        except ValueError as err:
                            print(
                                c.orange(
                                    "Cannot add an entry: {}".format(err)))

            sort_pofile_entries(po)
            po.save(output_path)
            print(
                c.green("Extracted {} cities to {}".format(
                    len(po), output_path)))

            stats.append((locale, hits, misses))

    print_stats_table("Cities translations", stats)
예제 #15
0
def extract_countries_po():
    input_path = get_shape_path("ne_50m_admin_0_countries")

    for locale in os.listdir(LOCALE_DIR):
        locale_dir = path.join(LOCALE_DIR, locale)
        locale_out_dir = path.join(LOCALE_OUT_DIR, locale)

        if os.path.isdir(locale_dir):
            with fiona.open(input_path) as source:
                po = POFile(encoding='utf-8', check_for_duplicates=True)
                po.metadata = {"Content-Type": "text/plain; charset=utf-8"}
                output_path = path.join(locale_out_dir, COUNTRIES_PO_FILENAME)

                if not path.exists(locale_out_dir):
                    os.makedirs(locale_out_dir)

                print("Generating {}".format(output_path))

                for feat in source:
                    props = lower_dict_keys(feat["properties"])

                    name_key = "name_" + map_locale(locale)
                    name_fallback = "name"

                    country_name = props.get("name")
                    formal_country_name = props.get("formal_en", country_name)

                    if props.get(name_key) is not None:
                        translated_name = props.get(name_key)
                    elif props.get(name_fallback) is not None:
                        translated_name = props.get(name_fallback)
                        print(
                            c.orange("Missing translation for {}".format(
                                translated_name)))
                    else:
                        raise ValueError(
                            "Cannot find the translation for {}. Probe keys: {}"
                            .format(locale, (name_key, name_fallback)))

                    entry = POEntry(msgid=country_name, msgstr=translated_name)
                    po.append(entry)

                    # add additional record for the formal country name.
                    if country_name != formal_country_name and formal_country_name is not None:
                        entry = POEntry(msgid=formal_country_name,
                                        msgstr=translated_name)
                        po.append(entry)

                    # exception for the US
                    if props.get("iso_a3") == "USA":
                        entry = POEntry(msgid="USA", msgstr=translated_name)
                        po.append(entry)

                    # exception for the UK
                    if props.get("iso_a3") == "GBR":
                        entry = POEntry(msgid="UK", msgstr=translated_name)
                        po.append(entry)

                sort_pofile_entries(po)
                po.save(output_path)
                print(
                    c.green("Extracted {} countries for {} to {}".format(
                        len(po), locale, output_path)))