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

Cannot export to Ruby on Rails project as default placeholders are wrong #8

Closed
zachd opened this issue Aug 17, 2022 · 18 comments
Closed
Assignees
Labels
good first issue Good for newcomers question Further information is requested

Comments

@zachd
Copy link

zachd commented Aug 17, 2022

Describe the bug
Hello, the default format: :ruby_yaml and placeholder_format: :icu does not work with a Ruby on Rails project with the default i18n setup using the rails-i18n gem.

Ruby on Rails expects placeholders to be %{my_placeholder} but this gem exports as {my_placeholder} when using type :icu.

I cannot get any combination of format/placeholder_format for the file export to work with this gem and a Ruby on Rails project. Can you confirm how to use this gem?

To Reproduce

  1. Export a file from Lokalise with default parameters
  # Import options have the following defaults:
  # c.import_opts = {
  #   format: 'yaml',
  #   placeholder_format: :icu,
  #   yaml_include_root: true,
  #   original_filenames: true,
  #   directory_prefix: '',
  #   indentation: '2sp'
  # }
  1. Use this file in the default config/locales folder of a Ruby on Rails project
  2. You will see that placeholders show up as {my_placeholder} when translating a string, instead of using the replaced variable. This is due to Rails expecting %{my_placeholder}

Expected behavior
We expect that this gem should export as %{my_placeholder}, the only format supported by Ruby on Rails rails-i18n gem.

Your environment:

  • Ruby version '2.7.5'
  • Rails version '6.0.5'

Additional context
See how placeholders work in i18n interpolation documentation
https://guides.rubyonrails.org/i18n.html#passing-variables-to-translations

Checked with your support team to log a ticket, they suggested to go ahead. Thanks!

@zachd zachd added the bug Something isn't working label Aug 17, 2022
@bodrovis
Copy link
Owner

Thank you for your feedback! I wanted to double-check, how your translations look on Lokalise? Specifically, how placeholders are represented there? Do you use universal Lokalise placeholders?

@bodrovis
Copy link
Owner

As a quick fix, you could try using Custom placeholders https://docs.lokalise.com/en/articles/5781985-custom-placeholders and Universal placeholders https://docs.lokalise.com/en/articles/1400511-lokalise-placeholders. For example, if your translation on Lokalise has the following contents:

Welcome, [%s:name]

Then you can enable Custom placeholders and provide the following data:

  • Starts with: %{
  • Ends with: }

Then use Ruby on Rails YAML format and Raw placeholder format when exporting your data.

@zachd
Copy link
Author

zachd commented Aug 17, 2022

Thanks @bodrovis ! We uploaded the YAML files from Ruby, and converted them to Universal Placeholders.

For example [%1$s:current_weather] currently. It’s [%1$s:current_temp].
image

@zachd
Copy link
Author

zachd commented Aug 17, 2022

We got access to the Custom Placeholders app, but it doesn't seem to format the data correctly when exported.

Set up with these settings:
image

And exported as Raw in YAML format, we still get Universal Placeholders in the export, like greeting: 'Hello [%1$s:recipient],'

It might be that the Custom Placeholders app needs to be enabled before we import the translations to Lokalise the first time? Sadly this is not possible for us as the keys are already uploaded and translated.

@zachd
Copy link
Author

zachd commented Aug 17, 2022

How does this gem usually work with a Rails project and rails-i18n, maybe you have an example project to show me?

@bodrovis
Copy link
Owner

Unfortunately I don't have any samples at hand, but let me try to double-check everything. It's strange because everything worked for me fine

@bodrovis
Copy link
Owner

Yeah you know it's not working with the placeholder you are currently using. However this approach is working fine

[%s:current_weather] currently. It’s [%s:current_temp]

@zachd
Copy link
Author

zachd commented Aug 17, 2022

Hi @bodrovis, are you suggesting that placeholder works in an en.yml file on Rails?

Doesn't seem to work for me on rails console:

