From f8bb3f83f37b621a6ace26dbe39c78023facfbbb Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 24 Feb 2022 23:18:20 -0400 Subject: strings - same language code, different build dir --- src/misc-templates.js | 4 ++-- src/upd8.js | 4 ++-- src/util/strings.js | 30 +++++++++++++++++++++++++----- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/misc-templates.js b/src/misc-templates.js index 3ad1f23..f51d0e5 100644 --- a/src/misc-templates.js +++ b/src/misc-templates.js @@ -396,9 +396,9 @@ export function getFooterLocalizationLinks(pathname, { { json: { 'meta.languageName': b } } ) => a < b ? -1 : a > b ? 1 : 0) .map(strings => html.tag('span', html.tag('a', { - href: (strings.code === languages.default.code + href: (strings.baseDirectory === languages.default.baseDirectory ? to('localizedDefaultLanguage' + keySuffix, ...toArgs) - : to('localizedWithBaseDirectory' + keySuffix, strings.code, ...toArgs)) + : to('localizedWithBaseDirectory' + keySuffix, strings.baseDirectory, ...toArgs)) }, strings.json['meta.languageName']))); return html.tag('div', diff --git a/src/upd8.js b/src/upd8.js index 4a755ab..81275ad 100755 --- a/src/upd8.js +++ b/src/upd8.js @@ -2040,7 +2040,7 @@ async function wrapLanguages(fn, {writeOneLanguage = null}) { for (let i = 0; i < entries.length; i++) { const [ key, strings ] = entries[i]; - const baseDirectory = (strings === languages.default ? '' : strings.code); + const baseDirectory = (strings === languages.default ? '' : strings.baseDirectory); await fn({ baseDirectory, @@ -2233,7 +2233,7 @@ async function main() { } if (error) return; - languages = Object.fromEntries(results.map(strings => [strings.code, strings])); + languages = Object.fromEntries(results.map(strings => [strings.baseDirectory, strings])); } else { languages = {}; } diff --git a/src/util/strings.js b/src/util/strings.js index e749b94..88dd571 100644 --- a/src/util/strings.js +++ b/src/util/strings.js @@ -68,6 +68,23 @@ export function genStrings(stringsJSON, { return {error: `Expected language code to be a string.`}; } + // Lanuages can also provide a 8ase-directory code, which will otherwise + // default to the language code. This controls the name of the directory the + // localized HTML will 8e outputted to, which could 8e useful for, like, + // testing a 8uild of the site with one language JSON set somewhere besides + // the default directory. + // + // Note that internally, baseDirectory, not code, is the property used to + // identify a language - two language files may share a language code (like + // "en" or "es), but base directories should always be unique. + let baseDirectory = stringsJSON['meta.baseDirectory']; + if (!baseDirectory) { + baseDirectory = code; + } + if (typeof baseDirectory !== 'string') { + return {Error: `Expected base directory to be a string.`}; + } + // Every value on the provided o8ject should be a string. // (This is lazy, but we only 8other checking this on stringsJSON, on the // assumption that defaultJSON was passed through this function too, and so @@ -76,7 +93,7 @@ export function genStrings(stringsJSON, { let err = false; for (const [ key, value ] of Object.entries(stringsJSON)) { if (typeof value !== 'string') { - logError`(${code}) The value for ${key} should be a string.`; + logError`(${baseDirectory}) The value for ${key} should be a string.`; err = true; } } @@ -94,12 +111,12 @@ export function genStrings(stringsJSON, { const presentKeys = Object.keys(stringsJSON); for (const key of presentKeys) { if (!expectedKeys.includes(key)) { - logWarn`(${code}) Unexpected translation key: ${key} - this won't be used!`; + logWarn`(${baseDirectory}) Unexpected translation key: ${key} - this won't be used!`; } } for (const key of expectedKeys) { if (!presentKeys.includes(key)) { - logWarn`(${code}) Missing translation key: ${key} - this won't be localized!`; + logWarn`(${baseDirectory}) Missing translation key: ${key} - this won't be localized!`; } } } @@ -144,7 +161,7 @@ export function genStrings(stringsJSON, { // just redundant! if (!invalidKeysFound.includes(key)) { invalidKeysFound.push(key); - logError`(${code}) Accessing invalid key ${key}. Fix a typo or provide this in strings-default.json!`; + logError`(${baseDirectory}) Accessing invalid key ${key}. Fix a typo or provide this in strings-default.json!`; } return `MISSING: ${key}`; } @@ -166,7 +183,7 @@ export function genStrings(stringsJSON, { // Post-processing: if any expected arguments *weren't* replaced, that // is almost definitely an error. if (output.match(/\{[A-Z_]+\}/)) { - logError`(${code}) Args in ${key} were missing - output: ${output}`; + logError`(${baseDirectory}) Args in ${key} were missing - output: ${output}`; } return output; @@ -180,6 +197,9 @@ export function genStrings(stringsJSON, { // Store the strings dictionary itself, also for convenience. strings.json = stringsJSON; + // Same with the base directory. + strings.baseDirectory = baseDirectory; + // Store Intl o8jects that can 8e reused for value formatting. strings.intl = { date: new Intl.DateTimeFormat(code, {full: true}), -- cgit 1.3.0-6-gf8a5