示例#1
0
def write_filtered_strings_txt(filtered, filepath, languages=None):
    logging.info("Writing strings to file {0}".format(filepath))
    strings_txt = StringsTxt()
    strings_dict = {key : dict(strings_txt.translations[key]) for key in filtered}
    strings_txt.translations = strings_dict
    strings_txt.comments_and_tags = {}
    strings_txt.write_formatted(filepath, languages=languages)
示例#2
0
def do_missing(args):
    ios = set(grep_ios())
    strings_txt_keys = set(StringsTxt().translations.keys())
    missing = ios - strings_txt_keys

    if missing:
        for m in missing:
            logging.info(m)
        exit(1)
    logging.info("Ok. No missing strings.")
    exit(0)
示例#3
0
    def __init__(self):
        args = self.parse_args()
        self.folder_path = args.folder
        self.all_po_files = self.find_all_po_files()

        if (args.strings_txt):
            self.dest_file = StringsTxt(args.strings_txt)
        elif (args.categories_txt):
            self.dest_file = CategoriesTxt(args.categories_txt)
        else:
            raise RuntimeError("You must specify either -s or -c")
示例#4
0
def do_single(args):
    ios = grep_ios()
    android = grep_android()

    new_tags = generate_auto_tags(ios, android)

    filtered = ios
    filtered.update(android)

    strings_txt = StringsTxt()
    strings_txt.translations = {
        key: dict(strings_txt.translations[key])
        for key in filtered
    }

    strings_txt.comments_and_tags = new_comments_and_tags(
        strings_txt, filtered, new_tags)

    path = args.output if isabs(args.output) else "{0}/{1}".format(
        OMIM_ROOT, args.output)
    strings_txt.write_formatted(languages=args.langs, target_file=path)

    if args.generate:
        exec_shell("{}/unix/generate_localizations.sh".format(OMIM_ROOT),
                   args.output, args.output)
示例#5
0
def do_single(args):
    ios = grep_ios()
    android = grep_android()

    new_tags = generate_auto_tags(ios, android)

    filtered = ios
    filtered.update(android)

    strings_txt = StringsTxt()
    strings_txt.translations = {key: dict(strings_txt.translations[key]) for key in filtered}

    strings_txt.comments_and_tags = new_comments_and_tags(strings_txt, filtered, new_tags)

    path = args.output if isabs(args.output) else "{0}/{1}".format(OMIM_ROOT, args.output)
    strings_txt.write_formatted(languages=args.langs, target_file=path)

    if args.generate:
        exec_shell(
            "{}/unix/generate_localizations.sh".format(OMIM_ROOT),
            args.output, args.output
        )
示例#6
0
def write_filtered_strings_txt(filtered, filepath, languages=None):
    logging.info("Writing strings to file {0}".format(filepath))
    strings_txt = StringsTxt()
    strings_dict = {
        key: dict(strings_txt.translations[key])
        for key in filtered
    }
    strings_txt.translations = strings_dict
    strings_txt.comments_and_tags = {}
    strings_txt.write_formatted(filepath, languages=languages)
示例#7
0
 def __init__(self):
     args = self.parse_args()
     self.folder_path = args.folder
     self.all_po_files = self.find_all_po_files()
     self.strings_txt = StringsTxt(args.strings_txt)
示例#8
0
class PoParser:
    def __init__(self):
        args = self.parse_args()
        self.folder_path = args.folder
        self.all_po_files = self.find_all_po_files()
        self.strings_txt = StringsTxt(args.strings_txt)


    def find_all_po_files(self):
        return [
            f for f in listdir(self.folder_path)
            if isfile(join(self.folder_path, f)) and f.endswith(".po")
        ]


    def parse_files(self):
        for po_file in self.all_po_files:
            self._parse_one_file(
                join(self.folder_path, po_file),
                self.lang_from_filename(po_file)
            )


    def lang_from_filename(self, filename):
        # file names are in this format: strings_ru_RU.po
        lang = filename[len("strings_"):-len(".po")]
        if lang in TRANSFORMATION_TABLE:
            return TRANSFORMATION_TABLE[lang]
        return lang[:2]


    def _parse_one_file(self, filepath, lang):
        self.translations = defaultdict(str)
        current_key = None
        string_started = False
        with open(filepath) as infile:
            for line in infile:
                if line.startswith("msgid"):
                    current_key = self.clean_line(line,"msgid")
                elif line.startswith("msgstr"):
                    if not current_key:
                        continue
                    translation = self.clean_line(line, "msgstr")
                    if not translation:
                        print("No translation for key {} in file {}".format(current_key, filepath))
                        continue
                    self.strings_txt.add_translation(
                        translation,
                        key="[{}]".format(current_key),
                        lang=lang
                    )
                    string_started = True

                elif not line or line.startswith("#"):
                    string_started = False
                    current_key = None

                else:
                    if not string_started:
                        continue
                    self.strings_txt.append_to_translation(current_key, lang, self.clean_line(line))


    def clean_line(self, line, prefix=""):
        return line[len(prefix):].strip().strip('"')


    def parse_args(self):
        parser = ArgumentParser(
            description="""
            A script for parsing stirngs in the PO format, which is used by our
            translation partners.
            """
        )

        parser.add_argument(
            "-f", "--folder",
            dest="folder", required=True,
            help="""Path to the folder where the PO files are. Required."""
        )

        parser.add_argument(
            "-s", "--strings",
            dest="strings_txt", required=True,
            help="""The path to the strings.txt file. The strings from the po
            files will be added to that strings.txt file."""
        )

        return parser.parse_args()