From 59e43809064e11e8e7d73750a7fa4ee4128c22f7 Mon Sep 17 00:00:00 2001 From: "Jocelyn Badgley (Twipped)" Date: Wed, 11 Aug 2021 11:23:59 -0700 Subject: [PATCH] Adding more internationalization support --- build/engines.js | 12 +++++++++- build/page-writer.js | 12 ++++++---- lang/en.js | 11 +++++++++ lang/es.js | 8 +++++++ lang/index.js | 12 ++++++++++ package.json | 2 +- public/_gdb-en-menu.hbs | 53 +++++++++++++++++++++-------------------- public/_gdb-es-menu.hbs | 53 +++++++++++++++++++++-------------------- public/_gdb-menu.hbs | 7 +++--- templates/layout.hbs | 23 ++++++------------ 10 files changed, 115 insertions(+), 78 deletions(-) create mode 100644 lang/en.js create mode 100644 lang/es.js create mode 100644 lang/index.js diff --git a/build/engines.js b/build/engines.js index 2b81c77..9268480 100644 --- a/build/engines.js +++ b/build/engines.js @@ -12,7 +12,7 @@ const slugify = require('./lib/slugify'); const { stripHtml } = require('string-strip-html'); const markdownIt = require('markdown-it'); - +const i18n = require('../lang'); const markdownEngines = { @@ -144,6 +144,7 @@ class Injectables { this.prod = prod; this.revManifest = revManifest; this.injections = {}; + this.languages = {}; } _parsePath (tpath, local, type) { @@ -182,6 +183,7 @@ class Injectables { icon: this.icon(), prod: this.production(), rev: this.rev(), + lang: this.lang(), }; } @@ -268,4 +270,12 @@ class Injectables { }; } + lang () { + return function (key, ...args) { + const { resolve: rval } = args.pop(); + const lang = rval('@root.this.page.lang').split('-')[0]; + return i18n(lang, key, ...args); + }; + } + } diff --git a/build/page-writer.js b/build/page-writer.js index 665bcc2..3f2a4bc 100644 --- a/build/page-writer.js +++ b/build/page-writer.js @@ -5,6 +5,7 @@ const { map, uniq } = require('lodash'); const { resolve, ROOT, TYPE } = require('./resolve'); const { siteInfo } = require(resolve('package.json')); const { minify } = require('html-minifier-terser'); +const i18n = require('../lang'); const MINIFY_CONFIG = { conservativeCollapse: true, @@ -73,17 +74,18 @@ function siblings (posts) { } function pageState (page, posts) { + const lang = page.meta.lang || siteInfo.lang || 'en'; return { ...page, meta: { ...page.meta, ...page }, page: { domain: siteInfo.domain, - lang: page.meta.lang || siteInfo.lang || "", - date: page.meta.date || "", + lang, + date: page.meta.date || '', title: page.meta.title - ? (page.meta.title + (page.meta.subtitle ? ', ' + page.meta.subtitle : '') + ' :: ' + siteInfo.title) - : siteInfo.title, - description: page.meta.description || siteInfo.description, + ? (page.meta.title + (page.meta.subtitle ? ', ' + page.meta.subtitle : '') + ' :: ' + i18n(lang, 'SITE_TITLE')) + : i18n(lang, 'SITE_TITLE'), + description: page.meta.description || i18n('SITE_DESCRIPTION'), }, site: siteInfo, local: { diff --git a/lang/en.js b/lang/en.js new file mode 100644 index 0000000..c7ad24b --- /dev/null +++ b/lang/en.js @@ -0,0 +1,11 @@ + +module.exports = exports = { + SITE_TITLE: 'That\'s Gender Dysphoria, FYI', + SITE_DESCRIPTION: 'A resource for those questioning their gender, already on a gender journey, or simply wanting to learn more about what it is to be transgender.', + + HEADER_TITLE: 'That\'s Gender Dysphoria, FYI', + PATREON_FOOTER_BODY: 'This site is made possible thanks to contributions from the community. If you have found this site invaluable, please consider making a pledge on patreon or a donation via Ko-fi.', + PATREON_FOOTER_THANKS: 'Special thanks to the following patreon supporters:', + FOOTER_COPYRIGHT: 'Site text, website code and original graphics are licensed CC BY-NC-SA.', +}; + diff --git a/lang/es.js b/lang/es.js new file mode 100644 index 0000000..d23fa67 --- /dev/null +++ b/lang/es.js @@ -0,0 +1,8 @@ + +module.exports = exports = { + SITE_TITLE: 'Eso es Disforia de Género, PSI', + + HEADER_TITLE: 'Eso es Disforia de Género, PSI', + +}; + diff --git a/lang/index.js b/lang/index.js new file mode 100644 index 0000000..bfbb034 --- /dev/null +++ b/lang/index.js @@ -0,0 +1,12 @@ + +const languages = { + en: require('./en'), + es: require('./es'), +}; + +module.exports = exports = function (lang, key, ...args) { + var entry = languages[lang] && languages[lang][key]; + if (!entry) entry = languages.en[key]; + if (typeof entry === 'function') return entry(...args); + return entry || key; +}; diff --git a/package.json b/package.json index 72e2b02..2c8c77e 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "siteInfo": { "title": "That's Gender Dysphoria, FYI", "domain": "genderdysphoria.fyi", - "lang": "en-US", + "lang": "en", "siteUrl": "https://genderdysphoria.fyi", "description": "A resource for those questioning their gender, already on a gender journey, or simply wanting to learn more about what it is to be transgender.", "creator": "TwippingVanilla", diff --git a/public/_gdb-en-menu.hbs b/public/_gdb-en-menu.hbs index 0816736..f0e2f61 100644 --- a/public/_gdb-en-menu.hbs +++ b/public/_gdb-en-menu.hbs @@ -1,27 +1,28 @@ - + + diff --git a/public/_gdb-es-menu.hbs b/public/_gdb-es-menu.hbs index ae6564e..44d9d2b 100644 --- a/public/_gdb-es-menu.hbs +++ b/public/_gdb-es-menu.hbs @@ -1,27 +1,28 @@ - + + diff --git a/public/_gdb-menu.hbs b/public/_gdb-menu.hbs index 9d96d10..478b022 100644 --- a/public/_gdb-menu.hbs +++ b/public/_gdb-menu.hbs @@ -1,5 +1,6 @@ + {{#is page.lang "es"}} -{{import '/public/_gdb-es-menu'}} +{{import '/public/_gdb-es-menu'}} {{else}} -{{import '/public/_gdb-en-menu'}} -{{/is}} \ No newline at end of file +{{import '/public/_gdb-en-menu'}} +{{/is}} diff --git a/templates/layout.hbs b/templates/layout.hbs index 8e7109b..46b6d8e 100644 --- a/templates/layout.hbs +++ b/templates/layout.hbs @@ -59,22 +59,16 @@
@@ -84,11 +78,8 @@