2016-08-10 21:36:11 +02:00
|
|
|
/**
|
|
|
|
* Main JS entry point for all the builds.
|
|
|
|
*
|
|
|
|
* Performs i18n and initial render.
|
|
|
|
*/
|
|
|
|
// React stuff
|
2016-07-26 13:21:37 +02:00
|
|
|
import React from "react";
|
2016-07-26 15:59:18 +02:00
|
|
|
import ReactDOM from "react-dom";
|
2016-08-03 22:31:11 +02:00
|
|
|
import { applyRouterMiddleware, hashHistory } from "react-router";
|
2016-07-26 13:21:37 +02:00
|
|
|
import { syncHistoryWithStore } from "react-router-redux";
|
2016-08-03 22:31:11 +02:00
|
|
|
import useScroll from "react-router-scroll";
|
2016-07-26 13:21:37 +02:00
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
// Store
|
|
|
|
import configureStore from "./app/store/configureStore";
|
|
|
|
|
|
|
|
// i18n stuff
|
2016-07-28 01:22:48 +02:00
|
|
|
import { addLocaleData } from "react-intl";
|
|
|
|
import en from "react-intl/locale-data/en";
|
|
|
|
import fr from "react-intl/locale-data/fr";
|
|
|
|
|
2016-08-01 00:26:52 +02:00
|
|
|
import { getBrowserLocales } from "./app/utils";
|
2016-07-28 01:22:48 +02:00
|
|
|
import rawMessages from "./app/locales";
|
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
// Init store and history
|
2016-07-26 13:21:37 +02:00
|
|
|
const store = configureStore();
|
|
|
|
const history = syncHistoryWithStore(hashHistory, store);
|
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
// Get root element
|
2016-07-30 22:54:19 +02:00
|
|
|
export const rootElement = document.getElementById("root");
|
2016-07-26 15:59:18 +02:00
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
/**
|
|
|
|
* Main function to be called once window.Intl has been populated.
|
|
|
|
*
|
|
|
|
* Populates the locales messages and perform render.
|
|
|
|
*/
|
|
|
|
export function onWindowIntl () {
|
|
|
|
// Add locales we support
|
2016-07-28 01:22:48 +02:00
|
|
|
addLocaleData([...en, ...fr]);
|
2016-08-10 21:36:11 +02:00
|
|
|
|
|
|
|
// Fetch current preferred locales from the browser
|
2016-08-01 00:26:52 +02:00
|
|
|
const locales = getBrowserLocales();
|
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
var locale = "en-US"; // Safe default
|
|
|
|
// Populate strings with best matching locale
|
2016-08-01 00:26:52 +02:00
|
|
|
var strings = {};
|
|
|
|
for (var i = 0; i < locales.length; ++i) {
|
|
|
|
if (rawMessages[locales[i]]) {
|
|
|
|
locale = locales[i];
|
|
|
|
strings = rawMessages[locale];
|
2016-08-10 21:36:11 +02:00
|
|
|
break; // Break at first matching locale
|
2016-08-01 00:26:52 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-10 21:36:11 +02:00
|
|
|
// Overload strings with default English translation, in case of missing translations
|
2016-07-28 01:22:48 +02:00
|
|
|
strings = Object.assign(rawMessages["en-US"], strings);
|
2016-08-10 21:36:11 +02:00
|
|
|
|
|
|
|
// Dynamically set html lang attribute
|
2016-08-02 13:59:26 +02:00
|
|
|
document.documentElement.lang = locale;
|
2016-07-26 15:59:18 +02:00
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
// Return a rendering function
|
|
|
|
return () => {
|
2016-07-28 01:22:48 +02:00
|
|
|
const Root = require("./app/containers/Root").default;
|
2016-07-26 15:59:18 +02:00
|
|
|
ReactDOM.render(
|
2016-08-03 22:31:11 +02:00
|
|
|
<Root store={store} history={history} render={applyRouterMiddleware(useScroll())} locale={locale} defaultLocale="en-US" messages={strings} />,
|
2016-07-26 15:59:18 +02:00
|
|
|
rootElement
|
|
|
|
);
|
|
|
|
};
|
2016-07-28 23:14:52 +02:00
|
|
|
};
|
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
/**
|
|
|
|
* Ensure window.Intl exists, or polyfill it.
|
|
|
|
*
|
|
|
|
* @param render Initial rendering function.
|
|
|
|
*/
|
|
|
|
export function Intl (render) {
|
2016-07-28 23:14:52 +02:00
|
|
|
if (!window.Intl) {
|
|
|
|
require.ensure([
|
|
|
|
"intl",
|
|
|
|
"intl/locale-data/jsonp/en.js",
|
|
|
|
"intl/locale-data/jsonp/fr.js"
|
|
|
|
], function (require) {
|
|
|
|
require("intl");
|
|
|
|
require("intl/locale-data/jsonp/en.js");
|
|
|
|
require("intl/locale-data/jsonp/fr.js");
|
|
|
|
render();
|
2016-07-28 01:22:48 +02:00
|
|
|
});
|
2016-07-28 23:14:52 +02:00
|
|
|
} else {
|
|
|
|
render();
|
2016-07-28 01:22:48 +02:00
|
|
|
}
|
|
|
|
};
|