I18n.backend.store_translations :en, { test_key: 'Example %{placeholder} here' }
I18n.translate('.test_key', placeholder: 'text')
=> "Example text here"

I18n.backend.store_translations :en, { test_key: 'Example [%s:placeholder] here' }
I18n.translate('.test_key', placeholder: 'text')
=> "Example [%s:placeholder] here"

@bodrovis
Copy link
Owner

No-no, here's an example.

Translation:

2022-08-17 19_56_54-Angular _ Lokalise — Mozilla Firefox

Configuration:
2022-08-17 19_57_15-Angular _ Lokalise — Mozilla Firefox

Download config:

2022-08-17 19_57_34-Angular _ Lokalise — Mozilla Firefox

Preview:

2022-08-17 19_57_52-Download Preview — Mozilla Firefox

@bodrovis
Copy link
Owner

So, effectively my universal placeholders are presented as ordinary RoR YAML placeholders when using "raw" format

@zachd
Copy link
Author

zachd commented Aug 17, 2022

Ah okay! There must be something wrong with how we uploaded our YAML files before, I assumed [%1$s:current_weather] was a universal placeholder processed by Lokalise.

Would you have a way to get this Rails gem working out-of-the-box with Lokalise? And without using the Custom Placeholders App so we don't need to fix all our translated strings (4k+) 😄

@bodrovis
Copy link
Owner

bodrovis commented Aug 17, 2022

Actually [%1$s:current_weather] should work as a universal placeholder - I'm not quite sure what's the problem, to be honest (I've asked other team members to look into it).

Also, the gem should be working out of the box with translations similar to this one:

2022-08-17 20_09_35-Angular _ Lokalise — Mozilla Firefox

What was the original translation you tried to export?..

@bodrovis
Copy link
Owner

I've actually added another test case bodrovis/lokalise_manager@45ea245#diff-ffcc6c7c9aa007b1bd12983b91222c59ddcfe6917e2fd1cf6cef519f1edc8ee6R124

So, if I have the following translation Welcome to the app, %{username} on Lokalise, it gets exported as-is

@bodrovis bodrovis added good first issue Good for newcomers question Further information is requested and removed bug Something isn't working labels Aug 17, 2022
@zachd
Copy link
Author

zachd commented Aug 18, 2022

Also, the gem should be working out of the box with translations similar to this one:

Could you explain how to get translations into Lokalise with those placeholders?

If I upload like this
image

with Convert to universal placeholders unchecked
image

I still get this result in the editor
image

@zachd
Copy link
Author

zachd commented Aug 18, 2022

I've actually added another test case bodrovis/lokalise_manager@45ea245#diff-ffcc6c7c9aa007b1bd12983b91222c59ddcfe6917e2fd1cf6cef519f1edc8ee6R124

And related to this, how did you get the placeholder %{username} to output in your new test on this line?

I see the request parameters in the VCR cassette have placeholder_format: icu defined, so I would expect {username} as that's what I was receiving.

@bodrovis
Copy link
Owner

I believe this is happening because you've enabled Custom placeholders. If you turn it off, you should see a plain old placeholder %{user_first_name}. Also, with the ICU format you'll get your %{} placeholders exported as-is because Lokalise doesn't detect those as placeholders at all - that's how it works in my test case

@zachd
Copy link
Author

zachd commented Aug 18, 2022

Okay I see, it's a bit of a hack then 😉 If you changed the default of this gem to placeholder_format: raw it would make more sense.

Let me know if you get an answer from the team working on Custom Placeholders to support all types of Universal Placeholders and it should be good. A few lines in the documentation to explain that Universal Placeholders are only compatible with Rails if you use a Lokalise App would be great too!

@bodrovis
Copy link
Owner

Unfortunately I don't think there's any easy solution with Custom placeholders, universal ones and Rails. If you need this feature, I'd suggest reporting it to the customer support team and they can forward it to product guys (it would be more appropriate as I'm not a member of CS). Also, I'll update the docs for Rails.

In any case, thank you for reporting this and I'm sorry it tooks us a while to sort things out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants