You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Hydro/hydro/lib/i18n.js

41 lines
1.3 KiB
JavaScript

5 years ago
const fs = require('fs');
const yaml = require('js-yaml');
5 years ago
const locales = {};
5 years ago
String.prototype.format = function formatStr(...args) {
let result = this;
if (args.length > 0) {
if (args.length === 1 && typeof (args[0]) === 'object') {
for (const key in args) {
if (args[key] !== undefined) {
const reg = new RegExp(`(\\{${key}\\})`, 'g');
result = result.replace(reg, args[key]);
}
}
5 years ago
} else {
for (let i = 0; i < args.length; i++) {
if (args[i] !== undefined) {
const reg = new RegExp(`(\\{)${i}(\\})`, 'g');
result = result.replace(reg, args[i]);
}
}
}
}
return result;
};
5 years ago
String.prototype.rawformat = function rawFormat(object) {
const res = this.split('{@}');
return [res[0], object, res[1]];
};
5 years ago
String.prototype.translate = function translate(language = 'zh_CN') {
if (locales[language]) return locales[language][this] || this;
5 years ago
return this;
};
module.exports = function load(file, language) {
if (!locales[language]) locales[language] = {};
5 years ago
const content = fs.readFileSync(file).toString();
Object.assign(locales[language], yaml.safeLoad(content));
5 years ago
};