<commit_before># -*- encoding: utf_8 -*-
#
# /usr/bin/python
#
# translate.py
#
# (c) D.C.-G. 2014
#
# Translation module for Python 2.7, especialy for Albow 2 and MCEdit 1.8 799
#
#-------------------------------------------------------------------------------
"""This module adds translation functionnalities to Albow.

It looks for locale files in a sub folder 'lang' according to the system
default, or user specified locale.

The locale files are text files where 'entries' are defined.

Syntax example:

o0 This is the first entry.
t0 Ceci est la première entrée.
o1 Here is the second one.
You can see it is splitted on two lines.
t1 Voici la deuxième.
Vous pouvez constater qu'elle est sur deux lignes.

The 'oN' markers on the begining of a line marks the original string;
the 'tN', the translations.
Strings can be splited on several lines and contain any sort of
characters (thus, be aware of unicode).

Files are named according to their language name, e.g. fr_FR.trn for
french.
The extension .trn is an arbitrary convention.
The file 'template.trn' is given to be used as a base for other ones. It
contains the strings used in Albow. They are not translated.

As convention, english is considered as the default language, and the
file en_EN.trn would never be present.

Usage:

You need, a least, these three function in your program:
* setLangPath to define the translations location;
* buildTranslation to build up the cache;
* _ function to translate strings.
"""

import logging
log = logging.getLogger(__name__)

import os
import re
import codecs
import json
import resource

import platform, locale
def getPlatInfo(**kwargs):
    """kwargs: dict: {"module_name": module,...}
    used to display version information about modules."""
    log.debug("*** Platform information")
    log.debug("    System: %s"%platform.system())
    log.debug("    Release: %s"%platform.release())
    log.debug("    Version: %s"%platform.version())
    log.debug("    Architecture: %s, %s"%platform.architecture())
    log.debug("    Dist: %s, %s, %s"%platform.dist())
    log.debug("    Machine: %s"%platform.machine())
    log.debug("    Processor: %s"%platform.processor())
    log.debug("    Locale: %s"%locale.getdefaultlocale()[0])
    log.debug("    Encoding: %s"%locale.getdefaultlocale()[1])
    reVer = re.compile(r"__version__|_version_|__version|_version|version|"
                       "__ver__|_ver_|__ver|_ver|ver", re.IGNORECASE)
    for name, mod in kwargs.items():
        s = "%s"%dir(mod)
        verObjNames = list(re.findall(reVer, s))
        if len(verObjNames) > 0:
            while verObjNames:
                verObjName = verObjNames.pop()
                verObj = getattr(mod, verObjName, None)
                if verObj:
                    if type(verObj) in (str, unicode, int, list, tuple):
                        ver = "%s"%verObj
                        break
                    elif "%s"%type(verObj) == "<type 'module'>":
                        verObjNames += ["%s.%s"%(verObjName, a) for a in re.findall(reVer, "%s"%dir(verObj))]
                    else:
                        ver = verObj()
                else:
                    ver = "%s"%type(verObj)
            log.debug("    %s version: %s"%(name, ver))
    log.debug("***")


enc = locale.getdefaultlocale()[1]

string_cache = {}
font_lang_cache = {}
langPath = os.sep.join((".", "lang"))
lang = "Default"

# template building
strNum = 0
template = {} # {"string": number}
buildTemplate = False
trnHeader = """# TRANSLATION BASICS
# 
# This file works by mapping original English strings(o##) to the new translated strings(t##)
# As long as the numbers match, it will translate the specified English string to the new language.
# Any text formatting is preserved, so new lines, tabs, spaces, brackets, quotes and other special characters can be used.
# 
# The space (' ') separating the strings from the numbers is mandatory.
# The file must also be encoded in UTF-8 or it won't work. Most editors should support this.
# 
# See TRANSLATION.txt for more detailed information.
#"""

#-------------------------------------------------------------------------------
# Translation loading and mapping functions
#-------------------------------------------------------------------------------
def _(string, doNotTranslate=False):
    """Returns the translated 'string', or 'string' itself if no translation found."""
    if doNotTranslate:
        return string
    if type(string) not in (str, unicode):
        return string
    if buildTemplate:
        global template
        global strNum
        if u"%s"%string not in template.values():
            template[strNum] = u"%s"%string
            strNum += 1
    return string_cache.get(string, string.replace("\n", "\n\n"))

#-------------------------------------------------------------------------------
def saveTemplate():
    if buildTemplate:
        fName = os.path.abspath(os.path.join(getLangPath(), "template.trn"))
        f = codecs.open(fName, "w", "utf-8")
        f.write(trnHeader)
        keys = template.keys()
        keys.sort()
        for key in keys:
            value = template[key]
            f.write(u"\no%s %s\nt%s "%(key, value, key))
        f.close()

#-------------------------------------------------------------------------------
def setLangPath(path):
    """Changes the default 'lang' folder path. Retrun True if the path is valid, False otherwise."""
    log.debug("setLangPath <<<")
    log.debug("Argument 'path': %s"%path)
    path = os.path.normpath(os.path.abspath(path))
    log.debug("   Norm and abs: %s"%path)
    log.debug("os.access(path, os.F_OK): %s; os.path.isdir(path): %s; os.access(path, os.R_OK): %s"%(os.access(path, os.F_OK), os.path.isdir(path), os.access(path, os.R_OK)))<commit_msg>Corrected blank UI elements when their strings aren't translated.<commit_after># -*- encoding: utf_8 -*-
