What is the suggested way to implement locale switching? #318
-
The docs mention setting a display option so that a user can select a language like English or French. config.lookbook.preview_display_options = {
bg_color: '#fff', # default bg_color value
theme: ["light", "dark"], # dynamic 'theme' display option
lang: [
["English", "en"],
["French", "fr"]
]
} This will add the option to select the language in the UI. Selecting the language does nothing. The locale switching logic needs to be (as far as I can tell) set up manually. After some poking around, I found that we can override the controller for previews and decided to add the locale switching logic to that controller. config.view_component.preview_controller = "PreviewController"
...
class PreviewController < ViewComponentsController
before_action :set_locale
def set_locale
I18n.locale = params[:lookbook][:display][:lang] || I18n.default_locale
end
end This kind of worked but only because I had, at some point, viewed the UI and selected a language which selected a locale and stored it in a cookie. So for me calling I shipped this to my colleague who immediately sent me a message saying he was receiving an error. After some troubleshooting the value of It took some time to discover the cookie thing and I started to dig in as to how to handle this when the cookie is not yet set. In troubleshooting this I noticed that We have since had to implement the following logic to avoid these pitfalls and it just seems like either maybe I'm swimming upstream or nobody else has tried to do language switching in lookbook yet. class PreviewController < ViewComponentsController
before_action :set_locale
def set_locale
I18n.locale = selected_language
end
def selected_language
lang = params[:lookbook][:display][:lang]
if lang.is_a?(Array)
lang.second
elsif lang.is_a?(String)
begin
JSON.parse(lang).second
rescue JSON::ParserError
lang
end
else
I18n.default_locale
end
end
end Is there a better or recommended approach to this? I'm happy to help contribute to the project if this needs better docs or maybe a PR to change how language switching works but I thought I'd start with a conversation here first and see how other are solving this problem. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 4 replies
-
Hey @rmontgomery429 - sorry for the slow reply, I missed this! You are correct that there is no in-built locale switching, that is up to the app to implement. However, the inconsistent behaviour of the I'll create an issue from this discussion and try to get that fixed up as soon as I can. Whilst you'll still need to implement the actual locale switching yourself that should make things a bit easier! FWIW, I suspect (just guessing here though) that the issue may be worked around by defining your languages like this: config.lookbook.preview_display_options = {
lang: ["en", "fr"]
} You will obviously not get the human-readable text displayed in the select but it may make the param value behave more consistently. That's just a potential workaround however - this is definitely a bug. I'll get a new release out with a fix as soon as it is sorted. Thanks for bringing it to my attention 👍 |
Beta Was this translation helpful? Give feedback.
-
Just a quick tip: you'd want to use |
Beta Was this translation helpful? Give feedback.
-
@rmontgomery429 I've just released v1.5.3 that should fix the inconsitency issue you are seeing here - You may have to clear the |
Beta Was this translation helpful? Give feedback.
-
I updated to 1.5.3 and have adjusted the PreviewController as follows and everything is working great! Thanks so much! 👏 class PreviewController < ViewComponentsController
around_action :switch_locale
def switch_locale(&action)
locale = params[:lookbook][:display][:lang] || I18n.default_locale
I18n.with_locale(locale, &action)
end
end |
Beta Was this translation helpful? Give feedback.
-
@rmontgomery429 fantastic! Glad it works for you now and sorry for the frustration. When I have a chance I'll try to add an example to the docs using your code above as I'm sure others would find it useful :-) |
Beta Was this translation helpful? Give feedback.
I updated to 1.5.3 and have adjusted the PreviewController as follows and everything is working great! Thanks so much! 👏