Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vue sample app - set i18n initial language based on SSR language or s… #337

Merged
Merged
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
21 changes: 13 additions & 8 deletions samples/vue/src/main.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import i18ninit from './i18n';
import { createApp } from './createApp';
import config from './temp/config';

/* eslint-disable no-underscore-dangle */

let initLanguage = config.defaultLanguage;

/*
SSR Data
If we're running in a server-side rendering scenario,
Expand All @@ -13,20 +16,22 @@ import { createApp } from './createApp';

SSR is initiated from /server/server.js.
*/
let __JSS_STATE__ = null;
const ssrRawJson = document.getElementById('__JSS_STATE__');
if (ssrRawJson) {
__JSS_STATE__ = JSON.parse(ssrRawJson.innerHTML);
}
if (__JSS_STATE__) {
// set i18n language SSR state language instead of static config default language
initLanguage = __JSS_STATE__.sitecore.context.language;
}

// initialize the dictionary, then render the app
// note: if not making a multlingual app, the dictionary init can be removed.
i18ninit().then((i18n) => {
i18ninit(initLanguage).then((i18n) => {
// HTML element to place the app into
const rootElement = document.getElementById('root');

// retrieve the initial app state if it is defined
let __JSS_STATE__ = null;
const ssrRawJson = document.getElementById('__JSS_STATE__');
if (ssrRawJson) {
__JSS_STATE__ = JSON.parse(ssrRawJson.innerHTML);
}

const initialState = __JSS_STATE__ || null;

const { app } = createApp(initialState, i18n);
Expand Down