Commit 5eac1bf0 by Vladislav Lagunov

Рефакторинг ~/gettext

parent 1aec7d53
...@@ -10,6 +10,7 @@ const CONTEXT_DELIMITER = String.fromCharCode(4); ...@@ -10,6 +10,7 @@ const CONTEXT_DELIMITER = String.fromCharCode(4);
// Translations // Translations
export type Data = Record<string, Record<string, string[]|string>>; export type Data = Record<string, Record<string, string[]|string>>;
export type JedFormat = { locale_data: { messages: Record<string, string[]> } }; export type JedFormat = { locale_data: { messages: Record<string, string[]> } };
export type ES6Module<T> = { default: T };
// Хелпер для перевода // Хелпер для перевода
...@@ -22,7 +23,11 @@ export class Translations { ...@@ -22,7 +23,11 @@ export class Translations {
readonly data: Data readonly data: Data
) {} ) {}
static concat(...webpackContexts) { 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); const jedTranslations: JedFormat[] = flattenModules(webpackContexts);
return new Translations(assignJedData({}, ...jedTranslations)); return new Translations(assignJedData({}, ...jedTranslations));
...@@ -34,7 +39,7 @@ export class Translations { ...@@ -34,7 +39,7 @@ export class Translations {
} }
} }
__(singular_key: string, plural_key?: string, context?: string, n?: number): I18nString { __: (singular_key: string, plural_key?: string, context?: string, n?: number) => I18nString = (singular_key, plural_key?, context?, 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(/_/, '-');
...@@ -75,7 +80,7 @@ export type LocaleCtx = { locale: string }; ...@@ -75,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.concat(...webpackContexts); const translations: Translations = webpackContexts[0] instanceof Translations ? webpackContexts[0] : Translations.fromJed(...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, '__'>> {
......
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