Exemplo n.º 1
0
def xwiki_xml_properties_to_properties(path, path_prefix, lang):
    """Convert an XWiki XML file with properties to a java properties file"""
    relative_dir_path = os.path.dirname(path)
    file_name = os.path.basename(path).split(".")[0]

    properties = PropertiesFile()
    xml_file = XmlFile(path_prefix + path)
    content = xml_file.get_tag_content("content")
    properties.load(content)

    properties_path = "{}.translation/{}_{}.properties".format(
        path_prefix, relative_dir_path + "/" + file_name, lang)
    properties.write(properties_path)
Exemplo n.º 2
0
def xwiki_xml_to_properties(path, path_prefix, lang):
    """Convert an XWiki XML file to a java properties file"""
    relative_dir_path = os.path.dirname(path)
    file_name = os.path.basename(path).split(".")[0]

    properties = PropertiesFile()
    xml_file = XmlFile(path_prefix + path)
    properties.set_value("{}.title".format(file_name),
                         xml_file.get_tag_content("title"))
    properties.set_value("{}.content".format(file_name),
                         xml_file.get_tag_content("content"))

    properties_path = "{}.translation/{}_{}.properties".format(
        path_prefix, relative_dir_path + "/" + file_name, lang)
    properties.write(properties_path)
Exemplo n.º 3
0
def open_or_create_translation(base_file_name, file_name, file_type, lang):
    properties = PropertiesFile()
    xml = None
    if file_type == FileType.PROPERTIES:
        if os.path.isfile(file_name):
            with open(file_name, "r") as f:
                document = f.read()
            properties.load(document)
    else:
        if os.path.isfile(file_name):
            xml = XmlFile()
            xml.load(file_name)
        else:
            xml = XmlFile.create_xml_file(file_name, base_file_name, lang)
        properties.load(xml.get_tag_content('content'))
    return (properties, xml)
Exemplo n.º 4
0
def convert(file_type, file_name_properties, path_prefix, base_file_name, lang):
    """Convert the translation file depending on its type"""
    # Current file name with xml extension
    file_name_xml = file_name_properties.replace('_{}.properties'.format(lang),
                                                 '.{}.xml'.format(lang))

    if file_type in [FileType.XML_PROPERTIES, FileType.XML]:
        if not os.path.isfile(PATH_PREFIX + file_name_xml):
            # Create the xml translation if it doesn't exists
            XmlFile.create_xml_file(PATH_PREFIX + file_name_xml, path_prefix + base_file_name, lang)

    if file_type == FileType.PROPERTIES:
        properties_to_xwiki_properties(file_name_properties, path_prefix, base_file_name, lang)
    elif file_type == FileType.XML_PROPERTIES:
        properties_to_xwiki_xml_properties(file_name_xml, path_prefix, base_file_name, lang)
    elif file_type == FileType.XML:
        properties_to_xwiki_xml(file_name_xml, path_prefix, lang)
Exemplo n.º 5
0
def xwiki_xml_properties_to_properties(file_path, path_prefix, lang):
    """Convert an XWiki XML file with properties to a java properties file"""
    # Directory of the translation file
    relative_dir_path = os.path.dirname(file_path)
    # Translation file name without the extension
    file_name = os.path.basename(file_path).split(".")[0]

    # Weblate translation file
    properties_path = "{}_{}.properties".format(
        path_prefix + TRANSLATION_PREFIX + relative_dir_path + "/" + file_name, lang)
    properties = PropertiesFile()

    xml_file = XmlFile()
    xml_file.load(path_prefix + file_path)
    content = xml_file.get_tag_content("content")
    properties.load(content)
    properties.filter_import()

    properties.write(properties_path)
