bodrovis / lokalise_rails

Ruby on Rails integration for Lokalise
MIT License
17 stars 3 forks source link

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

Closed zachd closed 2 years ago

zachd commented 2 years ago

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'
    # }
  2. Use this file in the default config/locales folder of a Ruby on Rails project
  3. 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:

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!

bodrovis commented 2 years ago

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 commented 2 years ago

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:

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

zachd commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

bodrovis commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

zachd commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

I've actually added another test case https://github.com/bodrovis/lokalise_manager/commit/45ea2452571306a6f67e293bad0dadec6dd13cce#diff-ffcc6c7c9aa007b1bd12983b91222c59ddcfe6917e2fd1cf6cef519f1edc8ee6R124

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

zachd commented 2 years ago

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 commented 2 years ago

I've actually added another test case https://github.com/bodrovis/lokalise_manager/commit/45ea2452571306a6f67e293bad0dadec6dd13cce#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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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!