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

Commit

Permalink
Fix compatibility with the latest version of Lingui
Browse files Browse the repository at this point in the history
* 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`)
  • Loading branch information
Iain Collins committed May 28, 2019
1 parent b7a058f commit 1061b86
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .linguirc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"compileNamespace": "window.LINGUI_CATALOG",
"compileNamespace": "cjs",
"fallbackLocale": "en",
"sourceLocale": "en",
"localeDir": "<rootDir>/locale",
Expand Down
2 changes: 1 addition & 1 deletion locale/en/messages.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion locale/es/messages.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion locale/fr/messages.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 12 additions & 10 deletions pages/_document.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import NextDocument, { Head, Main, NextScript } from "next/document";

const supportedLocale = ["en", "fr", "es"];
const supportedLocales = ["en", "fr", "es"];

export default class Document extends NextDocument {
static async getInitialProps(ctx) {
const queryLocale = ctx.query.locale;
const locale = supportedLocale.find(l => l === queryLocale)
? queryLocale
: "en";

const linguiCatalog = await import(`raw-loader!../locale/${locale}/messages.js`).then(
mod => mod.default
);

const initialProps = await NextDocument.getInitialProps(ctx);

// Try to detect locale from:
// * locale query string param (e.g. ?locale=fr)
// * hostname (e.g. `en.example.com`, `fr.example.com`, etc)
// First locale specified in supportedLocales is the default fallback used
const detectedLocale = ctx.query.locale || ctx.req.headers.host.split('.')[0];
const locale = supportedLocales.find(l => l === detectedLocale) ? detectedLocale : supportedLocales[0];

// Load source for current translation file and inject into page (hacky!)
let linguiCatalog = await import(`raw-loader!../locale/${locale}/messages.js`).then(mod => mod.default);
linguiCatalog = linguiCatalog.replace('module.exports = {', 'window.LINGUI_CATALOG = {');

return { ...initialProps, linguiCatalog };
}
Expand Down

0 comments on commit 1061b86

Please sign in to comment.