Skip to content

Commit

Permalink
Fix frontend to include languages that do not have iso-639-1 codes (#…
Browse files Browse the repository at this point in the history
…4363)

* Fix iso 639 language code fallbacks to include languages only in later, 3-character specifications

* Update frontend/src/locales/scripts/get-validated-locales.js
  • Loading branch information
zackkrida committed May 21, 2024
1 parent 8a12eb6 commit 48e300d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 5 additions & 1 deletion frontend/src/locales/scripts/create-wp-locale-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ This script extracts data for locales available in GlotPress and translate.wp.or
const fs = require("fs")

const axios = require("./axios")

const { addFetchedTranslationStatus } = require("./get-translations-status")

const base_url =
Expand All @@ -34,6 +33,8 @@ const createPropertyRePatterns = ({
"english_name",
"native_name",
"lang_code_iso_639_1", // used for HTML lang attribute
"lang_code_iso_639_2", // used for HTML lang attribute, fallback from previous
"lang_code_iso_639_3", // used for HTML lang attribute, fallback from previous
"slug", // unique identifier used by Nuxt i18n
"text_direction",
],
Expand All @@ -49,12 +50,14 @@ function parseLocaleData(rawData) {
const wpLocalePattern = /wp_locale *= *'(.*)';/
const propertyRePatterns = createPropertyRePatterns()
const wpLocaleMatch = rawData.match(wpLocalePattern)

// ugly check to exclude English from the locales list,
// so we don't overwrite `en.json` later. See `get-translations.js`
// to check how `en.json` file is created.
if (wpLocaleMatch && wpLocaleMatch[1] !== "en_US") {
const wpLocale = wpLocaleMatch[1]
const data = {}

Object.keys(propertyRePatterns).forEach((key) => {
const pattern = propertyRePatterns[key]
const value = rawData.match(pattern)
Expand All @@ -66,6 +69,7 @@ function parseLocaleData(rawData) {
data[camelCasedPropName] = value[1]
}
})

return [wpLocale, data]
}
}
Expand Down
9 changes: 8 additions & 1 deletion frontend/src/locales/scripts/get-validated-locales.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ const getValidatedLocales = async () => {
code: locale.slug,
dir: locale.textDirection || "ltr",
file: `${locale.slug}.json`,
iso: locale.langCodeIso_639_1 ?? undefined,
// Check for a language in all three versions of the ISO 639 spec,
// defaulting to the v1 two-character codes before checking for the
// three-character codes in the v2 and v3 specs.
iso:
locale.langCodeIso_639_1 ??
locale.langCodeIso_639_2 ??
locale.langCodeIso_639_3 ??
undefined,

/* Custom fields */

Expand Down

0 comments on commit 48e300d

Please sign in to comment.