Comments (8)
Thanks for opening the issue. I agree that this can be a useful feature.
from i18nice.
It's already covered by tests and described in the readme. You probably got confused because the library uses placeholder delimiter (defaults to %
) for placeholders (and now static refs) and namespace delimiter (defaults to .
) for keys.
To create a static reference, you put it as key prefixed with namespace delimiter ("%{.DUMMY_1}, and %{.DUMMY_2} with %{placeholder_2}"
in your case)
Let me know if you have any suggestions for description improvement, because I'm kind of bad when it comes to explaining things ;)
from i18nice.
New version is released 🎉
Thanks for your input. In case if something doesn't work as intended, please open a new issue
from i18nice.
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?
from i18nice.
You can take a look at my pull request and share your thoughts.
from i18nice.
Looks good and thanks for your work!
Let me confirm, so the static reference feature and placeholder feature share the delimiter %
, right?
Additionally, could you explain the behavior of the feature when the referenced value contains a placeholder?
For instance, in my example above, MENU_A_DUMMY
includes the placeholder %{placeholder_2}
and also references DUMMY_1
, which itself contains the placeholder %{placeholder_1}
. In this case, would i18n.t('MENU_A_DUMMY', placeholder_1="def", placeholder_2="abc")
work as expected? Perhaps this could be added as a test case.
from i18nice.
Oh, I figure it out now. It would be great if everything works just as expected! 👏
The description is just fine and thank you for the update. Looking forward to the new version being released on PyPI!
from i18nice.
I'll keep this open until the new release is published
from i18nice.
Related Issues (10)
- PyPI support HOT 11
- Add support for yes/no variables HOT 2
- Feedback about python-i18n v0.6.0 HOT 1
- Support None as fallback locale HOT 4
- Add `py.typed` marker to work with PEP 561 HOT 2
- regression: ValueError exception when %% is used in string HOT 1
- cool
- Question HOT 8
- conda recipe for i18nice HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from i18nice.