Skip to content
This repository was archived by the owner on Oct 13, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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