ampache_react/index.all.js

90 lines
2.6 KiB
JavaScript
Raw Normal View History

/**
* 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
// Store
import configureStore from "./app/store/configureStore";
// i18n stuff
import { addLocaleData } from "react-intl";
import en from "react-intl/locale-data/en";
import fr from "react-intl/locale-data/fr";
import { getBrowserLocales } from "./app/utils";
import rawMessages from "./app/locales";
// Init store and history
2016-07-26 13:21:37 +02:00
const store = configureStore();
const history = syncHistoryWithStore(hashHistory, store);
// 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
/**
* 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
addLocaleData([...en, ...fr]);
// Fetch current preferred locales from the browser
const locales = getBrowserLocales();
var locale = "en-US"; // Safe default
// Populate strings with best matching locale
var strings = {};
for (var i = 0; i < locales.length; ++i) {
if (rawMessages[locales[i]]) {
locale = locales[i];
strings = rawMessages[locale];
break; // Break at first matching locale
}
}
// Overload strings with default English translation, in case of missing translations
strings = Object.assign(rawMessages["en-US"], strings);
// 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
// Return a rendering function
return () => {
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
);
};
};
/**
* Ensure window.Intl exists, or polyfill it.
*
* @param render Initial rendering function.
*/
export function Intl (render) {
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();
});
} else {
render();
}
};