From aaf950e2ad5c07d4423f9822e3a0ae9f7b8d2bdf Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Mon, 30 Mar 2020 16:09:32 +0530 Subject: re-format with prettier v2, fix HTML --- src/webex/i18n.tsx | 75 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 26 deletions(-) (limited to 'src/webex/i18n.tsx') diff --git a/src/webex/i18n.tsx b/src/webex/i18n.tsx index 3923654e7..2e753e441 100644 --- a/src/webex/i18n.tsx +++ b/src/webex/i18n.tsx @@ -21,19 +21,17 @@ /** * Imports. */ -import {strings} from "../i18n/strings"; +import { strings } from "../i18n/strings"; // @ts-ignore: no type decl for this library import * as jedLib from "jed"; import * as React from "react"; - const jed = setupJed(); let enableTracing = false; - /** * Set up jed library for internationalization, * based on browser language settings. @@ -56,7 +54,6 @@ function setupJed(): any { return new jedLib.Jed(strings[lang]); } - /** * Convert template strings to a msgid */ @@ -71,22 +68,22 @@ function toI18nString(stringSeq: ReadonlyArray) { return s; } - /** * Internationalize a string template with arbitrary serialized values. */ export function str(stringSeq: TemplateStringsArray, ...values: any[]) { const s = toI18nString(stringSeq); - const tr = jed.translate(s).ifPlural(1, s).fetch(...values); + const tr = jed + .translate(s) + .ifPlural(1, s) + .fetch(...values); return tr; } - interface TranslateSwitchProps { target: number; } - function stringifyChildren(children: any): string { let n = 1; const ss = React.Children.map(children, (c) => { @@ -100,7 +97,6 @@ function stringifyChildren(children: any): string { return s; } - interface TranslateProps { /** * Component that the translated element should be wrapped in. @@ -114,7 +110,6 @@ interface TranslateProps { wrapProps?: any; } - /** * Translate text node children of this component. * If a child component might produce a text node, it must be wrapped @@ -130,11 +125,19 @@ interface TranslateProps { export class Translate extends React.Component { render(): JSX.Element { const s = stringifyChildren(this.props.children); - const tr = jed.ngettext(s, s, 1).split(/%(\d+)\$s/).filter((e: any, i: number) => i % 2 === 0); + const tr = jed + .ngettext(s, s, 1) + .split(/%(\d+)\$s/) + .filter((e: any, i: number) => i % 2 === 0); const childArray = React.Children.toArray(this.props.children!); for (let i = 0; i < childArray.length - 1; ++i) { - if ((typeof childArray[i]) === "string" && (typeof childArray[i + 1]) === "string") { - childArray[i + 1] = (childArray[i] as string).concat(childArray[i + 1] as string); + if ( + typeof childArray[i] === "string" && + typeof childArray[i + 1] === "string" + ) { + childArray[i + 1] = (childArray[i] as string).concat( + childArray[i + 1] as string, + ); childArray.splice(i, 1); } } @@ -158,7 +161,6 @@ export class Translate extends React.Component { } } - /** * Switch translation based on singular or plural based on the target prop. * Should only contain TranslateSingular and TransplatePlural as children. @@ -171,7 +173,10 @@ export class Translate extends React.Component { * * ``` */ -export class TranslateSwitch extends React.Component { +export class TranslateSwitch extends React.Component< + TranslateSwitchProps, + void +> { render(): JSX.Element { let singular: React.ReactElement | undefined; let plural: React.ReactElement | undefined; @@ -186,7 +191,7 @@ export class TranslateSwitch extends React.Component } }); } - if ((!singular) || (!plural)) { + if (!singular || !plural) { console.error("translation not found"); return React.createElement("span", {}, ["translation not found"]); } @@ -198,7 +203,6 @@ export class TranslateSwitch extends React.Component } } - interface TranslationPluralProps { target: number; } @@ -206,14 +210,24 @@ interface TranslationPluralProps { /** * See [[TranslateSwitch]]. */ -export class TranslatePlural extends React.Component { +export class TranslatePlural extends React.Component< + TranslationPluralProps, + void +> { render(): JSX.Element { const s = stringifyChildren(this.props.children); - const tr = jed.ngettext(s, s, 1).split(/%(\d+)\$s/).filter((e: any, i: number) => i % 2 === 0); + const tr = jed + .ngettext(s, s, 1) + .split(/%(\d+)\$s/) + .filter((e: any, i: number) => i % 2 === 0); const childArray = React.Children.toArray(this.props.children!); for (let i = 0; i < childArray.length - 1; ++i) { - if ((typeof childArray[i]) === "string" && (typeof childArray[i + 1]) === "string") { - childArray[i + i] = childArray[i] as string + childArray[i + 1] as string; + if ( + typeof childArray[i] === "string" && + typeof childArray[i + 1] === "string" + ) { + childArray[i + i] = ((childArray[i] as string) + + childArray[i + 1]) as string; childArray.splice(i, 1); } } @@ -234,18 +248,27 @@ export class TranslatePlural extends React.Component { +export class TranslateSingular extends React.Component< + TranslationPluralProps, + void +> { render(): JSX.Element { const s = stringifyChildren(this.props.children); - const tr = jed.ngettext(s, s, 1).split(/%(\d+)\$s/).filter((e: any, i: number) => i % 2 === 0); + const tr = jed + .ngettext(s, s, 1) + .split(/%(\d+)\$s/) + .filter((e: any, i: number) => i % 2 === 0); const childArray = React.Children.toArray(this.props.children!); for (let i = 0; i < childArray.length - 1; ++i) { - if ((typeof childArray[i]) === "string" && (typeof childArray[i + 1]) === "string") { - childArray[i + i] = childArray[i] as string + childArray[i + 1] as string; + if ( + typeof childArray[i] === "string" && + typeof childArray[i + 1] === "string" + ) { + childArray[i + i] = ((childArray[i] as string) + + childArray[i + 1]) as string; childArray.splice(i, 1); } } -- cgit v1.2.3