question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow definition of custom locales

See original GitHub issue

For a current project I would like to define a custom locale (let’s call it an ‘in-house-dialect’). It would be nice to be able to add it, preferably by inheriting the properties from an already available language.

Rough sketch:

pybabel init -i messages.pot -d translations --locale="dialect" --base="en"

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:3
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
kolyptocommented, Aug 25, 2021

@lt3Dev sure, by all means, please do! Let’s say the code is under the MIT license or beerware license 😄

1reaction
kolyptocommented, Sep 18, 2020

In the meanwhile, this snippet can be used to hack locale data:


def hack_babel_core_to_support_custom_locales(custom_locales: dict):
    """ Hack Babel core to make it support custom locale names

    Args:
        custom_locales: Mapping from { custom name => ordinary name }
    """
    from babel.core import get_global

    # In order for Babel to know "en_CUSTOM", we have to hack its database and put our custom 
    # locale names there.
    # This database is pickle-loaded from a .dat file and cached, so we only have to do it once.
    db = get_global('likely_subtags')
    for custom_name in custom_locales:
        db[custom_name] = custom_name

    # Also, monkey-patch the exists() and load() functions that load locale data from 'babel/locale-data'
    import babel.localedata

    # Originals
    o_exists, o_load = babel.localedata.exists, babel.localedata.load

    # Make sure we do not patch twice
    if o_exists.__module__ != __name__:
        # Definitions
        def exists(name):
            # Convert custom names to normalized names
            name = custom_locales.get(name, name)
            return o_exists(name)

        def load(name, merge_inherited=True):
            # Convert custom names to normalized names
            name = custom_locales.get(name, name)
            return o_load(name, merge_inherited)

        # Patch
        babel.localedata.exists = exists
        babel.localedata.load = load

        # See that they actually exist
        for normalized_name in custom_locales.values():
            assert o_exists(normalized_name)

Usage:

hack_babel_core_to_support_custom_locales({
    'en_CUSTOM': 'en',  # custom => original
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Working with Custom Locales - Win32 apps | Microsoft Learn
Your application can retrieve information about a particular supplemental locale only when that locale is the currently selected user locale.
Read more >
Local custom definition and meaning | Collins English Dictionary
Local custom definition: A custom is an activity, a way of behaving , or an event which is usual or traditional in... |...
Read more >
Define locales - ServiceNow Docs
The base system allows you to specify your locale so information such as dates, ... Prepare to left-pad number fields in custom tables....
Read more >
System and Custom locales - OnceHub Support Center
Custom locales enable you to modify any Customer interface text. All texts are editable and can be found by using the browser search...
Read more >
Content localization through locales | Contentful
Locales let you define content that needs to be available in multiple languages | Field-level | Entry-level | Content type and Space-level localization....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found