Skip to content

Commit

Permalink
Pass the current locale from Rails
Browse files Browse the repository at this point in the history
To ensure that we have consistent locales being applied in both Rails
and Vue, we pass the current locale from Rails via the `window.ukhpi`
state object. This is more reliable than checking URL arguments, since
we also detect if the user is selecting a language using the HTTP
`Accept-Language` header.
  • Loading branch information
ijdickinson committed Aug 6, 2020
1 parent a9741af commit b05f6b9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/javascript/lang/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ Vue.use(VueI18n)
const localeEn = require('../../../config/locales/en.yml')
const localeCy = require('../../../config/locales/cy.yml')

// The locale is passed via the window.ukhpi structure
// directly from Rails
const currentLocale = window.ukhpi.locale || 'en'

export default new VueI18n({
locale: 'en',
locale: currentLocale,
messages: { ...localeEn, ...localeCy }
})
1 change: 1 addition & 0 deletions app/views/layouts/webpack_application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
window.ukhpi = window.ukhpi || {};
window.ukhpi.environment = '#{ENV["DEPLOYMENT_ENVIRONMENT"] || Rails.env}';
window.ukhpi.version = '#{Version::VERSION}';
window.ukhpi.locale = '#{I18n.locale}';

= stylesheet_link_tag 'application', media: 'all'
= csrf_meta_tags
Expand Down
6 changes: 6 additions & 0 deletions config/locales/cy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ cy:
js:
action:
download: "# Download this data"
data_graph: "# See data graph"
data_table: "# See data table"
compare: "# Compare with location ..."

graph:
no_data: "# Sorry, there is no %{label} data available for %{location}. Please select a different location."

# old-------------

Expand Down

0 comments on commit b05f6b9

Please sign in to comment.