Skip to content

Commit

Permalink
Intl polyfill for unsupported clients [closes kriasoft#639]
Browse files Browse the repository at this point in the history
  • Loading branch information
langpavel committed May 26, 2016
1 parent 21c77a8 commit cbb064e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/clientConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// default locale is the first one
export const locales = ['en', 'cs'];
28 changes: 28 additions & 0 deletions src/clientLoader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

import areIntlLocalesSupported from 'intl-locales-supported';

import { locales } from './clientConfig';

if (global.Intl) {
// Determine if the built-in `Intl` has the locale data we need.
if (!areIntlLocalesSupported(locales)) {
// `Intl` exists, but it doesn't have the data we need, so load the
// polyfill and replace the constructors with need with the polyfill's.
require.ensure(['intl', './client'], require => {
const IntlPolyfill = require('intl');
Intl.NumberFormat = IntlPolyfill.NumberFormat;
Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat;
require('./client');
});
} else {
require.ensure(['./client'], require => {
require('./client');
});
}
} else {
// No `Intl`, so use and load the polyfill.
require.ensure(['intl', './client'], require => {
global.Intl = require('intl');
require('./client');
});
}
5 changes: 2 additions & 3 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
/* eslint-disable max-len */
/* jscs:disable maximumLineLength */

export { locales } from './clientConfig';

export const port = process.env.PORT || 3000;
export const host = process.env.WEBSITE_HOSTNAME || `localhost:${port}`;

// default locale is the first one
export const locales = ['en', 'cs'];

export const databaseUrl = process.env.DATABASE_URL || 'sqlite:database.sqlite';

export const analytics = {
Expand Down
2 changes: 1 addition & 1 deletion tools/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ const config = {
// -----------------------------------------------------------------------------

const clientConfig = extend(true, {}, config, {
entry: './client.js',
entry: './clientLoader.js',

output: {
filename: DEBUG ? '[name].js?[chunkhash]' : '[name].[chunkhash].js',
Expand Down

0 comments on commit cbb064e

Please sign in to comment.