Exemplo n.º 6
0
def properties_to_xwiki_xml(file_path, path_prefix, lang):
    """Convert a java properties file to an XWiki XML file"""
    # Directory of the translation file
    relative_dir_path = os.path.dirname(file_path)
    # Translation file name without the extension
    file_name = os.path.basename(file_path).split(".")[0]

    # Weblate translation file
    properties_path = "{}_{}.properties".format(
        path_prefix + TRANSLATION_PREFIX + relative_dir_path + "/" + file_name, lang)
    properties = PropertiesFile()
    with open(properties_path, "r") as f_properties:
        properties.load(f_properties.read())

    title = properties.get_value("{}.title".format(file_name))
    content = properties.get_value("{}.content".format(file_name))
    xml_file = XmlFile()
    xml_file.load(path_prefix + file_path)
    xml_file.set_tag_content("title", title)
    xml_file.set_tag_content("content", content)
    xml_file.write(path_prefix + file_path)
Exemplo n.º 7
0
def properties_to_xwiki_xml(path, path_prefix, lang):
    """Convert a java properties file to an XWiki XML file"""
    relative_dir_path = os.path.dirname(path)
    file_name = os.path.basename(path).split(".")[0]

    properties_path = "{}.translation/{}_{}.properties".format(
        path_prefix, relative_dir_path + "/" + file_name, lang)
    properties = PropertiesFile()
    with open(properties_path, "r") as f_properties:
        properties.load(f_properties.read())

    title = properties.get_value("{}.title".format(file_name))
    content = properties.get_value("{}.content".format(file_name))
    xml_file = XmlFile(path_prefix + path)
    xml_file.set_tag_content("title", title)
    xml_file.set_tag_content("content", content)
    xml_file.write()
Exemplo n.º 8
0
def open_or_create_translation(base_file_name, file_name, file_type, lang):
    properties = PropertiesFile()
    xml = None
    if file_type == FileType.PROPERTIES:
        if os.path.isfile(file_name):
            with open(file_name, "r") as f:
                document = f.read()
            properties.load(document)
    else:
        if os.path.isfile(file_name):
            xml = XmlFile()
            xml.load(file_name)
        else:
            xml = XmlFile.create_xml_file(file_name, base_file_name, lang)
        properties.load(xml.get_tag_content('content'))
    return (properties, xml)
Exemplo n.º 9
0
def xwiki_xml_to_properties(file_path, path_prefix, lang):
    """Convert an XWiki XML file to a java properties file"""
    # Directory of the translation file
    relative_dir_path = os.path.dirname(file_path)
    # Translation file name without the extension
    file_name = os.path.basename(file_path).split(".")[0]

    # Weblate translation file
    properties_path = "{}_{}.properties".format(
        path_prefix + TRANSLATION_PREFIX + relative_dir_path + "/" + file_name, lang)
    properties = PropertiesFile()

    xml_file = XmlFile()
    xml_file.load(path_prefix + file_path)
    properties.set_value("{}.title".format(file_name), xml_file.get_tag_content("title"))
    properties.set_value("{}.content".format(file_name),
                         xml_file.get_tag_content("content", ['xwikidoc']))

    properties.write(properties_path)
Exemplo n.º 10
0
def properties_to_xwiki_xml_properties(file_path, path_prefix, base_file_name, lang):
    """Convert a java properties file to an XWiki XML file with properties"""
    # Directory of the translation file
    relative_dir_path = os.path.dirname(file_path)
    # Translation file name without the extension
    file_name = os.path.basename(file_path).split(".")[0]

    # Weblate translation file
    properties_path = "{}_{}.properties".format(
        path_prefix + TRANSLATION_PREFIX + relative_dir_path + "/" + file_name, lang)
    properties = PropertiesFile()
    with open(properties_path, "r") as f_properties:
        properties.load(f_properties.read())

    # Use the base translation file as template
    xml_base_file = XmlFile()
    xml_base_file.load(path_prefix + base_file_name)
    content = xml_base_file.get_tag_content("content")
    base_properties = PropertiesFile()
    base_properties.load(content)

    # Replace keys with the current translation
    base_properties.replace_with(properties)
    base_properties.filter_export()

    xml_file = XmlFile()
    xml_file.load(path_prefix + file_path)
    xml_file.set_tag_content("content", base_properties.document)
    xml_file.write(path_prefix + file_path)
