Skip to content
This repository was archived by the owner on Oct 13, 2021. It is now read-only.

Commit 5a278d4

Browse files
author
Iain Collins
committed
Fix compatibility with the latest version of Lingqui
* Update to resolve incompatiblity with breaking changes in Lingui * Somewhat hacky approach in _document.js to resolve the issue (changed export type to cjs, use string replace) * Now also checks hostname for the locale (e.g. `en.example.com`, `fr.example.com`)
1 parent b7a058f commit 5a278d4

File tree

5 files changed

+16
-14
lines changed

5 files changed

+16
-14
lines changed

.linguirc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"compileNamespace": "window.LINGUI_CATALOG",
2+
"compileNamespace": "cjs",
33
"fallbackLocale": "en",
44
"sourceLocale": "en",
55
"localeDir": "<rootDir>/locale",

locale/en/messages.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

locale/es/messages.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

locale/fr/messages.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pages/_document.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import NextDocument, { Head, Main, NextScript } from "next/document";
22

3-
const supportedLocale = ["en", "fr", "es"];
3+
const supportedLocales = ["en", "fr", "es"];
44

55
export default class Document extends NextDocument {
66
static async getInitialProps(ctx) {
7-
const queryLocale = ctx.query.locale;
8-
const locale = supportedLocale.find(l => l === queryLocale)
9-
? queryLocale
10-
: "en";
11-
12-
const linguiCatalog = await import(`raw-loader!../locale/${locale}/messages.js`).then(
13-
mod => mod.default
14-
);
15-
167
const initialProps = await NextDocument.getInitialProps(ctx);
8+
9+
// Try to detect locale from:
10+
// * locale query string param (e.g. ?locale=fr)
11+
// * hostname (e.g. `en.example.com`, `fr.example.com`, etc)
12+
// First locale specified in supportedLocales is the default fallback used
13+
const detectedLocale = ctx.query.locale || ctx.req.headers.host.split('.')[0];
14+
const locale = supportedLocales.find(l => l === detectedLocale) ? detectedLocale : supportedLocales[0];
15+
16+
// Load source for current translation file and inject into page (hacky!)
17+
let linguiCatalog = await import(`raw-loader!../locale/${locale}/messages.js`).then(mod => mod.default);
18+
linguiCatalog = linguiCatalog.replace('module.exports = {', 'window.LINGUI_CATALOG = {');
1719

1820
return { ...initialProps, linguiCatalog };
1921
}

0 commit comments

Comments
 (0)