Registry / serialization / translationstring

translationstring

JSON →
library1.4pypypi✓ verified 25d 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.

pip install translationstring
INSTALL
IMPORT
SIG · TRANSLATIONSTRING
T
translationstring
serializationpythonv1.4
Install
1.5s avg
Import
10ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
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.95 runs
installs and imports cleanly · install 0.0s · import 0.002s · 17.9MB
glibc
py 3.103.95 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.

TranslationString
from translationstring import TranslationString
TranslationStringFactory
from translationstring import TranslationStringFactory
Translator
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 issues
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.
fix
Upgrade to Python 3.4+ or ensure Python environment is compatible with `translationstring` 1.4 requirements.
affects: <=1.3
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.
fix
Ensure that if `default` has interpolation markers, `msgid` is a simple identifier (e.g., `TranslationString('add-number', default='Add ${number}', mapping={'number':1})`).
affects: All
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.
fix
Always pass `TranslationString` instances through a `Translator` callable or explicitly call their `ugettext` method to ensure they are translated and interpolated as intended.
affects: All
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.
fix
Be aware of the parameter precedence. If you want to force a specific domain or mapping for a translation, pass them directly to the `Translator` callable. If the `TranslationString` itself holds the correct context, ensure no conflicting arguments are passed to the `Translator`.
affects: All
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.
fix
Follow the convention of `_ = TranslationStringFactory('your_domain')` for consistency and tooling compatibility, but remember that `_` is just a variable name and can be overridden.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'translationstring'
The 'translationstring' package has not been installed in the current Python environment.
fix
pip install translationstring
ImportError: cannot import name '_' from 'translationstring'
The `_` (underscore) is a conventional variable name for an instantiated `TranslationStringFactory` instance, not a function directly exported by the `translationstring` module.
fix
from translationstring import TranslationStringFactory; _ = TranslationStringFactory('my_domain')
TypeError: TranslationString.__init__() missing 1 required positional argument: '_msgid'
The `TranslationString` class requires a message ID string (`_msgid`) as its first argument when instantiated directly.
fix
from translationstring import TranslationString; msg = TranslationString('My message')
ImportError: cannot import name 'ugettext' from 'translationstring'
The `ugettext` (or `gettext`, `ngettext`) method is available on instances of `TranslationStringFactory`, not directly exposed as a top-level function by the `translationstring` module.
fix
from translationstring import TranslationStringFactory; _ = TranslationStringFactory('my_domain'); translated_message = _.ugettext('My message')
Upgrade
Version history
1.4latest on PyPI · released Jul 9, 2020
Audit
Dependencies
BabeloptionalIts translation and pluralization services are meant to work best when provided with an instance of `babel.support.Translations` for advanced features beyond basic `gettext.NullTranslations`.
Agent activity
14 hits · last 30 days
node
10
OpenAI (training)
1
Resources
translationstring — pip install translationstring · libregistry