Skip to content

Commit

Permalink
Improved razzle-ssr example (#1338)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ibadichan authored Jul 3, 2021
1 parent f08a82c commit c0fb97f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions example/razzle-ssr/src/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import LanguageDetector from 'i18next-browser-languagedetector';

const options = {
fallbackLng: 'en',
supportedLngs: ['en', 'de'],
load: 'languageOnly', // we only provide en, de -> no region specific locals like en-US, de-DE
// have a common namespace used around the full app
ns: ['translations'],
Expand Down
17 changes: 14 additions & 3 deletions example/razzle-ssr/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,22 @@ i18n
if (url) {
res.redirect(url);
} else {
// First preferred language
const initialLanguage = req.i18n.languages[0];

// Fill initialI18nStore with only the necessary namespaces.
const initialI18nStore = {};
req.i18n.languages.forEach(l => {
initialI18nStore[l] = req.i18n.services.resourceStore.data[l];
const usedNamespaces = req.i18n.reportNamespaces.getUsedNamespaces();

req.i18n.languages.forEach((language) => {
initialI18nStore[language] = {};

usedNamespaces.forEach((namespace) => {
initialI18nStore[language][namespace] = (
req.i18n.services.resourceStore.data[language][namespace]
);
});
});
const initialLanguage = req.i18n.language;

res.status(200).send(
`<!doctype html>
Expand Down

0 comments on commit c0fb97f

Please sign in to comment.