#
# /usr/bin/python
#
# translate.py
#
# (c) D.C.-G. 2014
#
# Translation module for Python 2.7, especialy for Albow 2 and MCEdit 1.8 799
#
#-------------------------------------------------------------------------------
"""This module adds translation functionnalities to Albow.

It looks for locale files in a sub folder 'lang' according to the system
default, or user specified locale.

The locale files are text files where 'entries' are defined.

Syntax example:

o0 This is the first entry.
t0 Ceci est la première entrée.
o1 Here is the second one.
You can see it is splitted on two lines.
t1 Voici la deuxième.
Vous pouvez constater qu'elle est sur deux lignes.

The 'oN' markers on the begining of a line marks the original string;
the 'tN', the translations.
Strings can be splited on several lines and contain any sort of
characters (thus, be aware of unicode).

Files are named according to their language name, e.g. fr_FR.trn for
french.
The extension .trn is an arbitrary convention.
The file 'template.trn' is given to be used as a base for other ones. It
contains the strings used in Albow. They are not translated.

As convention, english is considered as the default language, and the
file en_EN.trn would never be present.

Usage:

You need, a least, these three function in your program:
* setLangPath to define the translations location;
* buildTranslation to build up the cache;
* _ function to translate strings.
"""

import logging
log = logging.getLogger(__name__)

import os
import re
import codecs
import json
import resource

import platform, locale
def getPlatInfo(**kwargs):
    """kwargs: dict: {"module_name": module,...}
    used to display version information about modules."""
    log.debug("*** Platform information")
    log.debug("    System: %s"%platform.system())
    log.debug("    Release: %s"%platform.release())
    log.debug("    Version: %s"%platform.version())
    log.debug("    Architecture: %s, %s"%platform.architecture())
    log.debug("    Dist: %s, %s, %s"%platform.dist())
    log.debug("    Machine: %s"%platform.machine())
    log.debug("    Processor: %s"%platform.processor())
    log.debug("    Locale: %s"%locale.getdefaultlocale()[0])
    log.debug("    Encoding: %s"%locale.getdefaultlocale()[1])
    reVer = re.compile(r"__version__|_version_|__version|_version|version|"
                       "__ver__|_ver_|__ver|_ver|ver", re.IGNORECASE)
    for name, mod in kwargs.items():
        s = "%s"%dir(mod)
        verObjNames = list(re.findall(reVer, s))
        if len(verObjNames) > 0:
            while verObjNames:
                verObjName = verObjNames.pop()
                verObj = getattr(mod, verObjName, None)
                if verObj:
                    if type(verObj) in (str, unicode, int, list, tuple):
                        ver = "%s"%verObj
                        break
                    elif "%s"%type(verObj) == "<type 'module'>":
                        verObjNames += ["%s.%s"%(verObjName, a) for a in re.findall(reVer, "%s"%dir(verObj))]
                    else:
                        ver = verObj()
                else:
                    ver = "%s"%type(verObj)
            log.debug("    %s version: %s"%(name, ver))
    log.debug("***")


enc = locale.getdefaultlocale()[1]

string_cache = {}
font_lang_cache = {}
langPath = os.sep.join((".", "lang"))
lang = "Default"

# template building
strNum = 0
template = {} # {"string": number}
buildTemplate = False
trnHeader = """# TRANSLATION BASICS
# 
# This file works by mapping original English strings(o##) to the new translated strings(t##)
# As long as the numbers match, it will translate the specified English string to the new language.
# Any text formatting is preserved, so new lines, tabs, spaces, brackets, quotes and other special characters can be used.
# 
# The space (' ') separating the strings from the numbers is mandatory.
# The file must also be encoded in UTF-8 or it won't work. Most editors should support this.
# 
# See TRANSLATION.txt for more detailed information.
#"""

#-------------------------------------------------------------------------------
# Translation loading and mapping functions
#-------------------------------------------------------------------------------
def _(string, doNotTranslate=False):
    """Returns the translated 'string', or 'string' itself if no translation found."""
    if doNotTranslate:
        return string
    if type(string) not in (str, unicode):
        return string
    if buildTemplate:
        global template
        global strNum
        if u"%s"%string not in template.values():
            template[strNum] = u"%s"%string
            strNum += 1
    return string_cache.get(string, string.replace("\n", "\n\n")) or string

#-------------------------------------------------------------------------------
def saveTemplate():
    if buildTemplate:
        fName = os.path.abspath(os.path.join(getLangPath(), "template.trn"))
        f = codecs.open(fName, "w", "utf-8")
        f.write(trnHeader)
        keys = template.keys()
        keys.sort()
        for key in keys:
            value = template[key]
            f.write(u"\no%s %s\nt%s "%(key, value, key))
        f.close()

#-------------------------------------------------------------------------------
def setLangPath(path):
    """Changes the default 'lang' folder path. Retrun True if the path is valid, False otherwise."""
    log.debug("setLangPath <<<")
    log.debug("Argument 'path': %s"%path)
    path = os.path.normpath(os.path.abspath(path))
    log.debug("   Norm and abs: %s"%path)
    log.debug("os.access(path, os.F_OK): %s; os.path.isdir(path): %s; os.access(path, os.R_OK): %s"%(os.access(path, os.F_OK), os.path.isdir(path), os.access(path, os.R_OK)))
{'fraction_non_alphanumeric': 0.1845942700291645, 'fraction_numerical': 0.004288900325956425, 'mean_word_length': 2.2786839145106863, 'pattern_counts': {'":': 6, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4395231', 'n_tokens_mistral': 3369, 'n_tokens_neox': 3298, 'n_words': 1141}