feat(locales): add automatic locale fallback

This commit is contained in:
Ash Monty 2021-12-09 21:03:01 +01:00
parent eb35ec8feb
commit 2481749648
No known key found for this signature in database
GPG Key ID: 24F2A3D0CBE0A68F
7 changed files with 54 additions and 29 deletions

View File

@ -9,6 +9,8 @@ const logger = require('./logger');
const util = require('./util');
const config = require('../config.json');
const defaultLocale = require('../locales/US_en.json');
const { http: { port } } = config;
const app = express();
@ -118,6 +120,29 @@ app.engine('handlebars', handlebars({
},
neq(value1, value2) {
return value1 !== value2;
},
localeHelper(...args) {
let userLocaleString = args[0];
/*
* Removes the first and the last argument, and then loops through the rest to
* get the string in the user's locale. If not available, it will return it in
* the default locale.
*/
args.slice(1, -1).forEach(arg => {
userLocaleString = userLocaleString?.[arg];
});
if (!userLocaleString) {
let defaultLocaleString = defaultLocale;
args.slice(1, -1).forEach(arg => {
defaultLocaleString = defaultLocaleString?.[arg];
});
return defaultLocaleString;
} else {
return userLocaleString;
}
}
}
}));

View File

@ -7,8 +7,8 @@
<div class="progress-hero">
<div class="hero-meta reduced-margin">
<svg xmlns="http://www.w3.org/2000/svg" width="48.87" height="71.093" viewBox="0 0 48.87 71.093"><g id="XMLID_6_" transform="translate(0)"><path id="XMLID_15_" d="M69.581,29.593c-2.029,1.068-.249,4.129,1.78,3.061,5.162-2.67,11.463-2.6,16.981-1.1,4.735,1.282,9.5,3.845,12.246,8.045,1.246,1.922,4.307.142,3.061-1.78C96.921,27.386,80.3,24.04,69.581,29.593Z" transform="translate(-60.112 -20.086)" fill="#9d6ff3"/><path id="XMLID_14_" d="M103.359,21.045c-3.951-6.159-10.751-10-17.657-11.89C77.763,6.948,68.721,7.019,61.281,10.9c-2.029,1.068-.249,4.129,1.78,3.061,6.586-3.453,14.667-3.311,21.644-1.388,5.981,1.638,12.1,4.913,15.521,10.252C101.507,24.783,104.569,23,103.359,21.045Z" transform="translate(-54.766 -7.693)" fill="#9d6ff3"/><path id="XMLID_9_" d="M65.995,47.8a20.7,20.7,0,0,0-12.958,4.45H47.27a2.579,2.579,0,0,0-2.67,2.456v47.239a2.763,2.763,0,0,0,2.67,2.67h5.838a2.639,2.639,0,0,0,2.528-2.67V87.564A21.228,21.228,0,1,0,65.995,47.8Zm0,33.178a12,12,0,1,1,12-12A12,12,0,0,1,65.995,80.978Z" transform="translate(-44.6 -33.522)" fill="#9d6ff3"/></g></svg>
<h1 class="title dot">{{ locale.blogPage.title }}</h1>
<p class="text">{{{ locale.blogPage.description }}}</p>
<h1 class="title dot">{{ localeHelper locale "blogPage" "title" }}</h1>
<p class="text">{{{ localeHelper locale "blogPage" "description" }}}</p>
</div>
</div>

View File

