Django Configuration

TML library comes with many customization options that can be adjusted based on your application needs.

Basics

TML = {  
  'application': {
    'key': 'YOUR_APPLICATION_KEY'
  },
  'monkeypatch': True,   # support legacy translations
  'cache': {
    'enabled': True,
    'adapter': 'memcached',
    'backend': 'pylibmc',
    'namespace': 'myapp'
  },
  'api_client': 'tml.api.client.Client',
  'logger': {
    'enabled': True,
    'path': pj(BASE_DIR, 'logs', 'tml.log'),
    'level': logging.DEBUG
  },
  'env_generators': ('tml.tools.viewing_user.get_viewing_user',),
  'strict_mode': False,
  'default_source': 'index',
  'version_check_interval': 3600,
}
Name Type Default Description
application Object
key String Provide your key for application key.
monkeypatch Boolean false This option facilitates Tml to be backward compatible with django i18n engine. You can still use django native tags for translating your strings.
cache Object Define your cache configuration here. Read more about caching.
data_preprocessors Object Preprocessors applied on translateable string before actual processing. For example list preprocessors preprocess list like context variable into meaningful text token.
env_generators Object request.user Sometimes you do not need to provide substitution token variable. For example, viewing_user env generator could be substituted by the return value of env generator.
logger Object TML log that tracks interaction with SDK by http and supressed exceptions while translating string.

Monkeypatching

It is often the case that your project is partially translated using django.utils.translation module at the time you want connect TML SDK. If you want start using TML immediately, you may want to configure SDK in compatible mode with Django native modules. Just set monkeypatch flag in your TML settings and all your translation strings on the page would be registered under dashboard through a single http request:

TML = {  
   ...
   'monkeypatch': True
}

This flag also provides translation extensions to python builtin data types: str, list, datetime.date, datetime.datetime. For example, support you would need to work with localized dates:

from tml.strings import to_string  
dummy_dt = datetime.datetime(year=1989, month=10, day=7)  
assert to_string('Friday, October 7, 1989') == dummy_dt.tml_translate(':verbose')  
assert to_string('10/07/1989') == dummy_dt.tml_translate('%m/%d/%Y')  
assert to_string('October 7th') == dummy_dt.tml_translate('{month_name} {days::ord}')  

You can read more on Navigation section.

Environment generators

Sometimes you may need to use the same data token values across your entire application. For instance, a current user can be used in multiple places:

{% tr with viewing_user=request.user %}
{viewing_user|he,she} logged in.
{% endtr %}

Instead of providing the same token value in every place you refer to the current user, you can configure it as environment generator:

TML = {  
   'env_generators': ('tml.tools.viewing_user.get_viewing_user',),
}

Now you can use the data token without having to specify its value:

{% tr %} {viewing_user|he,she} logged in. {% endtr %}

Context Data Extraction

TML library has a built in rules engine that is used to evaluate language rules and provide optimal translations.

For instance, if you have a user object defined as following:

michael = User.new(name='Michael', gender='male')  

And if we have a translation of a form:

{% tr with user=michael %}
  {user} updated {user | his, her} profile
{% endtr %}

The rules engine will automatically extract the gender of the user from the gender attribute/method and use its value to determine which translation to use.

But how did it know to use the gender attribute and how did it know to figure out that male value corresponded to male gender?

This information is stored in the configuration as well and can be easily changed for specific projects. For example, instead of gender method you use sex and instead of 'male' and 'female' you use 1 and 0. In this case you can remap the methods to the following

TML = {  
  ...
  'context_rules': {
        'gender': {
            'variables': {
                '@gender': lambda obj: obj.sex == 1 and 'male' or 'female'
            }
        }
    }
}

A similar approach can be used to map values for number, genders, date, time, list and other objects.