Exemplo n.º 11
0
def properties_to_xwiki_xml(file_path, path_prefix, base_file_name, lang):
    """Convert a java properties file to an XWiki XML file"""
    # Directory of the translation file
    relative_dir_path = os.path.dirname(file_path)
    # Translation file name without the extension
    file_name = os.path.basename(file_path).split(".")[0]

    # Weblate translation file
    properties_path = "{}_{}.properties".format(
        path_prefix + TRANSLATION_PREFIX + relative_dir_path + "/" + file_name,
        lang)
    properties = PropertiesFile()
    with open(properties_path, "r") as f_properties:
        properties.load(f_properties.read())

    title = properties.get_value("{}.title".format(file_name))
    content = properties.get_value("{}.content".format(file_name))

    if content:
        if not os.path.isfile(path_prefix + file_path):
            # Create the xml translation if it doesn't exists
            XmlFile.create_xml_file(path_prefix + file_path,
                                    path_prefix + base_file_name, lang)

        xml_file = XmlFile()
        xml_file.load(path_prefix + file_path)
        xml_file.set_tag_content("title", title)
        xml_file.set_tag_content("content", content, ['xwikidoc'])
        xml_file.write(path_prefix + file_path)
    else:
        print "Warning: {} translation content is empty. Skipping it.".format(
            properties_path)
Exemplo n.º 12
0
def properties_to_xwiki_xml_properties(file_path, path_prefix, base_file_name, lang):
    """Convert a java properties file to an XWiki XML file with properties"""
    # Directory of the translation file
    relative_dir_path = os.path.dirname(file_path)
    # Translation file name without the extension
    file_name = os.path.basename(file_path).split(".")[0]

    # Weblate translation file
    properties_path = "{}_{}.properties".format(
        path_prefix + TRANSLATION_PREFIX + relative_dir_path + "/" + file_name, lang)
    properties = PropertiesFile()
    with open(properties_path, "r") as f_properties:
        properties.load(f_properties.read())

    if not properties.is_empty():
        if not os.path.isfile(path_prefix + file_path):
            # Create the xml translation if it doesn't exists
            XmlFile.create_xml_file(path_prefix + file_path, path_prefix + base_file_name, lang)

        # Use the base translation file as template
        xml_base_file = XmlFile()
        xml_base_file.load(path_prefix + base_file_name)
        content = xml_base_file.get_tag_content("content")
        base_properties = PropertiesFile()
        base_properties.load(content)

        # Replace keys with the current translation
        base_properties.replace_with(properties)
        base_properties.filter_export()

        xml_file = XmlFile()
        xml_file.load(path_prefix + file_path)
        xml_file.set_tag_content("content", base_properties.document)
        xml_file.write(path_prefix + file_path)
    else:
        print "Warning: {} translation is empty. Skipping it.".format(properties_path)
Exemplo n.º 13
0
def properties_to_xwiki_xml(file_path, path_prefix, base_file_name, lang):
    """Convert a java properties file to an XWiki XML file"""
    # Directory of the translation file
    relative_dir_path = os.path.dirname(file_path)
    # Translation file name without the extension
    file_name = os.path.basename(file_path).split(".")[0]

    # Weblate translation file
    properties_path = "{}_{}.properties".format(
        path_prefix + TRANSLATION_PREFIX + relative_dir_path + "/" + file_name, lang)
    properties = PropertiesFile()
    with open(properties_path, "r") as f_properties:
        properties.load(f_properties.read())

    title = properties.get_value("{}.title".format(file_name))
    content = properties.get_value("{}.content".format(file_name))

    if content:
        if not os.path.isfile(path_prefix + file_path):
            # Create the xml translation if it doesn't exists
            XmlFile.create_xml_file(path_prefix + file_path, path_prefix + base_file_name, lang)

        xml_file = XmlFile()
        xml_file.load(path_prefix + file_path)
        xml_file.set_tag_content("title", title)
        xml_file.set_tag_content("content", content, ['xwikidoc'])
        xml_file.write(path_prefix + file_path)
    else:
        print "Warning: {} translation content is empty. Skipping it.".format(properties_path)