@ -34,7 +34,7 @@
<div class="content">
<div class="content-inner">
{{#if missingInLocale}}
<p class="missing-in-locale-notice">{{locale.docs.missingInLocale}}</p>
<p class="missing-in-locale-notice">{{ localeHelper locale "docs" "missingInLocale" }}</p>
{{/if}}
{{#if showQuickLinks}}

View File

@ -5,15 +5,15 @@
<section class="hero">
<div class="hero-meta">
<h3 class="subtitle">{{ locale.hero.subtitle }}</h3>
<h1 class="title dot" {{#if locale.hero.titleSuffix}}data-title-suffix="{{locale.hero.titleSuffix}}"{{/if}}>{{ locale.hero.title }}</h1>
<h3 class="subtitle">{{ localeHelper locale "hero" "subtitle" }}</h3>
<h1 class="title dot" {{#if locale.hero.titleSuffix}}data-title-suffix="{{locale.hero.titleSuffix}}"{{/if}}>{{ localeHelper locale "hero" "title" }}</h1>
<p class="text">
{{ locale.hero.text }}
{{ localeHelper locale "hero" "text" }}
</p>
<div class="buttons">
<a href="/#about" class="do-button-margin">
<button class="button primary">
{{ locale.hero.buttons.readMore }}
{{ localeHelper locale "hero" "buttons" "readMore" }}
</button>
</a>
<a href="https://invite.gg/pretendo" class="do-button-margin" target="_blank">
@ -75,22 +75,22 @@
<section class="progress column-2">
<div class="left sect">
<div class="left-meta">
<h2 class="dot title" id="about" {{#if locale.aboutUs.titleSuffix}}data-title-suffix="{{locale.aboutUs.titleSuffix}}"{{/if}}>{{ locale.aboutUs.title }}</h2>
<h2 class="dot title" id="about" {{#if locale.aboutUs.titleSuffix}}data-title-suffix="{{locale.aboutUs.titleSuffix}}"{{/if}}>{{ localeHelper locale "aboutUs" "title" }}</h2>
{{#each locale.aboutUs.paragraphs}}
<p class="text">{{ this }}</p>
{{/each}}
</div>
</div>
<div class="right sect">
<h2 class="title">{{ locale.progress.title }}</h2>
<h2 class="title">{{ localeHelper locale "progress" "title" }}</h2>
{{> progress-list data=featuredFeatureList purple=true boards=boards }}
</div>
</section>
<section class="faq">
<div class="sect-top sect">
<h2 class="dot title" id="faq" {{#if locale.faq.titleSuffix}}data-title-suffix="{{locale.faq.titleSuffix}}"{{/if}}>{{ locale.faq.title }}</h2>
<p class="text">{{ locale.faq.text }}</p>
<h2 class="dot title" id="faq" {{#if locale.faq.titleSuffix}}data-title-suffix="{{locale.faq.titleSuffix}}"{{/if}}>{{ localeHelper locale "faq" "title" }}</h2>
<p class="text">{{ localeHelper locale "faq" "text" }}</p>
</div>
<div class="questions column-2">
{{#doFaq locale.faq.QAs}}
@ -108,8 +108,8 @@
<section class="team">
<div class="sect-top sect team-top">
<h2 class="dot title" id="credits" {{#if locale.credits.titleSuffix}}data-title-suffix="{{locale.credits.titleSuffix}}"{{/if}}>{{ locale.credits.title }}</h2>
<p class="text">{{ locale.credits.text }}</p>
<h2 class="dot title" id="credits" {{#if locale.credits.titleSuffix}}data-title-suffix="{{locale.credits.titleSuffix}}"{{/if}}>{{ localeHelper locale "credits" "title" }}</h2>
<p class="text">{{ localeHelper locale "credits" "text" }}</p>
</div>
<div class="team-cards">
{{#each locale.credits.people}}
@ -134,8 +134,8 @@
<section class="team team-helpers">
<div class="sect-top sect team-top">
<h2 class="dot title" id="special-thanks" {{#if locale.specialThanks.titleSuffix}}data-title-suffix="{{locale.specialThanks.titleSuffix}}"{{/if}}>{{ locale.specialThanks.title }}</h2>
<p class="text">{{ locale.specialThanks.text }}</p>
<h2 class="dot title" id="special-thanks" {{#if locale.specialThanks.titleSuffix}}data-title-suffix="{{locale.specialThanks.titleSuffix}}"{{/if}}>{{ localeHelper locale "specialThanks" "title" }}</h2>
<p class="text">{{ localeHelper locale "specialThanks" "text" }}</p>
</div>
<div class="team-cards">
{{#each locale.specialThanks.people}}

View File

@ -6,17 +6,17 @@
<div class="localization-wrapper">
<div class="localization-widget">
<h1 class="title dot">{{ locale.localizationPage.title }}</h1>
<p class="caption">{{ locale.localizationPage.description }}</p>
<h1 class="title dot">{{ localeHelper locale "localizationPage" "title" }}</h1>
<p class="caption">{{ localeHelper locale "localizationPage" "description" }}</p>
<a href="https://github.com/PretendoNetwork/Pretendo/blob/master/CONTRIBUTING.md#localization" target="_blank" class="localization-instr">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="var(--theme-light)" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-right"><line x1="5" y1="12" x2="19" y2="12"></line><polyline points="12 5 19 12 12 19"></polyline></svg>
{{ locale.localizationPage.instructions }}
{{ localeHelper locale "localizationPage" "instructions" }}
</a>
<form class="localization-form" autocomplete="off">
<label for="url">{{ locale.localizationPage.fileInput }}</label>
<label for="url">{{ localeHelper locale "localizationPage" "fileInput" }}</label>
<div class="input-wrapper">
<input type="url" id="url" placeholder="{{ locale.localizationPage.filePlaceholder }}" required />
<button type="submit" class="submit-btn">{{ locale.localizationPage.button }}</button>
<input type="url" id="url" placeholder="{{ localeHelper locale "localizationPage" "filePlaceholder" }}" required />
<button type="submit" class="submit-btn">{{ localeHelper locale "localizationPage" "button" }}</button>
</div>
</form>
</div>

View File

@ -25,12 +25,12 @@
</a>
<nav>
<a href="/#about">{{ locale.nav.about }}</a>
<a href="/#faq">{{ locale.nav.faq }}</a>
<a href="/#credits">{{ locale.nav.credits }}</a>
<a href="/docs">{{ locale.nav.docs }}</a>
<a href="/progress" class="keep-on-mobile">{{ locale.nav.progress }}</a>
<a href="/blog" class="keep-on-mobile">{{ locale.nav.blog }}</a>
<a href="/#about">{{ localeHelper locale "nav" "about" }}</a>
<a href="/#faq">{{ localeHelper locale "nav" "faq" }}</a>
<a href="/#credits">{{ localeHelper locale "nav" "credits" }}</a>
<a href="/docs">{{ localeHelper locale "nav" "docs"}}</a>
<a href="/progress" class="keep-on-mobile">{{ localeHelper locale "nav" "progress"}}</a>
<a href="/blog" class="keep-on-mobile">{{ localeHelper locale "nav" "blog" }}</a>
</nav>
<!-- Ordered the locales in the same way YouTube's language selector orders them -->

View File

@ -5,8 +5,8 @@
<div class="progress-hero">
<div class="hero-meta reduced-margin">
<svg xmlns="http://www.w3.org/2000/svg" width="48.87" height="71.093" viewBox="0 0 48.87 71.093"><g id="XMLID_6_" transform="translate(0)"><path id="XMLID_15_" d="M69.581,29.593c-2.029,1.068-.249,4.129,1.78,3.061,5.162-2.67,11.463-2.6,16.981-1.1,4.735,1.282,9.5,3.845,12.246,8.045,1.246,1.922,4.307.142,3.061-1.78C96.921,27.386,80.3,24.04,69.581,29.593Z" transform="translate(-60.112 -20.086)" fill="#9d6ff3"/><path id="XMLID_14_" d="M103.359,21.045c-3.951-6.159-10.751-10-17.657-11.89C77.763,6.948,68.721,7.019,61.281,10.9c-2.029,1.068-.249,4.129,1.78,3.061,6.586-3.453,14.667-3.311,21.644-1.388,5.981,1.638,12.1,4.913,15.521,10.252C101.507,24.783,104.569,23,103.359,21.045Z" transform="translate(-54.766 -7.693)" fill="#9d6ff3"/><path id="XMLID_9_" d="M65.995,47.8a20.7,20.7,0,0,0-12.958,4.45H47.27a2.579,2.579,0,0,0-2.67,2.456v47.239a2.763,2.763,0,0,0,2.67,2.67h5.838a2.639,2.639,0,0,0,2.528-2.67V87.564A21.228,21.228,0,1,0,65.995,47.8Zm0,33.178a12,12,0,1,1,12-12A12,12,0,0,1,65.995,80.978Z" transform="translate(-44.6 -33.522)" fill="#9d6ff3"/></g></svg>
<h1 class="title dot">{{ locale.progressPage.title }}</h1>
<p class="text">{{ locale.progressPage.description }}</p>
<h1 class="title dot">{{ localeHelper locale "progressPage" "title" }}</h1>
<p class="text">{{ localeHelper locale "progressPage" "description" }}</p>
</div>
</div>