UNPKG

@ptkdev/node-cli-boilerplate

Version:

Create node cli with this user friendly boilerplate. Use this respository as template for your new npm command line interface project

68 lines 2.14 kB
/** * Translate * ===================== * * @contributors: Patryk Rzucidło [@ptkdev] <support@ptkdev.io> (https://ptk.dev) * * @license: MIT License * */ import configs from "../configs/config"; import translations from "../routes/translations"; /** * Replace Params * ===================== * If translation text is: hi {name} how are you? * This function replace {name} token with correct value * * @param {string} text - text of current phrase to translate (mandatory) * @param {Object} language_params - object with token to replace, example: {name:"alex"} (mandatory) * * @return {string} text - text with replaced token * */ const replaceParams = (text, language_params) => { for (const [key, value] of Object.entries(language_params)) { text = text.replace(`{{${key}}}`, value); } return text; }; /** * Check * ===================== * Check if exist translation in selected language, if not exist get string of phrase from "en" translation * * @param {string} lang - language from group/user (mandatory) * @param {string} language_id - key of translation phrase from /translations/*.json (mandatory) * * @return {string} text - text of available translation * */ const check = (lang, language_id) => { return (translations?.[lang]?.[language_id] ?? translations?.["en"]?.[language_id] ?? `translation id: ${language_id} in ${lang}.json is undefined`); }; /** * Translate * ===================== * Get correct translation * * @param {string} lang - language from group/user (mandatory) * @param {string} language_id - key of translation phrase from /translations/*.json (mandatory) * @param {Object} language_params - object with token to replace, example: {name:"alex"} (optional) * * @return {string} text - text of available translation * */ const translate = (language_id, language_params) => { let text = ""; text = check(configs.language, language_id); if (language_params) { text = replaceParams(text, language_params); } return text; }; export { translate }; export default translate; //# sourceMappingURL=translate.js.map