UNPKG

lr-i18n

Version:

Allows to connect your `Laravel` Framework localization files with `React`.

112 lines (111 loc) 5.16 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = i18n; const fs_1 = __importDefault(require("fs")); const vite_1 = require("vite"); const key_type_1 = require("./plugin/key-type"); const parser_1 = __importDefault(require("./plugin/parser")); const locale_1 = __importDefault(require("./plugin/locale")); /** * */ function i18n(config) { const langDirname = (config === null || config === void 0 ? void 0 : config.langDirname) ? config.langDirname : 'lang'; const logger = (0, vite_1.createLogger)('info', { prefix: '[laravel-react-i18n]' }); let isPhpLocale = false; let files = []; let exitHandlersBound = false; let jsonLocales = []; let phpLocales = []; function clean() { files.forEach((file) => fs_1.default.existsSync(file.path) && fs_1.default.unlinkSync(file.path)); files = []; } function pushKeys(keys, locales) { if (typeof process.env.VITE_LARAVEL_REACT_I18N_LOCALE !== 'undefined' && locales.includes(process.env.VITE_LARAVEL_REACT_I18N_LOCALE)) { const fileName = isPhpLocale ? `php_${process.env.VITE_LARAVEL_REACT_I18N_LOCALE}` : process.env.VITE_LARAVEL_REACT_I18N_LOCALE; keys.push((0, key_type_1.convertToKeyType)(langDirname, fileName)); } if (typeof process.env.VITE_LARAVEL_REACT_I18N_FALLBACK_LOCALE !== 'undefined' && locales.includes(process.env.VITE_LARAVEL_REACT_I18N_FALLBACK_LOCALE) && process.env.VITE_LARAVEL_REACT_I18N_LOCALE !== process.env.VITE_LARAVEL_REACT_I18N_FALLBACK_LOCALE) { const fileName = isPhpLocale ? `php_${process.env.VITE_LARAVEL_REACT_I18N_FALLBACK_LOCALE}` : process.env.VITE_LARAVEL_REACT_I18N_FALLBACK_LOCALE; keys.push((0, key_type_1.convertToKeyType)(langDirname, fileName)); } } return { name: 'i18n', enforce: 'post', config() { const keys = []; // Check language directory is exists. if (!fs_1.default.existsSync(langDirname)) { const msg = [ 'Language directory is not exist, maybe you did not publish the language files with `php artisan lang:publish`.', 'For more information please visit: https://laravel.com/docs/10.x/localization#publishing-the-language-files' ]; msg.map((str) => logger.error(str, { timestamp: true })); return; } // JSON-file locales. jsonLocales = locale_1.default.getJsonLocale(langDirname); if (config === null || config === void 0 ? void 0 : config.typeTranslationKeys) { pushKeys(keys, jsonLocales); } // PHP-file locales. phpLocales = locale_1.default.getPhpLocale(langDirname); if (phpLocales.length > 0) { files = (0, parser_1.default)(langDirname); isPhpLocale = true; if (config === null || config === void 0 ? void 0 : config.typeTranslationKeys) { pushKeys(keys, phpLocales); } } else { const msg = [ 'Language directory not contain php translations files.', 'For more information please visit: https://laravel.com/docs/10.x/localization#introduction' ]; msg.map((str) => logger.info(str, { timestamp: true })); } if (config === null || config === void 0 ? void 0 : config.typeTranslationKeys) { (0, key_type_1.saveKeyTypeToFile)(keys.join('|'), config === null || config === void 0 ? void 0 : config.typeDestinationPath); } }, buildEnd: clean, handleHotUpdate(ctx) { const keys = []; if (config === null || config === void 0 ? void 0 : config.typeTranslationKeys) { pushKeys(keys, jsonLocales); } if (isPhpLocale) { if (/lang\/.*\.php$/.test(ctx.file)) { files = (0, parser_1.default)(langDirname); } if (config === null || config === void 0 ? void 0 : config.typeTranslationKeys) { pushKeys(keys, phpLocales); } } if (config === null || config === void 0 ? void 0 : config.typeTranslationKeys) { (0, key_type_1.saveKeyTypeToFile)(keys.join('|'), config === null || config === void 0 ? void 0 : config.typeDestinationPath); } }, configureServer() { if (exitHandlersBound) return; process.on('exit', clean); process.on('SIGINT', process.exit); process.on('SIGTERM', process.exit); process.on('SIGHUP', process.exit); exitHandlersBound = true; } }; }