i18nice icon indicating copy to clipboard operation
i18nice copied to clipboard

dashes not allowed in static references

Open belamu opened this issue 8 months ago • 4 comments

Minimal non-working example:

pip install i18nice[YAML]

translation/a.en.yml:

en:
  a-key: aValue

translation/b.en.yml:

en:
  a-key: "%{a.a-key}"

static-ref.py:

import i18n
i18n.load_path.append("./translation")
print("b.a-key=", i18n.t("b.a-key"))

Produces

Traceback (most recent call last):
  File "static_ref.py", line 8, in <module>
    print("b.akey=", i18n.t("b.akey"))
.......
  File "/i18n/formatters.py", line 57, in convert
    self._invalid(mo)
  File "/python3.12/string.py", line 101, in _invalid
    raise ValueError('Invalid placeholder in string: line %d, col %d' %
ValueError: Invalid placeholder in string: line 1, col 1

It confused me for quite a while because I assumed it wouldn't find the needed key a.a-key.

This is most likely because this string formatting does not allow dashes.

I have not checked other symbols like underscore, whitespace of any kind, weird unicode, comma, semicolon, ...

Proposition: add a warning in the documentation which symbols are OK for the keys. Note that this is (only) important if you use static references.

belamu avatar Apr 17 '25 19:04 belamu

unfortunately the official documentation is not that helpful: key Optional. Mapping key, consisting of a parenthesised sequence of characters (for example, (somename)).

from https://python-reference.readthedocs.io/en/latest/docs/str/formatting.html

belamu avatar Apr 17 '25 19:04 belamu

It's because of regular expressions formatters use. It's possible to add dash support.

solaluset avatar Apr 18 '25 05:04 solaluset

That would of course also be nice (if it doesn't create other problems). I didn't notice this regular expression. Maybe add it to the docs so that users exactly know what's allowed?

belamu avatar Apr 18 '25 06:04 belamu

Hello, sorry, I forgot about this issue 💀 Will fix tomorrow.

solaluset avatar May 28 '25 20:05 solaluset