// Copyright (c) 2015, JINGROW and Contributors
// MIT License. See license.txt

// for translation
jingrow._ = function (txt, replace, context = null) {
	if (!txt) return txt;
	if (typeof txt != "string") return txt;

	let translated_text = "";

	let key = txt; // txt.replace(/\n/g, "");
	if (context) {
		translated_text = jingrow._messages[`${key}:${context}`];
	}

	if (!translated_text) {
		translated_text = jingrow._messages[key] || txt;
	}

	if (replace && typeof replace === "object") {
		translated_text = $.format(translated_text, replace);
	}
	return translated_text;
};

window.__ = jingrow._;

jingrow.get_languages = function () {
	if (!jingrow.languages) {
		jingrow.languages = [];
		$.each(jingrow.boot.lang_dict, function (lang, value) {
			jingrow.languages.push({ label: lang, value: value });
		});
		jingrow.languages = jingrow.languages.sort(function (a, b) {
			return a.value < b.value ? -1 : 1;
		});
	}
	return jingrow.languages;
};
