diff --git a/.linguirc b/.linguirc index 14fb363..8999d6c 100644 --- a/.linguirc +++ b/.linguirc @@ -1,5 +1,5 @@ { - "compileNamespace": "window.LINGUI_CATALOG", + "compileNamespace": "cjs", "fallbackLocale": "en", "sourceLocale": "en", "localeDir": "/locale", diff --git a/locale/en/messages.js b/locale/en/messages.js index 4511c47..95cdce3 100644 --- a/locale/en/messages.js +++ b/locale/en/messages.js @@ -1 +1 @@ -/* eslint-disable */window.LINGUI_CATALOG={languageData:{"plurals":function(n,ord){var s=String(n).split("."),v0=!s[1],t0=Number(s[0])==n,n10=t0&&s[0].slice(-1),n100=t0&&s[0].slice(-2);if(ord)return n10==1&&n100!=11?"one":n10==2&&n100!=12?"two":n10==3&&n100!=13?"few":"other";return n==1&&v0?"one":"other"}},messages:{"description":"This is a demo of Lingui in Next.js","form.name.label":"Enter your name","form.name.placeholder":"Your name","greeting":function(a){return["Hello ",a("name")]}}}; \ No newline at end of file +/* eslint-disable */module.exports={languageData:{"plurals":function(n,ord){var s=String(n).split("."),v0=!s[1],t0=Number(s[0])==n,n10=t0&&s[0].slice(-1),n100=t0&&s[0].slice(-2);if(ord)return n10==1&&n100!=11?"one":n10==2&&n100!=12?"two":n10==3&&n100!=13?"few":"other";return n==1&&v0?"one":"other"}},messages:{"description":"This is a demo of Lingui in Next.js","form.name.label":"Enter your name","form.name.placeholder":"Your name","greeting":function(a){return["Hello ",a("name")]}}}; \ No newline at end of file diff --git a/locale/es/messages.js b/locale/es/messages.js index 017c470..9566925 100644 --- a/locale/es/messages.js +++ b/locale/es/messages.js @@ -1 +1 @@ -/* eslint-disable */window.LINGUI_CATALOG={languageData:{"plurals":function(n,ord){if(ord)return"other";return n==1?"one":"other"}},messages:{"description":"Esta es la demo de Lingui en Next.js","form.name.label":"Introduzca su nombre","form.name.placeholder":"Tu nombre","greeting":function(a){return["Hola ",a("name")]}}}; \ No newline at end of file +/* eslint-disable */module.exports={languageData:{"plurals":function(n,ord){if(ord)return"other";return n==1?"one":"other"}},messages:{"description":"Esta es la demo de Lingui en Next.js","form.name.label":"Introduzca su nombre","form.name.placeholder":"Tu nombre","greeting":function(a){return["Hola ",a("name")]}}}; \ No newline at end of file diff --git a/locale/fr/messages.js b/locale/fr/messages.js index dfee8ec..d86b27e 100644 --- a/locale/fr/messages.js +++ b/locale/fr/messages.js @@ -1 +1 @@ -/* eslint-disable */window.LINGUI_CATALOG={languageData:{"plurals":function(n,ord){if(ord)return n==1?"one":"other";return n>=0&&n<2?"one":"other"}},messages:{"description":"Ceci est la d\xE9mo de Lingui dans Next.js","form.name.label":"Entrez votre nom","form.name.placeholder":"Votre nom","greeting":function(a){return["Bonjour ",a("name")]}}}; \ No newline at end of file +/* eslint-disable */module.exports={languageData:{"plurals":function(n,ord){if(ord)return n==1?"one":"other";return n>=0&&n<2?"one":"other"}},messages:{"description":"Ceci est la d\xE9mo de Lingui dans Next.js","form.name.label":"Entrez votre nom","form.name.placeholder":"Votre nom","greeting":function(a){return["Bonjour ",a("name")]}}}; \ No newline at end of file diff --git a/pages/_document.js b/pages/_document.js index e0d5a16..6290212 100644 --- a/pages/_document.js +++ b/pages/_document.js @@ -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 }; }