Pyslate API reference

Config variables

class pyslate.config.DefaultConfig

Default values for configuration options of Pyslate.

If you want to overwrite defaults, create a subclass of DefaultConfig and overwrite interesting values in your class’ constructor. You can also create a function which instantiates DefaultConfig, overwrites some values and then gives it to Pyslate’s constructor as keyword-argument “config”. Please note you can use keyword-arguments of Pyslate constructor to specify own parser, cache and backend. Keyword arguments set in Pyslate constructor have higher precedence than values from the config.

pyslate module

class pyslate.pyslate.Pyslate(language, backend=None, config=<pyslate.config.DefaultConfig object>, context=None, cache=None, locales=None, parser=None, on_missing_tag_key_callback=None)

Main class responsible for all the translation and localization. When constructed it’s necessary to set language, backend. It’s possible to set custom config, context dict and instance of cache class.

l(value, short=False)

Method returning localized string representation of a value specified in the argument. Currently it guarantees to correctly localize the following types: float, datetime.date, datetime.datetime, datetime.time If value cannot be localized then its string representation is returned.

Parameters:value – value to be localized
Returns:string representation of the value, localized if being instance of the supported types
localize(value, short=False)

Method returning localized string representation of a value specified in the argument. Currently it guarantees to correctly localize the following types: float, datetime.date, datetime.datetime, datetime.time If value cannot be localized then its string representation is returned.

Parameters:value – value to be localized
Returns:string representation of the value, localized if being instance of the supported types
register_decorator(decorator_name, function, is_deterministic=False, language=None)

Registers a new decorator which will be available in the translation system. Overwrites any other decorator or function with the same name.

Parameters:
  • decorator_name – name of the decorator available in the translation system
  • function – a function whose only argument is input string string and returns an altered string
  • is_deterministic – if True then return value of the decorator for specified arguments will be cached to be reused in the future. Keep it disabled unless you really know you need it.
  • language – language for which decorator will be available, If unspecified then it’s available for all languages
register_function(tag_name, function, is_deterministic=False, language=None)

Registers a new custom function which will be available in the translation system. Overwrites any other decorator or function with the same name.

Parameters:
  • tag_name – name base tag key for which the function is accessible in an inner tag field. See the examples.
  • function – function with 3 arguments: - a helper (instance of PyslateHelper), which is a facade for translating specified tags or setting grammatical form of the custom function - tag_name - params (keyword arguments specified in Pyslate.translate)
  • is_deterministic – if True then return value and grammatical form of the function for specified arguments will be cached to be reused in the future. Keep it disabled unless you really know you need it.
  • language – language for which function will be available. If unspecified then it’s available for all languages
t(tag_name, **kwargs)

Method returning fully translated tag value for a specified tag_name using kwargs as a list of keyword arguments. If there’s no tag value for specified language, then tag value for fallback language (or global fallback language) is used.

Parameters:
  • tag_name – tag key which should be translated. It can contain decorators
  • kwargs – arguments which can be interpolated into tag value
Returns:

translated value for specified tag key.

translate(tag_name, **kwargs)

Method returning fully translated tag value for a specified tag_name using kwargs as a list of keyword arguments. If there’s no tag value for specified language, then tag value for fallback language (or global fallback language) is used.

Parameters:
  • tag_name – tag key which should be translated. It can contain decorators
  • kwargs – arguments which can be interpolated into tag value
Returns:

translated value for specified tag key.

class pyslate.pyslate.PyslateHelper(pyslate)

Class given as a first argument of the custom functions. It’s a facade which allows for translating or getting a grammatical form for specified tag keys. It also allows for setting a grammatical form of an entity represented by this custom function. This way a custom function can be a black-box treated exactly the same as a normal inner tag field.

form(tag_name, **kwargs)

Returns grammatical form of the tag_name tag (which can be None).

get_suffix(tag_name)
pass_the_suffix(tag_name)
return_form(form)

Specifies grammatical form of the entity represented by the custom function. It can later be retrieved by other fields of the tag value.

Parameters:form – grammatical form of this custom function
translation(tag_name, **kwargs)

Returns a translated string for specified tag_name and kwargs. Delegates to Pyslate.translate method.

translation_and_form(tag_name, **kwargs)

If you need both translation and grammatical form, then it’s more efficient to use it to get both at once. Returns a tuple whose first element is a translated string for specified tag_name and kwargs. The second element is grammatical form of the tag (which can be None).