-
Notifications
You must be signed in to change notification settings - Fork 9
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
Allow to set different globals per locale #5
Conversation
Sure, it's a fair addition which I haven't considered when I wrote the gem. My use case was to interpolate stuff like company and customer names into translated messages, which remained the same in every supported locale. I'm not completely sure about the hash structure though. I might restructure it so that the default variables go to the main I18n.config.globals = {
salutation: 'Bye bye'
hello: 'Hello',
de: {
salutation: 'Tschüss'
},
fr: {
salutation: 'Salut'
}
} I think this would be a bit simpler and cleaner, just amending your set of variables on a case-by-case basis when needed for different locales. Also, it'd prevent existing users from having to put all of their already used variables into a What do you think about this? By the way, regarding your last point, using dynamic global variables was specifically the use case I made the gem for. Basically, our translators wanted to have a set of basic info - user name, company, package prices, etc. - being available for them on any piece of text on the site, so they could make changes to their heart's content without worrying about whether a specific variable is available for a piece of translation or not. Hence, I just threw them into this globals hash. Hope this explains what I was getting at in the Readme. |
OK, I understand your use case. I made some changes so that there is no need for that So I added a class EmployeesController < ApplicationController
before_action :set_i18n_globals
# ...
private
def set_i18n_globals
I18n.config.cear_globals_cache
I18n.config.globals[:company] = Company.current.name
end
end This restricts the merging and rejecting to once per request at least. Not sure if this is elegant though, and maybe you have a better idea. (If Nevertheless I don't think that moving everything into a |
57858bb
to
3d0608f
Compare
Couple of things:
Thanks for your continued work on the project by the way! |
OK, then this can be removed.
It's simply there to cache the result, so it does not have to search the
See my last commit, this works by subclassing Edit: 👍 for adding tests, I was just about to write some, too ;-) |
2293dbc
to
50e350d
Compare
Nice, this looks very clean now! And much shorter than I thought it would be. Yes, I've just included a test suite and set up a Travis build. Beforehand it was just too simple for me to justify writing tests for a few lines of code but now it has some more features so it's nice to have them. Can you add some tests for this new behaviour? After that, I'm happy to merge the changes and release a new version. |
I added some tests and fixed some cache invalidation misses ... it got a little bit more complicated unfortunately, but is still graspable IMO. Anyway, if you are OK with this, I will rebase this into one commit before merging to keep the history clean. |
e755bbe
to
d703ccd
Compare
Alright, I don't see a clearer way to do this either. If you could rebase this, that would be perfect. Thanks! |
This changes the globals that when it has have a `default` key it can have locale aware globals. For example: ``` I18n.config.globals = { salutation: 'Bye bye', hello: 'Hello', de: { salutation: 'Tschüss' }, fr: { salutation: 'Salut' } } ``` Depending on the locale, the global variable `salutation` will be different. The default value is used as a fallback, so that `hello` will be available in each language, too.
710efc5
to
29ef126
Compare
👍 rebased it, something new in README is missing, though ... |
Great, I'll merge it now. Thanks for all the help! |
This changes the globals that when it has have a
default
key it canhave locale aware globals. For example:
Depending on the locale, the global variable
salutation
will bedifferent. A default value is used as a fallback, so that
hello
willbe available in each language, too.
When there is no
default
key it works as before (locale independent).What this breaks - but only if you have a
default
key in your globals - is setting globals in an before action like stated in the readme:It breaks because in the locale aware case the globals are cached on first call (https://github.com/attilahorvath/i18n-globals/pull/5/files#diff-fb7fb950c7f99492dbbb178ff8bd8710R23)
But I am not sure if that is not bad practice anyway - because if you want to have something in your translation which is dependent of the state of the application, you should always pass this directly on your
I18n.t
call (e.g.I18n.t('welcome', company: Company.current.name)
) IMO.Nevertheless I'm open to what you say to this @attilahorvath.