Iteration J4: Adding a Locale Switcher

We’ve completed the task, but we need to advertise its availability more. We spy some unused area in the top-right side of the layout, so we add a form immediately before the image_tag:

  <header class=​"bg-green-700"​>
» <aside data-controller=​"locale"​>
»<%=​ form_tag store_index_path, ​class: ​​'locale'​ ​do​ ​%>
»<%=​ select_tag ​'set_locale'​,
» options_for_select(LANGUAGES, I18n.​locale​.​to_s​),
»onchange: ​​'this.form.submit()'​ ​%>
»<%=​ submit_tag ​'submit'​, ​data: ​{​'locale-target'​ => ​'submit'​} ​%>
»<%​ ​end​ ​%>
» </aside>
 <%=​ image_tag ​'logo.svg'​, ​alt: ​​'The Pragmatic Bookshelf'​ ​%>
  <h1>​<%=​ @page_title ​%>​</h1> ...

Get Agile Web Development with Rails 7 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.