0

I am writing an app that creates/provides various Python decorators. I'd like this app to be localized, including the names of the decorators. The decorators would ultimately be used by other developers who are using my app as a framework (think of my app as a test framework).

Ideally, I'd like to have the ability to create the decorators dynamically based on the localization settings.

For instance, if the language selected is English, I'd like my decorator to be named "apple", and if it's French, I'd like it to be named "pomme".

In the Python code, it would either look like:

@apple
def my_func():
    pass

or

@pomme
def my_func():
    pass

I ultimately want to have the flexibility to add many more languages without having to modify or duplicate a bunch of code. Ideally, I would only have one decorator function that would handle "apple" or "pomme" depending on the settings.

What's the best way to achieve this?

5
  • 2
    Decorators are just functions following a particular protocol (they take a function as argument and return a function). And since Python features first-class functions, you can simply assign them to any name(s) you like. If you want to do this dynamically / programmatically, the __init__.py of your package would be an obvious place. Commented Aug 8, 2014 at 20:14
  • 5
    That being said, I would advise against this. In my personal opinion, source code, including variable names, documentation and docstrings, should be in English. The rest of the language is (keywords, statements, etc.), mixing it with any native language makes it much harder to read. Commented Aug 8, 2014 at 20:17
  • 4
    I agree, don't do this. Write the code in English (although I would recommend translating the documentation if you think it is likely that people with English as a second language will use the framework). Commented Aug 8, 2014 at 20:24
  • 1
    Also consider, you would have to provide different example code for each language, as all the function, variable and keyword names would be different. Commented Aug 8, 2014 at 20:24
  • Thanks for all your comments. Lukas, can you please elaborate on how the __init__.py of my package could be used to do this? Commented Aug 8, 2014 at 22:04

1 Answer 1

1

First, don't do this. This will bring many problems upon you and make life much harder for you and your users. Anyway, python is very dynamic so you can do that.

Setup your package like that:

yourpackage/
    __init__.py
    decorators.py

In decorators.py:

# List all decorators you want to publish. Use english names here.
__all__ = ['apple', 'orange', ...]

# Here come implementations named in english
def apple(...):
    ...

...

In __init__.py:

# Whatever over submodules export or just []
__all__ = [...]

from . import decorators

# Get locale somehow
LOCALE = ...

# This translation function could be as complex as you wish
# I use a lookup in hard-coded dict
TRANSLATIONS = {
    'fr': {'apple': u'pomme', ...},
    ...
}
def _translate_name(name):
    # If something is not translated we use default english name,
    # could be easily changed to raise error
    return TRANSLATIONS.get(LOCALE, {}).get(name, name)

# Generate dynamic attributes to current module and add them to __all__
import sys
this_module = sys.modules[__name__]

for name in decorators.__all__:
    translated = _translate_name(name)
    setattr(this_module, translated, getattr(decorators, name))
    __all__.append(translated)

Managing __all__ in __init__.py is optional. This is to allow from yourmodule import *.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.