Commit b4fc2890 by Vladislav Lagunov

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

parent 76e213e1
...@@ -8,8 +8,8 @@ const CONTEXT_DELIMITER = String.fromCharCode(4); ...@@ -8,8 +8,8 @@ const CONTEXT_DELIMITER = String.fromCharCode(4);
// Translations // Translations
export type Data = Record<string, Record<string, string[]|string>>; export type TranslationsData = Record<string, Record<string, string[]|string>>;
export type JedFormat = { locale_data: { messages: Record<string, string[]> } }; export type POData = Record<string, string[]|string>;
export type ES6Module<T> = { default: T }; export type ES6Module<T> = { default: T };
...@@ -20,16 +20,16 @@ export type I18nString = (locale?: string|LocaleCtx|null) => string; ...@@ -20,16 +20,16 @@ export type I18nString = (locale?: string|LocaleCtx|null) => string;
export class Translations { export class Translations {
constructor( constructor(
readonly data: Data readonly data: TranslationsData,
) {} ) {}
static fromJed(...data: JedFormat[]): Translations; static fromPO(...data: POData[]): Translations;
static fromJed(...data: ES6Module<JedFormat>[]): Translations; static fromPO(...data: ES6Module<POData>[]): Translations;
static fromJed(...data: JedFormat[][]): Translations; static fromPO(...data: POData[][]): Translations;
static fromJed(...data: ES6Module<JedFormat>[][]): Translations; static fromPO(...data: ES6Module<POData>[][]): Translations;
static fromJed(...webpackContexts) { static fromPO(...webpackContexts) {
const jedTranslations: JedFormat[] = flattenModules(webpackContexts); const podata: POData[] = flattenModules(webpackContexts);
return new Translations(assignJedData({}, ...jedTranslations)); return new Translations(assignData({}, ...podata));
function flattenModules(xs: any[]) { function flattenModules(xs: any[]) {
return Array.prototype.concat.apply([], xs.map(x => { return Array.prototype.concat.apply([], xs.map(x => {
...@@ -39,7 +39,7 @@ export class Translations { ...@@ -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 => { return localeOrCtx => {
let locale = !localeOrCtx ? 'en-US' : typeof(localeOrCtx) === 'string' ? localeOrCtx : localeOrCtx.locale; let locale = !localeOrCtx ? 'en-US' : typeof(localeOrCtx) === 'string' ? localeOrCtx : localeOrCtx.locale;
locale = locale.replace(/_/, '-'); locale = locale.replace(/_/, '-');
...@@ -80,7 +80,7 @@ export type LocaleCtx = { locale: string }; ...@@ -80,7 +80,7 @@ export type LocaleCtx = { locale: string };
* ``` * ```
*/ */
export function withGettext(...webpackContexts) { 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>) => { return <P extends { __: Gettext, ctx?: { locale: string } }>(Component: React.ComponentType<P>) => {
class WithTranslations extends React.Component<Omit<P, '__'>> { class WithTranslations extends React.Component<Omit<P, '__'>> {
...@@ -104,27 +104,21 @@ export function withGettext(...webpackContexts) { ...@@ -104,27 +104,21 @@ export function withGettext(...webpackContexts) {
/** /**
* Добавление переводов из `srcs` в `dst` * Добавление переводов из `srcs` в `dst`
*/ */
function assignJedData(dst: Data, ...srcs: JedFormat[]): Data { function assignData(dst: TranslationsData, ...srcs: POData[]): TranslationsData {
srcs.forEach(data => { srcs.forEach(data => {
// @ts-ignore
const locale_ = data[''].language || data[''].lang || data[''].locale; if (!locale_ || typeof(locale_) !== 'string') return; const locale_ = data[''].language || data[''].lang || data[''].locale; if (!locale_ || typeof(locale_) !== 'string') return;
const locale = locale_.replace(/_/g, '-'); const locale = locale_.replace(/_/g, '-');
const podata = data['%podata'];
for (const k of Object.keys(data)) { for (const k of Object.keys(data)) {
if (k === '' || k === '%podata') continue; if (k === '') continue;
dst[locale] = dst[locale] || {}; 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); dst[locale][k] = data[k].slice(1);
if (dst[locale][k].length === 1) dst[locale][k] = dst[locale][k][0]; if (dst[locale][k].length === 1) dst[locale][k] = dst[locale][k][0];
} else { } else {
dst[locale][k] = data[k]; 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; 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