UNPKG

1.79 kBJavaScriptView Raw
1/** Copyright (c) 2018 Uber Technologies, Inc.
2 *
3 * This source code is licensed under the MIT license found in the
4 * LICENSE file in the root directory of this source tree.
5 *
6 * @flow
7 */
8
9import {Locale, Locales} from 'locale';
10import fs from 'fs';
11import path from 'path';
12
13import {memoize} from 'fusion-core';
14import type {Context} from 'fusion-core';
15
16import type {TranslationsObjectType} from './types.js';
17
18export type I18nLoaderType = {
19 from: (
20 ctx: Context
21 ) => {locale: string | Locale, translations: TranslationsObjectType},
22};
23export type LocaleResolverType = (ctx: Context) => string;
24export type LoaderFactoryType = (
25 resolveLocales?: LocaleResolverType
26) => I18nLoaderType;
27
28const defaultResolveLocales: LocaleResolverType = ctx =>
29 ctx.headers['accept-language'];
30
31const loader: LoaderFactoryType = (resolveLocales = defaultResolveLocales) => {
32 const readDir = root => {
33 try {
34 return fs.readdirSync(root);
35 } catch (e) {
36 return [];
37 }
38 };
39 const root = './translations';
40 const locales = readDir(root)
41 .filter(p => p.match(/json$/))
42 .map(p => p.replace(/\.json$/, ''));
43 const data = locales.reduce((memo, locale) => {
44 const parsedLocale = new Locale(locale);
45 memo[parsedLocale.normalized] = JSON.parse(
46 fs.readFileSync(path.join(root, locale + '.json'), 'utf8')
47 );
48 return memo;
49 }, {});
50 const supportedLocales = new Locales(locales);
51
52 return {
53 from: memoize(ctx => {
54 const expectedLocales = new Locales(resolveLocales(ctx));
55 const locale = expectedLocales.best(supportedLocales);
56 const translations: TranslationsObjectType = data[locale.normalized];
57 return {translations, locale};
58 }),
59 };
60};
61
62export default (((__NODE__ ? loader : null): any): typeof loader);