Skip to content

applocales

Provide conversion services between python.locale and host languages.

Attributes

DEFAULT_LANG_DIR = 'LTR' module-attribute

DEFAULT_LOCALE = AppLocale(lang_type=ApplicationServices.LanguageType.English_USA, lang_name='English USA', locale_codes=['en_us', 'english']) module-attribute

APP_LOCALES = [DEFAULT_LOCALE, AppLocale(lang_type=ApplicationServices.LanguageType.German, lang_name='Deutsch', locale_codes=['de_de', 'german']), AppLocale(lang_type=ApplicationServices.LanguageType.Spanish, lang_name='español', locale_codes=['es_es', 'spanish']), AppLocale(lang_type=ApplicationServices.LanguageType.French, lang_name='français', locale_codes=['fr_fr', 'french']), AppLocale(lang_type=ApplicationServices.LanguageType.Italian, lang_name='italiano', locale_codes=['it_it', 'italian']), AppLocale(lang_type=ApplicationServices.LanguageType.Dutch, lang_name='Nederlands', locale_codes=['nl_nl', 'nl_be', 'dutch']), AppLocale(lang_type=ApplicationServices.LanguageType.Chinese_Simplified, lang_name='简体中文', locale_codes=['chinese_s', 'chinese']), AppLocale(lang_type=ApplicationServices.LanguageType.Chinese_Traditional, lang_name='繁體中文', locale_codes=['chinese_t', 'chinese']), AppLocale(lang_type=ApplicationServices.LanguageType.Japanese, lang_name='日本語', locale_codes=['ja', 'japanese']), AppLocale(lang_type=ApplicationServices.LanguageType.Korean, lang_name='한국어', locale_codes=['ko', 'korean']), AppLocale(lang_type=ApplicationServices.LanguageType.Russian, lang_name='Русский', locale_codes=['ru', 'russian']), AppLocale(lang_type=ApplicationServices.LanguageType.Czech, lang_name='Čeština', locale_codes=['cs', 'czech']), AppLocale(lang_type=ApplicationServices.LanguageType.Polish, lang_name='Polski', locale_codes=['pl', 'polish']), AppLocale(lang_type=ApplicationServices.LanguageType.Hungarian, lang_name='Magyar', locale_codes=['hu', 'hungarian']), AppLocale(lang_type=ApplicationServices.LanguageType.Brazilian_Portuguese, lang_name='Português do Brasil', locale_codes=['pt_br', 'portuguese_brazil', 'brazilian', 'portuguese', 'pt_pt'])] module-attribute

Classes

AppLocale(lang_type, locale_codes, lang_name=None, lang_dir=DEFAULT_LANG_DIR)

Bases: object

Type representing a language option.

Source code in pyrevitlib/pyrevit/coreutils/applocales.py
def __init__(self,
             lang_type,
             locale_codes,
             lang_name=None,
             lang_dir=DEFAULT_LANG_DIR):
    if isinstance(lang_type, ApplicationServices.LanguageType):
        self.lang_type = lang_type
    elif isinstance(lang_type, str):
        self.lang_type = lang_type
    self.lang_name = lang_name
    self.lang_dir = lang_dir
    self.locale_codes = locale_codes
    if self.locale_codes:
        self.locale_code = self.locale_codes[0]

Attributes

lang_type = lang_type instance-attribute
lang_name = lang_name instance-attribute
lang_dir = lang_dir instance-attribute
locale_codes = locale_codes instance-attribute
locale_code = self.locale_codes[0] instance-attribute

Functions

get_applocale_by_local_code(locale_code)

Return application locale by locale code.

Parameters:

Name Type Description Default
locale_code str

locale code

required

Returns:

Type Description
AppLocale

application locale

Source code in pyrevitlib/pyrevit/coreutils/applocales.py
def get_applocale_by_local_code(locale_code):
    """Return application locale by locale code.

    Args:
        locale_code (str): locale code

    Returns:
        (AppLocale): application locale
    """
    for applocale in APP_LOCALES:
        if locale_code in applocale.locale_codes:
            return applocale

get_applocale_by_lang_type(lang_type)

Return application locale by language type.

Parameters:

Name Type Description Default
lang_type LanguageType | str

language type

required

Returns:

Type Description
AppLocale

application locale

Source code in pyrevitlib/pyrevit/coreutils/applocales.py
def get_applocale_by_lang_type(lang_type):
    """Return application locale by language type.

    Args:
        lang_type (ApplicationServices.LanguageType | str): language type

    Returns:
        (AppLocale): application locale
    """
    for applocale in APP_LOCALES:
        if lang_type == applocale.lang_type:
            return applocale

get_applocale_by_lang_name(lang_name)

Return application locale by language name.

Parameters:

Name Type Description Default
lang_name str

language name

required

Returns:

Type Description
AppLocale

application locale

Source code in pyrevitlib/pyrevit/coreutils/applocales.py
def get_applocale_by_lang_name(lang_name):
    """Return application locale by language name.

    Args:
        lang_name (str): language name

    Returns:
        (AppLocale): application locale
    """
    for applocale in APP_LOCALES:
        if lang_name in {applocale.lang_name, str(applocale.lang_type)}:
            return applocale

get_current_applocale()

Return the current locale.

This is the user locale, if set, or the host application locale otherwise.

Returns:

Type Description
AppLocale

current locale

Source code in pyrevitlib/pyrevit/coreutils/applocales.py
def get_current_applocale():
    """Return the current locale.

    This is the user locale, if set, or the host application locale otherwise.

    Returns:
        (AppLocale): current locale
    """
    if user_config.user_locale:
        return get_applocale_by_local_code(user_config.user_locale)
    return get_applocale_by_lang_type(HOST_APP.language)

get_host_applocale()

Return host application locale.

Returns:

Type Description
AppLocale

host application locale

Source code in pyrevitlib/pyrevit/coreutils/applocales.py
def get_host_applocale():
    """Return host application locale.

    Returns:
        (AppLocale): host application locale
    """
    return get_applocale_by_lang_type(HOST_APP.language)

get_locale_string(string_dict)

Returns the correct string from given dict based on host language.

Parameters:

Name Type Description Default
string_dict dict[str, str]

dict of strings in various locales

required

Returns:

Type Description
str

string in correct locale

Examples:

data = {"en_us":"Hello", "chinese_s":"你好"}
from pyrevit.coreutils import applocales
# assuming running Revit is Chinese
applocales.get_locale_string(data)
"你好"

Source code in pyrevitlib/pyrevit/coreutils/applocales.py
def get_locale_string(string_dict):
    """Returns the correct string from given dict based on host language.

    Args:
        string_dict (dict[str, str]): dict of strings in various locales

    Returns:
        (str): string in correct locale

    Examples:
        ```python
        data = {"en_us":"Hello", "chinese_s":"你好"}
        from pyrevit.coreutils import applocales
        # assuming running Revit is Chinese
        applocales.get_locale_string(data)
        ```
        "你好"
    """
    applocale = get_applocale_by_local_code(user_config.user_locale)
    if applocale:
        local_codes = applocale.locale_codes + DEFAULT_LOCALE.locale_codes
    else:
        local_codes = DEFAULT_LOCALE.locale_codes
    for locale_code in local_codes:
        if locale_code in string_dict:
            return string_dict[locale_code]