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

Interpolations? #162

Closed
glebm opened this issue Jan 30, 2018 · 11 comments
Closed

Interpolations? #162

glebm opened this issue Jan 30, 2018 · 11 comments
Labels

Comments

@glebm
Copy link

glebm commented Jan 30, 2018

For example, consider a model with a translated greeting attribute with value such as Hello, %{name}!.

with Globalize, the following works:

record.greeting(name: 'Chris')

with Mobility, interpolation is not performed.

I've tried migrating from globalize as per the wiki article.

@shioyama
Copy link
Owner

shioyama commented Jan 30, 2018

Oh nice, I didn't actually know that Globalize could do this. This would be easy to implement as a plugin, so you could enable it with interpolate: true.

One problem is that Mobility already accepts and uses a hash of options passed ot the translation getter, e.g. post.title(fallback: false), which disables fallbacks for a single read. So the interface would have to be something a little different. Any ideas? 😉

@shioyama
Copy link
Owner

Updated the wiki to mention this is not currently supported.

@shioyama
Copy link
Owner

I suppose something like:

record.greeting(interpolate: { name: 'Chris' })

would work.

@glebm
Copy link
Author

glebm commented Jan 30, 2018

This API makes sense. I would prefer interpolations as the key though.

@pwim
Copy link
Collaborator

pwim commented Jan 30, 2018

I don't see a need for this to be part of mobility, as String already supports interpolation via the % method:

"Hello, %{name}!" % { name: "Chris"} # => "Hello, Chris!"

So you should be able to use it to get the desired result:

record.greeting % {name: 'Chris'} # => "Hello, Chris!"

As an alternative, looking at globalize's implementation, it just seems to be doing some argument checking and then calling I18n.interpolate. So I think the following should be equivalent to globalize:

I18n.interpolate(record.greeting, name: 'Chris') # => "Hello, Chris!"

I didn't look into the details of what the difference between % and I18n.interpolate is under the hood.

@glebm
Copy link
Author

glebm commented Jan 30, 2018

@pwim I18n interpolation is not as naive as % and should use the I18n backend settings. For example, it supports strategies for resolving / reporting missing / extraneous interpolation values.

Globalize does call I18n.interpolate:

https://github.com/globalize/globalize/blob/38443bcd07da78b7b8a9433e4c6c32dd51f964a3/lib/globalize/interpolation.rb#L20

@shioyama
Copy link
Owner

Here is the implementation in I18n.

@glebm
Copy link
Author

glebm commented Jan 30, 2018

@shioyama That's not all of it, another important part is this optimization:

https://github.com/svenfuchs/i18n/blob/7c6ccf471ecf2c0a406d3a5317375f28d2da3613/lib/i18n/backend/interpolation_compiler.rb

Nevermind, that doesn't look useful for Mobility

@glebm
Copy link
Author

glebm commented Jan 30, 2018

Manually calling I18n.interpolate should work, even if a bit awkard.

@shioyama
Copy link
Owner

It's true that the difference between:

I18n.interpolate(record.greeting, name: 'Chris')

and

record.greeting(interpolate: { name: 'Chris' })

is pretty small, if that's all the plugin would do. It's also more obvious what is going on in the first case.

@shioyama
Copy link
Owner

I'm going to close this, it doesn't seem like a killer feature. If anybody things otherwise, please feel free to comment.

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

No branches or pull requests

3 participants