add pgettext to InternationalizationExtension#1126
add pgettext to InternationalizationExtension#1126ThiefMaster merged 1 commit intopallets:masterfrom imomaliev:pgettext-support
Conversation
|
I don't think this is useful without adding |
Yes also |
|
Agreed! Good luck finding a decent syntax for this :) |
Thanks)) |
|
@ThiefMaster hi! Finally found time to finish this. This already works. TODO:
Current syntax for trans tag |
wo0dyn
left a comment
There was a problem hiding this comment.
Thanks @imomaliev. 👍 I'm looking forward to this.
I think having the context statement and the corresponding context string as two seperate strings seems problematic. It feels a lot more intuitive to have it be an assignment like |
Yes it is allowed Lines 378 to 384 in d754c88 But I don't think it is same for |
|
Since blocks are used for both comments ( {# Some button #}
{% trans count=n %}
{{ count }} thing
{% pluralize %}
{{ count }} things
{% endtrans %}{% trans %}
Click me
{% context %}
In annoying popup
{% endtrans %} |
|
@remram44 Context usually is one word, I feel using blocks for this case would be too verbose. |
|
Possibly. Just offering an alternative, since adding a third different syntax in the tag (variables |
|
@remram44 This could break some staff, but maybe it will be better to deprecate |
|
I agree in that blocks feel very verbose and if the whole section needs to be wrapped inside a context it would just boil down to {% trans %}{% context "greeting" %}Hello World{% endtrans %}Adding a third syntax into the mix also feels unnecessary though I think it may be the best we can do. Flags (like IMO we should either just pass a context like any other variable using |
|
What about an option that doesn't use the keyword
Apologies for bike-shedding, anything works for me really (and I'm excited to get context support), I just want to make sure we don't get stuck on a non-optimal syntax. The problem I see with the |
|
@remram44 I really like the |
|
even though |
|
hm, |
|
"Inside" the pluralize part? That part doesn't have an end tag, it just ends at {# Comment here #}
{% trans count=n %}
{{ count }} message
{% pluralize %}
{{ count }} messages
{% context %}
Context information
{% endtrans %}Whether we want to allow both orders is up to us (I think it's really weird to have context in between though): {# Comment here #}
{% trans count=n %}
{{ count }} message
{% context %}
Context information
{% pluralize %}
{{ count }} messages
{% endtrans %} |
|
Looking at it again (especially after adding indentation and highlighting to your comment) I think it makes perfect sense to allow both orders. The first one with the context a the very end looks much better though. FWIW, I was initially thinking about something like this: {% trans count=n %}
{% context "context information" %}
{{ count }} message
{% pluralize %}
{{ count }} messages
{% endtrans %}which looks very weird now that i see it. BTW I think when we use |
|
So I guess these two options would be the default (that would also be documented): {% trans name='bar' %}
Hello, {{ name }}!
{% context %}
Context information
{% endtrans %}{% trans count=n %}
{{ count }} message
{% pluralize %}
{{ count }} messages
{% context %}
Context information
{% endtrans %}I'd say for context before pluralize we can silently allow it if it just works, and in case supporting it makes the code more complex we explicitly disallow it. |
This should be consistent with similar tags like
I think having the context in between should probably be discouraged anyways as otherwise people may think it only applies to everything before. Also I think passing the context information as the content of the block after a |
|
I like |
|
I do not think deprecating any other current syntax is currently planned. However I think the syntax with |
|
Most often the context will only be a single word. It is mostly used for single word translation which have similar meanings and you need to specify which one you want. Sentences as the string to be translated are self describing and sentences as a context are never a good idea nor necessary in my experience. Here is a command which will search your local message catalogues for messages with a context specifier and the results on my pc: $ locate "*.po" | xargs grep "msgctxt"
/usr/lib/python3/dist-packages/tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.po:msgctxt "law"
/usr/lib/python3/dist-packages/tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.po:msgctxt "good"
/usr/lib/python3/dist-packages/tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.po:msgctxt "organization"
/usr/lib/python3/dist-packages/tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.po:msgctxt "stick"
$ locate "*.po" | xargs grep -A 2 "msgctxt"
/usr/lib/python3/dist-packages/tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.po:msgctxt "law"
/usr/lib/python3/dist-packages/tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.po-msgid "right"
/usr/lib/python3/dist-packages/tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.po-msgstr "le droit"
--
/usr/lib/python3/dist-packages/tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.po:msgctxt "good"
/usr/lib/python3/dist-packages/tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.po-msgid "right"
/usr/lib/python3/dist-packages/tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.po-msgstr "le bien"
--
/usr/lib/python3/dist-packages/tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.po:msgctxt "organization"
/usr/lib/python3/dist-packages/tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.po-msgid "club"
/usr/lib/python3/dist-packages/tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.po-msgid_plural "clubs"
--
/usr/lib/python3/dist-packages/tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.po:msgctxt "stick"
/usr/lib/python3/dist-packages/tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.po-msgid "club"
/usr/lib/python3/dist-packages/tornado/test/gettext_translations/fr_FR/LC_MESSAGES/tornado_test.po-msgid_plural "clubs" |
|
I don't think a library/framework is a good example - I'd be more interested in full-scale web applications. Does transifex have a good way to quickly scrape many translations of open source projects from their site? |
|
That was from 1563 catalogues however most of them were empty. I just got a command which unpacks the find /usr/share/locale/de/LC_MESSAGES -name "*.mo" -exec msgunfmt "{}" \; | grep msgctxt
# for less noise:
find /usr/share/locale/de/LC_MESSAGES -name "*.mo" -exec msgunfmt "{}" \; | grep msgctxt | cut -d " " -f 2- | uniq |
|
Another interesting data point: Django uses a context keyword with no equal sign:
|
|
It seems that the patch to add Perhaps users would benefit from splitting these two into separate issues and resolving at least the first one? |
|
@mcejp I think that would be the best solution so far. The inability to use context is one of the biggest problems of jinja2. |
|
@ThiefMaster Hi, should we separate this PR as @mcejp suggested? If this is ok I could cleanup this PR or open new one with just |
|
I think that would be a good idea. Usually it's the short strings (likely without any html etc in them) that need contexts the most, so having them available in general would already help a lot there. |
|
Updated. This PR now contains only short syntax. |
|
cc @ThiefMaster |
|
@ThiefMaster Also added docs |
|
@ThiefMaster updated |
ThiefMaster
left a comment
There was a problem hiding this comment.
Besides some minor documentation changes I think this PR is ready to be merged.
Code looks fine and I tested usage + extraction in my application without any problems.
|
Oh, one last thing I forgot: Could you please add a changelog entry to CHANGES.rst? |
|
@ThiefMaster Added. I am not sure about wording. Let me know if there is a better way describe this. |
|
I'm doing a pass to squash commits and check docs/formatting, don't push or merge yet. |
|
@davidism Note that I updated last commit |
|
Squashed and force pushed. If you need to make further changes: |
Fixes #441