lr-i18n
Version:
Allows to connect your `Laravel` Framework localization files with `React`.
25 lines (24 loc) • 1.2 kB
JavaScript
import recognizer from '../utils/recognizer';
/**
* Resolver the language file.
*
* @param files
* @param locale
*/
export default function resolver(files, locale) {
const { isJsonLocale, isPhpLocale, getJsonFile, getPhpFile } = recognizer(files);
const jsonLocale = isJsonLocale(locale) ? files[getJsonFile(locale)] : undefined;
const phpLocale = isPhpLocale(locale) ? files[getPhpFile(locale)] : undefined;
const getType = (obj) => Object.prototype.toString.call(obj);
if (['[object Promise]', '[object Module]'].includes(getType(jsonLocale)) ||
['[object Promise]', '[object Module]'].includes(getType(phpLocale))) {
return [jsonLocale ? jsonLocale : { default: {} }, phpLocale ? phpLocale : { default: {} }];
}
if (getType(jsonLocale) === '[object Object]' || getType(phpLocale) === '[object Object]') {
return [{ default: jsonLocale || {} }, { default: phpLocale || {} }];
}
if (getType(jsonLocale) === '[object Function]' || getType(phpLocale) === '[object Function]') {
return [jsonLocale ? jsonLocale() : { default: {} }, phpLocale ? phpLocale() : { default: {} }];
}
return [{ default: {} }, { default: {} }];
}