-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request for Adding Support for Localized Key References #10
Comments
I have created a simple wrapper class to address this issue. import i18n
from string import Template
i18n.set('file_format', 'json')
i18n.set('filename_format', '{locale}.{format}')
i18n.load_path.append('./i18n/')
class KeyRefFormatter(Template):
delimiter = "$"
def __init__(self, template, **kwargs):
template = i18n.t(template, **kwargs)
super().__init__(template)
self.kwargs = kwargs
# From https://github.com/python/cpython/issues/90465
# For Python 3.11+, use library method string.Template.get_identifiers() instead
def get_identifiers(self):
return list(
set(
filter(
lambda v: v is not None,
(mo.group('named') or mo.group('braced')
for mo in self.pattern.finditer(self.template))
)
)
)
def format(self):
sub_dict = { key: i18n.t(key, **self.kwargs) for key in self.get_identifiers() }
return self.safe_substitute(sub_dict)
def _(key, **kwargs):
return KeyRefFormatter(key, **kwargs).format()
print(_('MENU_A_DUMMY', placeholder_1="def", placeholder_2="abc"))
# Output: dummy1 with def, and dummy2 with abc The corresponding en.json file is as follows: {
"en": {
"DUMMY_1": "dummy1 with %{placeholder_1}",
"DUMMY_2": "dummy2",
"MENU_A_DUMMY": "${DUMMY_1}, and ${DUMMY_2} with %{placeholder_2}",
"MENU_B_DUMMY": "${DUMMY_1}"
}
} This is a workaround and a temporary solution. Perhaps it can be further integrated into this project in the future? |
Thanks for opening the issue. I agree that this can be a useful feature. |
You can take a look at my pull request and share your thoughts. |
Looks good and thanks for your work! Let me confirm, so the static reference feature and placeholder feature share the delimiter Additionally, could you explain the behavior of the feature when the referenced value contains a placeholder? For instance, in my example above, |
It's already covered by tests and described in the readme. You probably got confused because the library uses placeholder delimiter (defaults to To create a static reference, you put it as key prefixed with namespace delimiter ( Let me know if you have any suggestions for description improvement, because I'm kind of bad when it comes to explaining things ;) |
Oh, I figure it out now. It would be great if everything works just as expected! 👏 |
I'll keep this open until the new release is published |
New version is released 🎉 |
Appreciation
First, I want to thank you first for taking on the maintenance of the original
python-i18n
package and add more functionality to it.This simple, lightweight, and user-friendly i18n tool saves me a lot of time. Salute.
Feature Request
Here I'd like to request a feature addition to enhance the package's capabilities. Specifically, the support for localized key references.
An example written in JSON is provided as follows:
And the corresponding expected result will be:
With a special syntax (
${}
here, could be replaced with any other delimiters), it would be possible to reference existing localized keys within other translations.The proposed approach offers two significant advantages: interface flexibility through referencing multiple localized keys within a single translation, allowing diverse interfaces that are easily modifiable in the future, and translation reusability, minimizing redundancy and improving maintenance efficiency.
I believe adding support for localized key references would improve the package's utility, especially for projects with complex translation requirements.
Thank you for considering this enhancement request.
The text was updated successfully, but these errors were encountered: