Skip to content
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

Can't retrieve translation #26

Closed
clmnt-sg opened this issue Jan 13, 2021 · 3 comments
Closed

Can't retrieve translation #26

clmnt-sg opened this issue Jan 13, 2021 · 3 comments

Comments

@clmnt-sg
Copy link

Hi!

I'm trying to implement i18n in one of my python script. I tried to figure out how this lib works but I'm facing an issue. I didn't find how to retrieve the desired value in my yml file:

python script:

    i18n.load_path.append('template')
    i18n.set('file_format', 'yaml')
    i18n.set('locale', 'en')
    print(i18n.t('template.subject'))

yaml file (template.en.yml):

en:
  subject: Test %{filename}

But my script only prints "template.subject" and not the string.

What am I missing here ?

Thanks!

@fabianjs
Copy link

fabianjs commented Jan 22, 2021

Hi,

Not the maintainer but was facing the same issue. Switching to json worked for me (see python-i18n/i18n/tests/resources/translations/en.json for an example).

You will need to set

i18n.set('file_format', 'json')
i18n.set('filename_format', '{locale}.{format}')

and finally of course
i18n.load_path.append('path/to/translations/folder')

for this to work.

I would however be really interested in the correct YAML solution which I couldn't get to work either.

@clmnt-sg
Copy link
Author

I actually achieved to make it work with YAML. By default, python-i18n uses YAML for external files. I just removed the i18n.set format thingy.

Also, I struggled a little bit with the path structure (and It's not that easy to find the issue because if the code does not find the right translation, It will print you the path that you're searching and not something like "Error translation not found" ^^)

Here is my code:

i18n.load_path.append('template')
i18n.set('locale', countrycode)
subject = i18n.t(f'{countrycode}.mail.subject', filename=filename)
body = i18n.t(f'{countrycode}.mail.body', filename=filename, error=i18n.t(f'{countrycode}.error.{errorcode}'))

And my paths structured like this:
template/en/mail.en.yml

@perrotuerto
Copy link

In my case was that I didn't install pyyaml. With it, works as @clmnt-sg said. Without it, works as @fabianjs suggested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants