Registry / serialization / translationstring

translationstring

JSON →
library1.4pypypi✓ verified 35d ago

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.

serialization
Install & Compatibility
Where this runs
tested against v1.4 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.925 runs
installs and imports cleanly · install 0.0s · import 0.003s · 17.9MB
glibc
py 3.103.925 runs
installs and imports cleanly · install 1.5s · import 0.002s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

from translationstring import TranslationString
from translationstring import TranslationStringFactory
from translationstring import Translator

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)}")
Debug
Known footguns
breakingVersion 1.4 of `translationstring` dropped support for Python 2.6, 3.2, and 3.3. Projects using these older Python versions will need to stick to an earlier `translationstring` version or upgrade their Python environment.
gotchaWhen using the `TranslationString` constructor with both `msgid` and `default`, if `default` contains replacement markers (e.g., `${number}`), then the `msgid` should *not* contain replacement markers. This is a common pattern for 'opaque' message identifiers.
gotchaA `TranslationString` object, when treated like a normal string, will display its `msgid` value. Actual translation (and interpolation of `mapping` values) only occurs when its `ugettext` method is called or when it's passed through a `translationstring.Translator` callable.
gotchaWhen passing a `TranslationString` to a `Translator` callable, any `domain` or `mapping` arguments provided directly to the `Translator` will override or combine with the `domain` and `mapping` attributes already present on the `TranslationString` instance.
gotchaThe package encourages the convention of assigning a `TranslationStringFactory` instance to the variable `_` (e.g., `_ = TranslationStringFactory('mydomain')`). This is a common `gettext` convention and is often supported by translation file generation tools.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
14 hits · last 30 days
gptbot
4
ahrefsbot
4
bytedance
3
bingbot
2
script
1
Resources