@grammyjs/i18n
Version:
Internationalization plugin for grammY based on Fluent.
96 lines (95 loc) • 4.03 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.readLocalesDir = readLocalesDir;
exports.readLocalesDirSync = readLocalesDirSync;
const dntShim = __importStar(require("../_dnt.shims.js"));
const deps_js_1 = require("./deps.js");
function throwReadFileError(path) {
throw new Error(`Something went wrong while reading the "${path}" file, usually, this can be caused by the file being empty. \
If it is, please add at least one translation key to this file (or simply just delete it) to solve this error.`);
}
async function readLocalesDir(path) {
const files = new Array();
const locales = new Set();
for await (const entry of (0, deps_js_1.walk)(path)) {
if (entry.isFile && (0, deps_js_1.extname)(entry.name) === ".ftl") {
try {
const decoder = new TextDecoder("utf-8");
const excludeRoot = entry.path.replace(path, "");
const contents = await dntShim.Deno.readFile((0, deps_js_1.join)(path, excludeRoot));
const belongsTo = excludeRoot.split(deps_js_1.SEP)[1].split(".")[0];
const translationSource = decoder.decode(contents);
files.push({
belongsTo,
translationSource,
});
locales.add(belongsTo);
}
catch {
throwReadFileError(entry.path);
}
}
}
return Array.from(locales).map((locale) => {
const sameLocale = files.filter((file) => file.belongsTo === locale);
const sourceOnly = sameLocale.map((file) => file.translationSource);
return {
belongsTo: locale,
translationSource: sourceOnly.join("\n"),
};
});
}
function readLocalesDirSync(path) {
const files = new Array();
const locales = new Set();
for (const entry of (0, deps_js_1.walkSync)(path)) {
if (entry.isFile && (0, deps_js_1.extname)(entry.name) === ".ftl") {
try {
const decoder = new TextDecoder("utf-8");
const excludeRoot = entry.path.replace(path, "");
const contents = dntShim.Deno.readFileSync((0, deps_js_1.join)(path, excludeRoot));
const belongsTo = excludeRoot.split(deps_js_1.SEP)[1].split(".")[0];
const translationSource = decoder.decode(contents);
files.push({
belongsTo,
translationSource,
});
locales.add(belongsTo);
}
catch {
throwReadFileError(entry.path);
}
}
}
return Array.from(locales).map((locale) => {
const sameLocale = files.filter((file) => file.belongsTo === locale);
const sourceOnly = sameLocale.map((file) => file.translationSource);
return {
belongsTo: locale,
translationSource: sourceOnly.join("\n"),
};
});
}