Skip to content

Commit 43bed86

Browse files
committed
fix: set temporarily the I18n.locale when rendering a Maglev page
1 parent a63b9ae commit 43bed86

File tree

3 files changed

+6
-20
lines changed

3 files changed

+6
-20
lines changed

app/controllers/maglev/page_preview_controller.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class PagePreviewController < ApplicationController
77
include Maglev::ContentLocaleConcern
88

99
before_action :fetch_maglev_site
10-
before_action :extract_content_locale
10+
around_action :extract_content_locale
1111

1212
def index
1313
render_maglev_page
@@ -43,9 +43,9 @@ def use_engine_vite?
4343
false
4444
end
4545

46-
def extract_content_locale
46+
def extract_content_locale(&block)
4747
_, locale = maglev_services.extract_locale.call(params: params, locales: maglev_site.locale_prefixes)
48-
::I18n.locale = locale
48+
::I18n.with_locale(locale, &block)
4949
end
5050

5151
def fallback_to_default_locale

app/frontend/editor/plugins/i18n.js

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,9 @@ import messages from '@/locales'
44

55
Vue.use(VueI18n)
66

7-
const AVAILABLE_LOCALES = ['en', 'fr']
8-
var locale = 'en'
9-
10-
if (document.documentElement.lang) {
11-
// fetch the local from the HTML tag
12-
locale = document.documentElement.lang
13-
} else {
14-
// try to fetch the browser locale
15-
const language = navigator.languages[0]
16-
if (language) {
17-
locale = language.split('-')[0]
18-
if (AVAILABLE_LOCALES.indexOf(locale) === -1) locale = null
19-
}
20-
}
21-
227
const i18n = new VueI18n({
23-
locale,
24-
fallbackLocale: AVAILABLE_LOCALES[0],
8+
locale: window.uiLocale,
9+
fallbackLocale: 'en',
2510
messages,
2611
})
2712

app/views/maglev/editor/show.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
<%= javascript_tag nonce: true do %>
1010
window.locale = <%= h content_locale.to_json.html_safe %>;
11+
window.uiLocale = <%= h editor_ui_locale.to_json.html_safe %>;
1112
window.baseUrl = <%= h site_base_editor_path.to_json.html_safe %>;
1213
window.leaveUrl = <%= site_leave_editor_path.to_json.html_safe %>;
1314
window.apiBaseUrl = <%= h api_base_path.to_json.html_safe %>;

0 commit comments

Comments
 (0)