web: Replace lingui.js with lit-localize (#5761)

* \#\# Details

web: replace lingui with lit/localize

\#\# Changes

This rather massive shift replaces the lingui and `t()` syntax with lit-localize, XLIFF, and the `msg()`
syntax used by lit-localize.  90% of this work was mechanized; simple perl scripts found and replaced
all uses of `t()` with the appropriate corresponding syntax for `msg()` and `msg(str())`.

The XLIFF files were auto-generated from the PO files.  They have not been audited, and they should be
checked over by professional translators.  The actual _strings_ have not been changed, but as this was
a mechanized change there is always the possibility of mis-translation-- not by the translator, but by
the script.

* web: revise lit/localize: fix two installation issues.

* web: revise localization

TL;DR:

- Replaced all of Lingui's `t()` syntax with `msg()` syntax.
- Mechanically (i.e with a script) converted all of the PO files to XLIFF files
- Refactored the localization code to be a bit smarter:
  - the function `getBestMatchLocale` takes the locale lists and a requested locale, and returns the
    first match of:
    - The locale's code exactly matches the requested locale
    - The locale code exactly matches the prefix of the requested locale (i.e the "en" part of "en-US")
    - the locale code's prefix exactly matches the prefix of the requested locale
    This function is passed to lit-locate's `loadLocale()`.
  - `activateLocale()` just calls `loadLocale()` now.
  - `autodetectLanguage` searches the following, and picks the first that returns a valid locale
    object, before passing it to `loadLocale()`:
    - The User's settings
    - A `?locale=` component found in `window.location.search`
    - The `window.navigator.language` field
    - English

The `msg()` only runs when it's run.  This seems obvious, but it means that you cannot cache
strings at load time; they must be kept inside functions that are re-run so that the `msg()` engine
can look up the strings in the preferred language of the user at that moment.

You can use thunks-of-strings if you really need them that way.

* Including the 'xliff-converter' in case anyone wants to review it.

* The xliff-converter is tagged as 'xliff-converter', but has been
deleted.

\#\# Details

-   Resolves #5171

\#\# Changes

\#\#\# New Features

-   Adds a "Add an Application" to the LibraryView if there are no applications and the user is an administrator.

\#\#\# Breaking Changes

-   Adds breaking change which causes \<issue\>.

\#\# Checklist

-   [ ] Local tests pass (`ak test authentik/`)
-   [ ] The code has been formatted (`make lint-fix`)

If an API change has been made

-   [ ] The API schema has been updated (`make gen-build`)

If changes to the frontend have been made

-   [ ] The code has been formatted (`make web`)
-   [ ] The translation files have been updated (`make i18n-extract`)

If applicable

-   [ ] The documentation has been updated
-   [ ] The documentation has been formatted (`make website`)

* web: fix redundant locales for zh suite.

* web: prettier pass for locale update

* web: localization moderization

Changed the names of the lit-localize commands to make it clear they're
part of the localization effort, and not just "build" and "extract".

* update transifex config

Signed-off-by: Jens Langhammer <jens@goauthentik.io>

* fix package lock?

Signed-off-by: Jens Langhammer <jens@goauthentik.io>

* use build not compile

Signed-off-by: Jens Langhammer <jens@goauthentik.io>

* web: conversion to lit-localize

The CI produced a list of problems that I hadn't caught earlier,
due to a typo ("localize build" is correct, "localize compile" is
not) I had left in package.json.  They were minor and linty, but
it was still wise to fix them.

* web: replace lingui with lit/locale

This commit fixes some minor linting issues that were hidden by a typo in package.json.  The
issues were not apparently problematic from a Javascript point of view, but they pointed
to sloppy thinking in the progression of types through the system, so I cleaned them
up and formalized the types from LocaleModule to AkLocale.

* web: replace lingui with lit/localize

One problem that has repeatedly come up is that localize's templates do not produce
JavaScript that conforms with our shop style.  I've replaced `build-locale` with
a two-step that builds the locale *and* ensures that it conforms to the shop style
via `prettier` every time.

* web: replace lingui with lit-locale

This commit applies the most recent bundle of translations to the
new lit-locale aspect component.  It also revises the algorithm
for *finding* the correct locale, replacing the complex fall-back
with some rather straightforward regular expressions.

In the case of Chinese, the fallback comes at the end of the
selection list, which may not be, er, politically valuable
(since Taiwan and Hong Kong come before, being exceptions that
need to be tested).  If we need a different order for presentation,
that'll be a future feature.

* web: replace lingui with lit/locale

Well, that was embarassing.

---------

Signed-off-by: Jens Langhammer <jens@goauthentik.io>
Co-authored-by: Jens Langhammer <jens@goauthentik.io>
This commit is contained in:
Ken Sternberg
2023-06-02 08:08:36 -07:00
committed by GitHub
parent afa8a505ee
commit 44a057ed9c
273 changed files with 64104 additions and 117217 deletions

View File

@ -4,8 +4,7 @@ import { ModalButton } from "@goauthentik/elements/buttons/ModalButton";
import "@goauthentik/elements/buttons/SpinnerButton";
import { showMessage } from "@goauthentik/elements/messages/MessageContainer";
import { t } from "@lingui/macro";
import { msg, str } from "@lit/localize";
import { TemplateResult, html } from "lit";
import { customElement, property } from "lit/decorators.js";
@ -49,7 +48,7 @@ export class ConfirmationForm extends ModalButton {
onError(e: Error): void {
showMessage({
message: t`${this.errorMessage}: ${e.toString()}`,
message: msg(str`${this.errorMessage}: ${e.toString()}`),
level: MessageLevel.error,
});
}
@ -82,7 +81,7 @@ export class ConfirmationForm extends ModalButton {
}}
class="pf-m-secondary"
>
${t`Cancel`}
${msg("Cancel")}
</ak-spinner-button>
</footer>`;
}

View File

@ -7,8 +7,7 @@ import { showMessage } from "@goauthentik/elements/messages/MessageContainer";
import { PaginatedResponse } from "@goauthentik/elements/table/Table";
import { Table, TableColumn } from "@goauthentik/elements/table/Table";
import { t } from "@lingui/macro";
import { msg, str } from "@lit/localize";
import { CSSResult, TemplateResult, html } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { until } from "lit/directives/until.js";
@ -87,26 +86,26 @@ export class DeleteObjectsTable<T> extends Table<T> {
renderUsedBy(usedBy: UsedBy[]): TemplateResult {
if (usedBy.length < 1) {
return html`<span>${t`Not used by any other object.`}</span>`;
return html`<span>${msg("Not used by any other object.")}</span>`;
}
return html`<ul class="pf-c-list">
${usedBy.map((ub) => {
let consequence = "";
switch (ub.action) {
case UsedByActionEnum.Cascade:
consequence = t`object will be DELETED`;
consequence = msg("object will be DELETED");
break;
case UsedByActionEnum.CascadeMany:
consequence = t`connection will be deleted`;
consequence = msg("connection will be deleted");
break;
case UsedByActionEnum.SetDefault:
consequence = t`reference will be reset to default value`;
consequence = msg("reference will be reset to default value");
break;
case UsedByActionEnum.SetNull:
consequence = t`reference will be set to an empty value`;
consequence = msg("reference will be set to an empty value");
break;
}
return html`<li>${t`${ub.name} (${consequence})`}</li>`;
return html`<li>${msg(str`${ub.name} (${consequence})`)}</li>`;
})}
</ul>`;
}
@ -131,10 +130,10 @@ export class DeleteBulkForm<T> extends ModalButton {
const rec = item as Record<string, unknown>;
const meta = [];
if (Object.prototype.hasOwnProperty.call(rec, "name")) {
meta.push({ key: t`Name`, value: rec.name as string });
meta.push({ key: msg("Name"), value: rec.name as string });
}
if (Object.prototype.hasOwnProperty.call(rec, "pk")) {
meta.push({ key: t`ID`, value: rec.pk as string });
meta.push({ key: msg("ID"), value: rec.pk as string });
}
return meta;
};
@ -168,14 +167,14 @@ export class DeleteBulkForm<T> extends ModalButton {
onSuccess(): void {
showMessage({
message: t`Successfully deleted ${this.objects.length} ${this.objectLabel}`,
message: msg(str`Successfully deleted ${this.objects.length} ${this.objectLabel}`),
level: MessageLevel.success,
});
}
onError(e: Error): void {
showMessage({
message: t`Failed to delete ${this.objectLabel}: ${e.toString()}`,
message: msg(str`Failed to delete ${this.objectLabel}: ${e.toString()}`),
level: MessageLevel.error,
});
}
@ -184,7 +183,9 @@ export class DeleteBulkForm<T> extends ModalButton {
return html`<section class="pf-c-modal-box__header pf-c-page__main-section pf-m-light">
<div class="pf-c-content">
<h1 class="pf-c-title pf-m-2xl">
${this.actionLabel ? this.actionLabel : t`Delete ${this.objectLabel}`}
${this.actionLabel
? this.actionLabel
: msg(str`Delete ${this.objectLabel}`)}
</h1>
</div>
</section>
@ -193,7 +194,9 @@ export class DeleteBulkForm<T> extends ModalButton {
<p class="pf-c-title">
${this.actionSubtext
? this.actionSubtext
: t`Are you sure you want to delete ${this.objects.length} ${this.objectLabel}?`}
: msg(
str`Are you sure you want to delete ${this.objects.length} ${this.objectLabel}?`,
)}
</p>
<slot name="notice"></slot>
</form>
@ -213,7 +216,7 @@ export class DeleteBulkForm<T> extends ModalButton {
}}
class="pf-m-danger"
>
${t`Delete`} </ak-spinner-button
${msg("Delete")} </ak-spinner-button
>&nbsp;
<ak-spinner-button
.callAction=${async () => {
@ -221,7 +224,7 @@ export class DeleteBulkForm<T> extends ModalButton {
}}
class="pf-m-secondary"
>
${t`Cancel`}
${msg("Cancel")}
</ak-spinner-button>
</footer>`;
}

View File

@ -4,8 +4,7 @@ import { ModalButton } from "@goauthentik/elements/buttons/ModalButton";
import "@goauthentik/elements/buttons/SpinnerButton";
import { showMessage } from "@goauthentik/elements/messages/MessageContainer";
import { t } from "@lingui/macro";
import { msg, str } from "@lit/localize";
import { CSSResult, TemplateResult, html } from "lit";
import { customElement, property } from "lit/decorators.js";
import { until } from "lit/directives/until.js";
@ -52,14 +51,14 @@ export class DeleteForm extends ModalButton {
onSuccess(): void {
showMessage({
message: t`Successfully deleted ${this.objectLabel} ${this.obj?.name}`,
message: msg(str`Successfully deleted ${this.objectLabel} ${this.obj?.name}`),
level: MessageLevel.success,
});
}
onError(e: Error): void {
showMessage({
message: t`Failed to delete ${this.objectLabel}: ${e.toString()}`,
message: msg(str`Failed to delete ${this.objectLabel}: ${e.toString()}`),
level: MessageLevel.error,
});
}
@ -73,12 +72,14 @@ export class DeleteForm extends ModalButton {
}
return html`<section class="pf-c-modal-box__header pf-c-page__main-section pf-m-light">
<div class="pf-c-content">
<h1 class="pf-c-title pf-m-2xl">${t`Delete ${this.objectLabel}`}</h1>
<h1 class="pf-c-title pf-m-2xl">${msg(str`Delete ${this.objectLabel}`)}</h1>
</div>
</section>
<section class="pf-c-modal-box__body pf-m-light">
<form class="pf-c-form pf-m-horizontal">
<p>${t`Are you sure you want to delete ${this.objectLabel} ${objName} ?`}</p>
<p>
${msg(str`Are you sure you want to delete ${this.objectLabel} ${objName}?`)}
</p>
</form>
</section>
${this.usedBy
@ -90,26 +91,32 @@ export class DeleteForm extends ModalButton {
return html`
<section class="pf-c-modal-box__body pf-m-light">
<form class="pf-c-form pf-m-horizontal">
<p>${t`The following objects use ${objName} `}</p>
<p>${msg(str`The following objects use ${objName}`)}</p>
<ul class="pf-c-list">
${usedBy.map((ub) => {
let consequence = "";
switch (ub.action) {
case UsedByActionEnum.Cascade:
consequence = t`object will be DELETED`;
consequence = msg("object will be DELETED");
break;
case UsedByActionEnum.CascadeMany:
consequence = t`connecting object will be deleted`;
consequence = msg(
"connecting object will be deleted",
);
break;
case UsedByActionEnum.SetDefault:
consequence = t`reference will be reset to default value`;
consequence = msg(
"reference will be reset to default value",
);
break;
case UsedByActionEnum.SetNull:
consequence = t`reference will be set to an empty value`;
consequence = msg(
"reference will be set to an empty value",
);
break;
}
return html`<li>
${t`${ub.name} (${consequence})`}
${msg(str`${ub.name} (${consequence})`)}
</li>`;
})}
</ul>
@ -126,7 +133,7 @@ export class DeleteForm extends ModalButton {
}}
class="pf-m-danger"
>
${t`Delete`} </ak-spinner-button
${msg("Delete")} </ak-spinner-button
>&nbsp;
<ak-spinner-button
.callAction=${async () => {
@ -134,7 +141,7 @@ export class DeleteForm extends ModalButton {
}}
class="pf-m-secondary"
>
${t`Cancel`}
${msg("Cancel")}
</ak-spinner-button>
</footer>`;
}

View File

@ -2,8 +2,7 @@ import { convertToSlug } from "@goauthentik/common/utils";
import { AKElement } from "@goauthentik/elements/Base";
import { FormGroup } from "@goauthentik/elements/forms/FormGroup";
import { t } from "@lingui/macro";
import { msg } from "@lit/localize";
import { CSSResult, css } from "lit";
import { TemplateResult, html } from "lit";
import { customElement, property } from "lit/decorators.js";
@ -137,7 +136,7 @@ export class HorizontalFormElement extends AKElement {
<div class="pf-c-form__horizontal-group">
${this.writeOnly
? html`<p class="pf-c-form__helper-text" aria-live="polite">
${t`Click to change value`}
${msg("Click to change value")}
</p>`
: html``}
${this.errorMessages.map((message) => {

View File

@ -4,8 +4,7 @@ import { ModalButton } from "@goauthentik/elements/buttons/ModalButton";
import "@goauthentik/elements/buttons/SpinnerButton";
import { Form } from "@goauthentik/elements/forms/Form";
import { t } from "@lingui/macro";
import { msg } from "@lit/localize";
import { TemplateResult, html } from "lit";
import { customElement, property } from "lit/decorators.js";
@ -21,16 +20,16 @@ export class ModalForm extends ModalButton {
loading = false;
@property({ type: String })
cancelText = t`Cancel`;
cancelText = msg("Cancel");
async confirm(): Promise<void> {
const form = this.querySelector<Form<unknown>>("[slot=form]");
if (!form) {
return Promise.reject(t`No form found`);
return Promise.reject(msg("No form found"));
}
const formPromise = form.submit(new Event("submit"));
if (!formPromise) {
return Promise.reject(t`Form didn't return a promise for submitting`);
return Promise.reject(msg("Form didn't return a promise for submitting"));
}
return formPromise
.then(() => {

View File

@ -3,8 +3,7 @@ import { ascii_letters, digits, groupBy, randomString } from "@goauthentik/commo
import { AKElement } from "@goauthentik/elements/Base";
import { PreventFormSubmit } from "@goauthentik/elements/forms/Form";
import { t } from "@lingui/macro";
import { msg } from "@lit/localize";
import { CSSResult, TemplateResult, html, render } from "lit";
import { customElement, property } from "lit/decorators.js";
@ -35,7 +34,7 @@ export class SearchSelect<T> extends AKElement {
blankable = false;
@property()
placeholder: string = t`Select an object.`;
placeholder: string = msg("Select an object.");
static get styles(): CSSResult[] {
return [PFBase, PFForm, PFFormControl, PFSelect];
@ -94,7 +93,7 @@ export class SearchSelect<T> extends AKElement {
toForm(): unknown {
if (!this.objects) {
throw new PreventFormSubmit(t`Loading options...`);
throw new PreventFormSubmit(msg("Loading options..."));
}
return this.value(this.selectedObject) || "";
}
@ -258,7 +257,7 @@ export class SearchSelect<T> extends AKElement {
this.renderMenu();
let value = "";
if (!this.objects) {
value = t`Loading...`;
value = msg("Loading...");
} else if (this.selectedObject) {
value = this.renderElement(this.selectedObject);
} else if (this.blankable) {