Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Set default English locale correctly (#461)

Olga Bulat3c4ced5718cc
diff --git a/nuxt.config.js b/nuxt.config.js
index 9c7293e96..c6b25edee 100644
--- a/nuxt.config.js
+++ b/nuxt.config.js
@@ -173,9 +173,13 @@ export default {
i18n: {
locales: [
{
+ // unique identifier for the locale in Vue i18n
code: 'en',
name: 'English',
+ // ISO code used for SEO purposes (html lang attribute)
iso: 'en',
+ // wp_locale as found in GlotPress
+ wpLocale: 'en_US',
file: 'en.json',
},
...(locales ?? []),
diff --git a/src/mixins/i18n-sync.js b/src/mixins/i18n-sync.js
index a2e5d0a35..443cd97e4 100644
--- a/src/mixins/i18n-sync.js
+++ b/src/mixins/i18n-sync.js
@@ -5,20 +5,28 @@ export default {
methods: {
/**
* Handles messages of type `localeSet` received by the `iframe`. Any
- * other message types will by discarded.
+ * other message types will be discarded.
*
- * @param {string} type - the nature of the message received
- * @param {{ locale: string, lang: string }} value - the data sent with the message
+ * @param {string} type - the nature of the message received.
+ * @param {Object} value - the data sent with the message.
+ * @param {string} value.locale - wpLocale code of the locale, e.g. 'en_US'.
+ * @param {string} value.lang - the iso code for language
+ * (and sometimes country), e.g. 'en-US'.
+ * Default lang value on wp.org is 'en', and on wp.org/openverse - 'en-US'.
+ * @param {'rtl'|'ltr'} [value.dir] - the locale text direction.
*/
- localeMsgHandler({ data: { type, value } }) {
+ async localeMsgHandler({ data: { type, value } }) {
if (type !== 'localeSet') return
-
+ // If the locale set by wp.org is 'en_US', not 'en', this is not necessary.
+ const wpLocaleValue = value.locale === 'en' ? 'en_US' : value.locale
const locale = this.$i18n.locales.find(
- (item) => item.wpLocale === value.locale
+ (item) => item.wpLocale === wpLocaleValue
)
if (locale) {
- this.$i18n.setLocale(locale.code)
- document.documentElement.lang = value.lang
+ await this.$i18n.setLocale(locale.code)
+ document.documentElement.lang = locale.wpLocale.replace('_', '-')
+ // Always set `dir` property, default to 'ltr'
+ document.documentElement.dir = locale.dir === 'rtl' ? 'rtl' : 'ltr'
}
},
},