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]

get_locale_string_from_xaml(xaml_base_path, key)

Get a localized string from a XAML resource dictionary file.

Derives the resource dictionary path from xaml_base_path by replacing the .xaml extension with .ResourceDictionary.{locale}.xaml. Tries all locale codes of the current locale, then falls back to the default (English) locale. Returns key unchanged when no match is found.

Parameters:

Name Type Description Default
xaml_base_path str

Path to the base XAML file, e.g. from script.get_bundle_file("MyForm.xaml").

required
key str

Resource key to look up.

required

Returns:

Type Description
str

Localized string, or key as the final fallback.

Examples:

from pyrevit import script
from pyrevit.coreutils import applocales
xaml = script.get_bundle_file("MyForm.xaml")
title = applocales.get_locale_string_from_xaml(xaml, "WindowTitle")
Source code in pyrevitlib/pyrevit/coreutils/applocales.py
def get_locale_string_from_xaml(xaml_base_path, key):
    """Get a localized string from a XAML resource dictionary file.

    Derives the resource dictionary path from ``xaml_base_path`` by replacing
    the ``.xaml`` extension with ``.ResourceDictionary.{locale}.xaml``.
    Tries all locale codes of the current locale, then falls back to the
    default (English) locale. Returns ``key`` unchanged when no match is found.

    Args:
        xaml_base_path (str): Path to the base XAML file, e.g. from
            ``script.get_bundle_file("MyForm.xaml")``.
        key (str): Resource key to look up.

    Returns:
        (str): Localized string, or ``key`` as the final fallback.

    Examples:
        ```python
        from pyrevit import script
        from pyrevit.coreutils import applocales
        xaml = script.get_bundle_file("MyForm.xaml")
        title = applocales.get_locale_string_from_xaml(xaml, "WindowTitle")
        ```
    """

    def _read_key(filepath):
        try:
            root = ET.parse(filepath).getroot()
            x_ns = "http://schemas.microsoft.com/winfx/2006/xaml"
            sys_ns = "clr-namespace:System;assembly=mscorlib"
            for elem in root.iter("{%s}String" % sys_ns):
                if elem.get("{%s}Key" % x_ns) == key:
                    return elem.text
        except Exception:
            pass
        return None

    base = os.path.splitext(xaml_base_path)[0]
    locale = get_current_applocale() or DEFAULT_LOCALE

    for code in locale.locale_codes:
        path = "{}.ResourceDictionary.{}.xaml".format(base, code)
        if os.path.isfile(path):
            result = _read_key(path)
            if result is not None:
                return result
            break  # right locale file found but key missing — fall to English

    if locale is not DEFAULT_LOCALE:
        for code in DEFAULT_LOCALE.locale_codes:
            path = "{}.ResourceDictionary.{}.xaml".format(base, code)
            if os.path.isfile(path):
                result = _read_key(path)
                if result is not None:
                    return result
                break

    return key