Commit b4fc2890 by Vladislav Lagunov

[gettext] Испралена обработка формата po2json

parent 76e213e1
......@@ -8,8 +8,8 @@ const CONTEXT_DELIMITER = String.fromCharCode(4);
// Translations
export type Data = Record<string, Record<string, string[]|string>>;
export type JedFormat = { locale_data: { messages: Record<string, string[]> } };
export type TranslationsData = Record<string, Record<string, string[]|string>>;
export type POData = Record<string, string[]|string>;
export type ES6Module<T> = { default: T };
......@@ -20,16 +20,16 @@ export type I18nString = (locale?: string|LocaleCtx|null) => string;
export class Translations {
constructor(
readonly data: Data
readonly data: TranslationsData,
) {}
static fromJed(...data: JedFormat[]): Translations;
static fromJed(...data: ES6Module<JedFormat>[]): Translations;
static fromJed(...data: JedFormat[][]): Translations;
static fromJed(...data: ES6Module<JedFormat>[][]): Translations;
static fromJed(...webpackContexts) {
const jedTranslations: JedFormat[] = flattenModules(webpackContexts);
return new Translations(assignJedData({}, ...jedTranslations));
static fromPO(...data: POData[]): Translations;
static fromPO(...data: ES6Module<POData>[]): Translations;
static fromPO(...data: POData[][]): Translations;
static fromPO(...data: ES6Module<POData>[][]): Translations;
static fromPO(...webpackContexts) {
const podata: POData[] = flattenModules(webpackContexts);
return new Translations(assignData({}, ...podata));
function flattenModules(xs: any[]) {
return Array.prototype.concat.apply([], xs.map(x => {
......@@ -39,7 +39,7 @@ export class Translations {
}
}
__: (singular_key: string, plural_key?: string, context?: string, n?: number) => I18nString = (singular_key, plural_key?, context?, n?) => {
__: (singular_key: string, context?: string, plural_key?: string, n?: number) => I18nString = (singular_key, context?, plural_key?, n?) => {
return localeOrCtx => {
let locale = !localeOrCtx ? 'en-US' : typeof(localeOrCtx) === 'string' ? localeOrCtx : localeOrCtx.locale;
locale = locale.replace(/_/, '-');
......@@ -80,7 +80,7 @@ export type LocaleCtx = { locale: string };
* ```
*/
export function withGettext(...webpackContexts) {
const translations: Translations = webpackContexts[0] instanceof Translations ? webpackContexts[0] : Translations.fromJed(...webpackContexts);
const translations: Translations = webpackContexts[0] instanceof Translations ? webpackContexts[0] : Translations.fromPO(...webpackContexts);
return <P extends { __: Gettext, ctx?: { locale: string } }>(Component: React.ComponentType<P>) => {
class WithTranslations extends React.Component<Omit<P, '__'>> {
......@@ -104,27 +104,21 @@ export function withGettext(...webpackContexts) {
/**
* Добавление переводов из `srcs` в `dst`
*/
function assignJedData(dst: Data, ...srcs: JedFormat[]): Data {
function assignData(dst: TranslationsData, ...srcs: POData[]): TranslationsData {
srcs.forEach(data => {
// @ts-ignore
const locale_ = data[''].language || data[''].lang || data[''].locale; if (!locale_ || typeof(locale_) !== 'string') return;
const locale = locale_.replace(/_/g, '-');
const podata = data['%podata'];
for (const k of Object.keys(data)) {
if (k === '' || k === '%podata') continue;
if (k === '') continue;
dst[locale] = dst[locale] || {};
if (Array.isArray(data[k]) && data[k][0] === null) {
if (Array.isArray(data[k])) {
dst[locale][k] = data[k].slice(1);
if (dst[locale][k].length === 1) dst[locale][k] = dst[locale][k][0];
} else {
dst[locale][k] = data[k];
}
}
if (podata) for (const explicit of podata) {
const key = explicit.msgctxt ? explicit.msgctxt + CONTEXT_DELIMITER + explicit.msgid : explicit.msgid;
dst[locale] = dst[locale] || {};
dst[locale][key] = explicit.msgstr;
}
});
return dst;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment