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

@ -6,8 +6,7 @@ import { ModelForm } from "@goauthentik/elements/forms/ModelForm";
import "@goauthentik/elements/forms/Radio";
import "@goauthentik/elements/forms/SearchSelect";
import { t } from "@lingui/macro";
import { msg } from "@lit/localize";
import { TemplateResult, html } from "lit";
import { customElement } from "lit/decorators.js";
@ -23,9 +22,9 @@ export class TokenForm extends ModelForm<Token, string> {
getSuccessMessage(): string {
if (this.instance) {
return t`Successfully updated token.`;
return msg("Successfully updated token.");
} else {
return t`Successfully created token.`;
return msg("Successfully created token.");
}
}
@ -44,7 +43,11 @@ export class TokenForm extends ModelForm<Token, string> {
renderForm(): TemplateResult {
return html`<form class="pf-c-form pf-m-horizontal">
<ak-form-element-horizontal label=${t`Identifier`} name="identifier" ?required=${true}>
<ak-form-element-horizontal
label=${msg("Identifier")}
name="identifier"
?required=${true}
>
<input
type="text"
value="${first(this.instance?.identifier, "")}"
@ -52,10 +55,10 @@ export class TokenForm extends ModelForm<Token, string> {
required
/>
<p class="pf-c-form__helper-text">
${t`Unique identifier the token is referenced by.`}
${msg("Unique identifier the token is referenced by.")}
</p>
</ak-form-element-horizontal>
<ak-form-element-horizontal label=${t`User`} ?required=${true} name="user">
<ak-form-element-horizontal label=${msg("User")} ?required=${true} name="user">
<ak-search-select
.fetchObjects=${async (query?: string): Promise<User[]> => {
const args: CoreUsersListRequest = {
@ -82,26 +85,26 @@ export class TokenForm extends ModelForm<Token, string> {
>
</ak-search-select>
</ak-form-element-horizontal>
<ak-form-element-horizontal label=${t`Intent`} ?required=${true} name="intent">
<ak-form-element-horizontal label=${msg("Intent")} ?required=${true} name="intent">
<ak-radio
.options=${[
{
label: t`API Token`,
label: msg("API Token"),
value: IntentEnum.Api,
default: true,
description: html`${t`Used to access the API programmatically`}`,
description: html`${msg("Used to access the API programmatically")}`,
},
{
label: t`App password.`,
label: msg("App password."),
value: IntentEnum.AppPassword,
description: html`${t`Used to login using a flow executor`}`,
description: html`${msg("Used to login using a flow executor")}`,
},
]}
.value=${this.instance?.intent}
>
</ak-radio>
</ak-form-element-horizontal>
<ak-form-element-horizontal label=${t`Description`} name="description">
<ak-form-element-horizontal label=${msg("Description")} name="description">
<input
type="text"
value="${first(this.instance?.description, "")}"
@ -120,13 +123,15 @@ export class TokenForm extends ModelForm<Token, string> {
<i class="fas fa-check" aria-hidden="true"></i>
</span>
</span>
<span class="pf-c-switch__label">${t`Expiring`}</span>
<span class="pf-c-switch__label">${msg("Expiring")}</span>
</label>
<p class="pf-c-form__helper-text">
${t`If this is selected, the token will expire. Upon expiration, the token will be rotated.`}
${msg(
"If this is selected, the token will expire. Upon expiration, the token will be rotated.",
)}
</p>
</ak-form-element-horizontal>
<ak-form-element-horizontal label=${t`Expires on`} name="expires">
<ak-form-element-horizontal label=${msg("Expires on")} name="expires">
<input
type="datetime-local"
data-type="datetime-local"

View File

@ -10,8 +10,7 @@ import { PaginatedResponse } from "@goauthentik/elements/table/Table";
import { TableColumn } from "@goauthentik/elements/table/Table";
import { TablePage } from "@goauthentik/elements/table/TablePage";
import { t } from "@lingui/macro";
import { msg } from "@lit/localize";
import { TemplateResult, html } from "lit";
import { customElement, property } from "lit/decorators.js";
@ -20,15 +19,15 @@ import { CoreApi, IntentEnum, Token } from "@goauthentik/api";
export function IntentToLabel(intent: IntentEnum): string {
switch (intent) {
case IntentEnum.Api:
return t`API Access`;
return msg("API Access");
case IntentEnum.AppPassword:
return t`App password`;
return msg("App password");
case IntentEnum.Recovery:
return t`Recovery`;
return msg("Recovery");
case IntentEnum.Verification:
return t`Verification`;
return msg("Verification");
case IntentEnum.UnknownDefaultOpenApi:
return t`Unknown intent`;
return msg("Unknown intent");
}
}
@ -38,10 +37,12 @@ export class TokenListPage extends TablePage<Token> {
return true;
}
pageTitle(): string {
return t`Tokens`;
return msg("Tokens");
}
pageDescription(): string {
return t`Tokens are used throughout authentik for Email validation stages, Recovery keys and API access.`;
return msg(
"Tokens are used throughout authentik for Email validation stages, Recovery keys and API access.",
);
}
pageIcon(): string {
return "pf-icon pf-icon-security";
@ -63,22 +64,22 @@ export class TokenListPage extends TablePage<Token> {
columns(): TableColumn[] {
return [
new TableColumn(t`Identifier`, "identifier"),
new TableColumn(t`User`, "user"),
new TableColumn(t`Expires?`, "expiring"),
new TableColumn(t`Expiry date`, "expires"),
new TableColumn(t`Intent`, "intent"),
new TableColumn(t`Actions`),
new TableColumn(msg("Identifier"), "identifier"),
new TableColumn(msg("User"), "user"),
new TableColumn(msg("Expires?"), "expiring"),
new TableColumn(msg("Expiry date"), "expires"),
new TableColumn(msg("Intent"), "intent"),
new TableColumn(msg("Actions")),
];
}
renderToolbarSelected(): TemplateResult {
const disabled = this.selectedElements.length < 1;
return html`<ak-forms-delete-bulk
objectLabel=${t`Token(s)`}
objectLabel=${msg("Token(s)")}
.objects=${this.selectedElements}
.metadata=${(item: Token) => {
return [{ key: t`Identifier`, value: item.identifier }];
return [{ key: msg("Identifier"), value: item.identifier }];
}}
.usedBy=${(item: Token) => {
return new CoreApi(DEFAULT_CONFIG).coreTokensUsedByList({
@ -92,7 +93,7 @@ export class TokenListPage extends TablePage<Token> {
}}
>
<button ?disabled=${disabled} slot="trigger" class="pf-c-button pf-m-danger">
${t`Delete`}
${msg("Delete")}
</button>
</ak-forms-delete-bulk>`;
}
@ -100,10 +101,10 @@ export class TokenListPage extends TablePage<Token> {
renderObjectCreate(): TemplateResult {
return html`
<ak-forms-modal>
<span slot="submit"> ${t`Create`} </span>
<span slot="header"> ${t`Create Token`} </span>
<span slot="submit"> ${msg("Create")} </span>
<span slot="header"> ${msg("Create Token")} </span>
<ak-token-form slot="form"> </ak-token-form>
<button slot="trigger" class="pf-c-button pf-m-primary">${t`Create`}</button>
<button slot="trigger" class="pf-c-button pf-m-primary">${msg("Create")}</button>
</ak-forms-modal>
`;
}
@ -112,19 +113,19 @@ export class TokenListPage extends TablePage<Token> {
return [
html`<div>${item.identifier}</div>
${item.managed
? html`<small>${t`Token is managed by authentik.`}</small>`
? html`<small>${msg("Token is managed by authentik.")}</small>`
: html``}`,
html`<a href="#/identity/users/${item.userObj?.pk}">${item.userObj?.username}</a>`,
html` <ak-label color=${item.expiring ? PFColor.Green : PFColor.Orange}>
${item.expiring ? t`Yes` : t`No`}
${item.expiring ? msg("Yes") : msg("No")}
</ak-label>`,
html`${item.expiring ? item.expires?.toLocaleString() : t`-`}`,
html`${item.expiring ? item.expires?.toLocaleString() : msg("-")}`,
html`${IntentToLabel(item.intent || IntentEnum.Api)}`,
html`
${!item.managed
? html`<ak-forms-modal>
<span slot="submit"> ${t`Update`} </span>
<span slot="header"> ${t`Update Token`} </span>
<span slot="submit"> ${msg("Update")} </span>
<span slot="header"> ${msg("Update Token")} </span>
<ak-token-form slot="form" .instancePk=${item.identifier}></ak-token-form>
<button slot="trigger" class="pf-c-button pf-m-plain">
<i class="fas fa-edit"></i>