示例#1
0
def load_messages():
    g.messages.clear()

    files = dirs.get_readable_i18n_files("messages.po", language, default_language=False)

    for lang, pofile in files:
        try:
            po = polib.pofile(pofile)
            for entry in po.translated_entries():
                g.messages[entry.msgid] = entry.msgstr
        except IOError: pass # silently ignore non-existing files
示例#2
0
def load_data_str():
    g.data_strings.clear()

    files = dirs.get_readable_i18n_files("data_str.po", language, default_language=False)

    for lang, pofile in files:
        try:
            po = polib.pofile(pofile)
            for entry in po.translated_entries():
                g.data_strings[(entry.msgctxt, entry.msgid)] = entry.msgstr
        except IOError:
            pass # silently ignore non-existing files
示例#3
0
def _load_po_file(translation_table, pofilename, use_context=True):
    translation_table.clear()

    files = dirs.get_readable_i18n_files(pofilename,
                                         language,
                                         default_language=False)

    for lang, pofile in files:
        try:
            po = polib.pofile(pofile)
        except IOError:
            # silently ignore non-existing files
            continue
        for entry in po.translated_entries():
            key = (
                entry.msgctxt,
                entry.msgid) if entry.msgctxt and use_context else entry.msgid
            translation_table[key] = entry.msgstr
示例#4
0
def _load_mo_file(pofilename):

    files = dirs.get_readable_i18n_files(pofilename,
                                         language,
                                         default_language=False)

    for lang, pofile in files:
        try:
            po = polib.pofile(pofile)

            # Use hash to check whether the.po file has changed, then generate .mo file as needed
            sha_base_filename = os.path.basename(
                os.path.dirname(pofile)) + '_' + os.path.basename(pofile)
            sha_filename = dirs.get_writable_file_in_dirs(
                sha_base_filename + ".sha1", "i18n")

            previous_hash = ''
            new_hash = ''
            if os.path.exists(sha_filename):
                with open(sha_filename, 'r') as sha_file:
                    previous_hash = sha_file.read()

            with open(pofile, 'rb') as currentpo:
                new_hash = hashlib.sha1(currentpo.read()).hexdigest()

            # Ensure directory exists before writing
            locale_mo_dir = os.path.join(_get_main_localedir(), lang,
                                         'LC_MESSAGES')
            dirs.makedirs_if_not_exist(locale_mo_dir)
            mofile_path = os.path.join(
                locale_mo_dir, TEXTDOMAIN_PREFIX +
                os.path.basename(pofile).split('.')[0] + '.mo')

            # Create MO file and write new hash
            if new_hash != previous_hash or not os.path.exists(mofile_path):
                print("Installing translation file: " + mofile_path)
                po.save_as_mofile(mofile_path)
                with open(sha_filename, 'w') as sha_file:
                    sha_file.write(new_hash)

        except IOError:
            # silently ignore non-existing files
            continue
示例#5
0
def load_story_defs(lang=None):
    story = g.story = {}

    story_files = dirs.get_readable_i18n_files("story.dat", lang)

    if len(story_files) == 0:
        print("Story is missing. Skipping.")
        return

    # Take the last story file, story is never partially translated.
    story_file = open(story_files[-1][1], 'r', encoding='utf-8')

    section_name = ""
    segment = ""
    line_num = 1

    for line in story_file.readlines():
        if line and line != "\n":
            if line[0] == "#":
                pass  # Ignore comment
            elif line[0] == "[":
                if line[-2] == "]":
                    section_name = line[1:-2]
                    story[section_name] = []
                else:
                    sys.stderr.write(
                        "Line start with [ and is not a section at line %d.\n"
                        % line_num)
            elif line[0] == "|":
                segment += line[1:]
            else:
                # TODO: Parse command
                sys.stderr.write("Invalid command at line %d.\n" % line_num)
        else:
            if segment:
                story[section_name].append(segment)
                segment = ""

        line_num += 1

    # Add last segment.
    if segment:
        story[section_name].append(segment)