translationstring

1.4 · maintenance · verified Fri Apr 10

Translationstring is a utility library for internationalization (i18n) primarily used by various Repoze and Pyramid packages. It provides core components like a `TranslationString` class, a `TranslationStringFactory` for creating translation strings, and primitives for translation and pluralization. These objects behave like Unicode strings but carry additional metadata for higher-level translation systems. The current version is 1.4, and it appears to be in maintenance mode, with its last release in July 2020.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `TranslationString` and `TranslationStringFactory` to mark translatable text, and then how to use a `Translator` object to process them. It includes an example with a mock `gettext.NullTranslations` instance to simulate translation, showing singular/plural forms and interpolation.

import gettext
from translationstring import TranslationString, TranslationStringFactory, Translator

# 1. Create a mock translations object (e.g., from gettext or Babel)
# In a real app, this would load .mo files for a specific locale.
class MockTranslations(gettext.NullTranslations):
    def ugettext(self, message):
        # Simulate a translation, for demo just uppercase it if in 'app' domain
        # In a real scenario, this would look up in a catalog.
        if self.domain == 'app' and message == 'Hello':
            return 'Bonjour'
        return message
    
    def ungettext(self, singular, plural, n):
        if self.domain == 'app' and singular == '${num} item':
            if n == 1: return '${num} article'
            else: return '${num} articles'
        return super().ungettext(singular, plural, n)

mock_translations = MockTranslations()
mock_translations.add_domain('app')
mock_translations.set_output_charset('utf-8')

# 2. Create a Translator callable from the translations object
translator_callable = Translator(mock_translations)

# 3. Use TranslationString for individual strings
ts_hello = TranslationString('Hello', domain='app')
ts_item_plural = TranslationString('${num} item', plural='${num} items', mapping={'num': 1}, domain='app')
ts_item_plural_many = TranslationString('${num} item', plural='${num} items', mapping={'num': 5}, domain='app')

print(f"Raw TranslationString (no explicit translation): {ts_hello}")
print(f"Translated via callable: {transl_hello = translator_callable(ts_hello)}")
print(f"Plural translated (1 item): {transl_plural_1 = translator_callable(ts_item_plural, n=1)}")
print(f"Plural translated (5 items): {transl_plural_5 = translator_callable(ts_item_plural_many, n=5)}")

# 4. Use TranslationStringFactory for domain-prefixed strings (common convention is to assign to '_')
_ = TranslationStringFactory('app')
ts_factory_example = _('Welcome', default='Welcome to our application!')
print(f"Factory string (no translation in mock): {transl_welcome = translator_callable(ts_factory_example)}")

# Demonstrate interpolation
ts_interp = TranslationString('Hello ${name}', mapping={'name': 'World'})
print(f"Interpolated string: {transl_interp = translator_callable(ts_interp)}")

view raw JSON →