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

@ -1,3 +1,4 @@
import { placeholderHelperText } from "@goauthentik/admin/helperText";
import { DEFAULT_CONFIG } from "@goauthentik/common/api/config";
import { first } from "@goauthentik/common/utils";
import "@goauthentik/elements/forms/FormGroup";
@ -5,8 +6,7 @@ import "@goauthentik/elements/forms/HorizontalFormElement";
import { ModelForm } from "@goauthentik/elements/forms/ModelForm";
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";
import { ifDefined } from "lit/directives/if-defined.js";
@ -45,9 +45,9 @@ export class LDAPSourceForm extends ModelForm<LDAPSource, string> {
getSuccessMessage(): string {
if (this.instance) {
return t`Successfully updated source.`;
return msg("Successfully updated source.");
} else {
return t`Successfully created source.`;
return msg("Successfully created source.");
}
}
@ -66,7 +66,7 @@ export class LDAPSourceForm extends ModelForm<LDAPSource, string> {
renderForm(): TemplateResult {
return html`<form class="pf-c-form pf-m-horizontal">
<ak-form-element-horizontal label=${t`Name`} ?required=${true} name="name">
<ak-form-element-horizontal label=${msg("Name")} ?required=${true} name="name">
<input
type="text"
value="${ifDefined(this.instance?.name)}"
@ -74,7 +74,7 @@ export class LDAPSourceForm extends ModelForm<LDAPSource, string> {
required
/>
</ak-form-element-horizontal>
<ak-form-element-horizontal label=${t`Slug`} ?required=${true} name="slug">
<ak-form-element-horizontal label=${msg("Slug")} ?required=${true} name="slug">
<input
type="text"
value="${ifDefined(this.instance?.slug)}"
@ -94,7 +94,7 @@ export class LDAPSourceForm extends ModelForm<LDAPSource, string> {
<i class="fas fa-check" aria-hidden="true"></i>
</span>
</span>
<span class="pf-c-switch__label">${t`Enabled`}</span>
<span class="pf-c-switch__label">${msg("Enabled")}</span>
</label>
</ak-form-element-horizontal>
<ak-form-element-horizontal name="syncUsers">
@ -109,7 +109,7 @@ export class LDAPSourceForm extends ModelForm<LDAPSource, string> {
<i class="fas fa-check" aria-hidden="true"></i>
</span>
</span>
<span class="pf-c-switch__label">${t`Sync users`}</span>
<span class="pf-c-switch__label">${msg("Sync users")}</span>
</label>
</ak-form-element-horizontal>
<ak-form-element-horizontal name="syncUsersPassword">
@ -124,10 +124,12 @@ export class LDAPSourceForm extends ModelForm<LDAPSource, string> {
<i class="fas fa-check" aria-hidden="true"></i>
</span>
</span>
<span class="pf-c-switch__label">${t`User password writeback`}</span>
<span class="pf-c-switch__label">${msg("User password writeback")}</span>
</label>
<p class="pf-c-form__helper-text">
${t`Login password is synced from LDAP into authentik automatically. Enable this option only to write password changes in authentik back to LDAP.`}
${msg(
"Login password is synced from LDAP into authentik automatically. Enable this option only to write password changes in authentik back to LDAP.",
)}
</p>
</ak-form-element-horizontal>
<ak-form-element-horizontal name="syncGroups">
@ -142,14 +144,14 @@ export class LDAPSourceForm extends ModelForm<LDAPSource, string> {
<i class="fas fa-check" aria-hidden="true"></i>
</span>
</span>
<span class="pf-c-switch__label">${t`Sync groups`}</span>
<span class="pf-c-switch__label">${msg("Sync groups")}</span>
</label>
</ak-form-element-horizontal>
<ak-form-group .expanded=${true}>
<span slot="header"> ${t`Connection settings`} </span>
<span slot="header"> ${msg("Connection settings")} </span>
<div slot="body" class="pf-c-form">
<ak-form-element-horizontal
label=${t`Server URI`}
label=${msg("Server URI")}
?required=${true}
name="serverUri"
>
@ -161,7 +163,7 @@ export class LDAPSourceForm extends ModelForm<LDAPSource, string> {
required
/>
<p class="pf-c-form__helper-text">
${t`Specify multiple server URIs by separating them with a comma.`}
${msg("Specify multiple server URIs by separating them with a comma.")}
</p>
</ak-form-element-horizontal>
<ak-form-element-horizontal name="startTls">
@ -176,14 +178,14 @@ export class LDAPSourceForm extends ModelForm<LDAPSource, string> {
<i class="fas fa-check" aria-hidden="true"></i>
</span>
</span>
<span class="pf-c-switch__label">${t`Enable StartTLS`}</span>
<span class="pf-c-switch__label">${msg("Enable StartTLS")}</span>
</label>
<p class="pf-c-form__helper-text">
${t`To use SSL instead, use 'ldaps://' and disable this option.`}
${msg("To use SSL instead, use 'ldaps://' and disable this option.")}
</p>
</ak-form-element-horizontal>
<ak-form-element-horizontal
label=${t`TLS Verification Certificate`}
label=${msg("TLS Verification Certificate")}
name="peerCertificate"
>
<ak-search-select
@ -215,10 +217,12 @@ export class LDAPSourceForm extends ModelForm<LDAPSource, string> {
>
</ak-search-select>
<p class="pf-c-form__helper-text">
${t`When connecting to an LDAP Server with TLS, certificates are not checked by default. Specify a keypair to validate the remote certificate.`}
${msg(
"When connecting to an LDAP Server with TLS, certificates are not checked by default. Specify a keypair to validate the remote certificate.",
)}
</p>
</ak-form-element-horizontal>
<ak-form-element-horizontal label=${t`Bind CN`} name="bindCn">
<ak-form-element-horizontal label=${msg("Bind CN")} name="bindCn">
<input
type="text"
value="${ifDefined(this.instance?.bindCn)}"
@ -226,13 +230,17 @@ export class LDAPSourceForm extends ModelForm<LDAPSource, string> {
/>
</ak-form-element-horizontal>
<ak-form-element-horizontal
label=${t`Bind Password`}
label=${msg("Bind Password")}
?writeOnly=${this.instance !== undefined}
name="bindPassword"
>
<input type="text" value="" class="pf-c-form-control" />
</ak-form-element-horizontal>
<ak-form-element-horizontal label=${t`Base DN`} ?required=${true} name="baseDn">
<ak-form-element-horizontal
label=${msg("Base DN")}
?required=${true}
name="baseDn"
>
<input
type="text"
value="${ifDefined(this.instance?.baseDn)}"
@ -243,10 +251,10 @@ export class LDAPSourceForm extends ModelForm<LDAPSource, string> {
</div>
</ak-form-group>
<ak-form-group ?expanded=${true}>
<span slot="header"> ${t`LDAP Attribute mapping`} </span>
<span slot="header"> ${msg("LDAP Attribute mapping")} </span>
<div slot="body" class="pf-c-form">
<ak-form-element-horizontal
label=${t`User Property Mappings`}
label=${msg("User Property Mappings")}
?required=${true}
name="propertyMappings"
>
@ -278,14 +286,14 @@ export class LDAPSourceForm extends ModelForm<LDAPSource, string> {
})}
</select>
<p class="pf-c-form__helper-text">
${t`Property mappings used to user creation.`}
${msg("Property mappings used to user creation.")}
</p>
<p class="pf-c-form__helper-text">
${t`Hold control/command to select multiple items.`}
${msg("Hold control/command to select multiple items.")}
</p>
</ak-form-element-horizontal>
<ak-form-element-horizontal
label=${t`Group Property Mappings`}
label=${msg("Group Property Mappings")}
?required=${true}
name="propertyMappingsGroup"
>
@ -312,18 +320,18 @@ export class LDAPSourceForm extends ModelForm<LDAPSource, string> {
})}
</select>
<p class="pf-c-form__helper-text">
${t`Property mappings used to group creation.`}
${msg("Property mappings used to group creation.")}
</p>
<p class="pf-c-form__helper-text">
${t`Hold control/command to select multiple items.`}
${msg("Hold control/command to select multiple items.")}
</p>
</ak-form-element-horizontal>
</div>
</ak-form-group>
<ak-form-group>
<span slot="header"> ${t`Additional settings`} </span>
<span slot="header"> ${msg("Additional settings")} </span>
<div slot="body" class="pf-c-form">
<ak-form-element-horizontal label=${t`Group`} name="syncParentGroup">
<ak-form-element-horizontal label=${msg("Group")} name="syncParentGroup">
<ak-search-select
.fetchObjects=${async (query?: string): Promise<Group[]> => {
const args: CoreGroupsListRequest = {
@ -350,10 +358,10 @@ export class LDAPSourceForm extends ModelForm<LDAPSource, string> {
>
</ak-search-select>
<p class="pf-c-form__helper-text">
${t`Parent group for all the groups imported from LDAP.`}
${msg("Parent group for all the groups imported from LDAP.")}
</p>
</ak-form-element-horizontal>
<ak-form-element-horizontal label=${t`User path`} name="userPathTemplate">
<ak-form-element-horizontal label=${msg("User path")} name="userPathTemplate">
<input
type="text"
value="${first(
@ -362,12 +370,10 @@ export class LDAPSourceForm extends ModelForm<LDAPSource, string> {
)}"
class="pf-c-form-control"
/>
<p class="pf-c-form__helper-text">
${t`Path template for users created. Use placeholders like \`%(slug)s\` to insert the source slug.`}
</p>
<p class="pf-c-form__helper-text">${placeholderHelperText}</p>
</ak-form-element-horizontal>
<ak-form-element-horizontal
label=${t`Addition User DN`}
label=${msg("Addition User DN")}
name="additionalUserDn"
>
<input
@ -376,11 +382,11 @@ export class LDAPSourceForm extends ModelForm<LDAPSource, string> {
class="pf-c-form-control"
/>
<p class="pf-c-form__helper-text">
${t`Additional user DN, prepended to the Base DN.`}
${msg("Additional user DN, prepended to the Base DN.")}
</p>
</ak-form-element-horizontal>
<ak-form-element-horizontal
label=${t`Addition Group DN`}
label=${msg("Addition Group DN")}
name="additionalGroupDn"
>
<input
@ -389,11 +395,11 @@ export class LDAPSourceForm extends ModelForm<LDAPSource, string> {
class="pf-c-form-control"
/>
<p class="pf-c-form__helper-text">
${t`Additional group DN, prepended to the Base DN.`}
${msg("Additional group DN, prepended to the Base DN.")}
</p>
</ak-form-element-horizontal>
<ak-form-element-horizontal
label=${t`User object filter`}
label=${msg("User object filter")}
?required=${true}
name="userObjectFilter"
>
@ -404,11 +410,11 @@ export class LDAPSourceForm extends ModelForm<LDAPSource, string> {
required
/>
<p class="pf-c-form__helper-text">
${t`Consider Objects matching this filter to be Users.`}
${msg("Consider Objects matching this filter to be Users.")}
</p>
</ak-form-element-horizontal>
<ak-form-element-horizontal
label=${t`Group object filter`}
label=${msg("Group object filter")}
?required=${true}
name="groupObjectFilter"
>
@ -419,11 +425,11 @@ export class LDAPSourceForm extends ModelForm<LDAPSource, string> {
required
/>
<p class="pf-c-form__helper-text">
${t`Consider Objects matching this filter to be Groups.`}
${msg("Consider Objects matching this filter to be Groups.")}
</p>
</ak-form-element-horizontal>
<ak-form-element-horizontal
label=${t`Group membership field`}
label=${msg("Group membership field")}
?required=${true}
name="groupMembershipField"
>
@ -434,11 +440,13 @@ export class LDAPSourceForm extends ModelForm<LDAPSource, string> {
required
/>
<p class="pf-c-form__helper-text">
${t`Field which contains members of a group. Note that if using the "memberUid" field, the value is assumed to contain a relative distinguished name. e.g. 'memberUid=some-user' instead of 'memberUid=cn=some-user,ou=groups,...'`}
${msg(
"Field which contains members of a group. Note that if using the \"memberUid\" field, the value is assumed to contain a relative distinguished name. e.g. 'memberUid=some-user' instead of 'memberUid=cn=some-user,ou=groups,...'",
)}
</p>
</ak-form-element-horizontal>
<ak-form-element-horizontal
label=${t`Object uniqueness field`}
label=${msg("Object uniqueness field")}
?required=${true}
name="objectUniquenessField"
>
@ -449,7 +457,7 @@ export class LDAPSourceForm extends ModelForm<LDAPSource, string> {
required
/>
<p class="pf-c-form__helper-text">
${t`Field which contains a unique Identifier.`}
${msg("Field which contains a unique Identifier.")}
</p>
</ak-form-element-horizontal>
</div>

View File

@ -9,8 +9,7 @@ import "@goauthentik/elements/buttons/SpinnerButton";
import "@goauthentik/elements/events/ObjectChangelog";
import "@goauthentik/elements/forms/ModalForm";
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";
@ -63,7 +62,7 @@ export class LDAPSourceViewPage extends AKElement {
return html`<ak-tabs>
<section
slot="page-overview"
data-tab-title="${t`Overview`}"
data-tab-title="${msg("Overview")}"
class="pf-c-page__main-section pf-m-no-padding-mobile"
@activate=${() => {
new SourcesApi(DEFAULT_CONFIG)
@ -81,7 +80,9 @@ export class LDAPSourceViewPage extends AKElement {
<dl class="pf-c-description-list pf-m-2-col-on-lg">
<div class="pf-c-description-list__group">
<dt class="pf-c-description-list__term">
<span class="pf-c-description-list__text">${t`Name`}</span>
<span class="pf-c-description-list__text"
>${msg("Name")}</span
>
</dt>
<dd class="pf-c-description-list__description">
<div class="pf-c-description-list__text">
@ -92,7 +93,7 @@ export class LDAPSourceViewPage extends AKElement {
<div class="pf-c-description-list__group">
<dt class="pf-c-description-list__term">
<span class="pf-c-description-list__text"
>${t`Server URI`}</span
>${msg("Server URI")}</span
>
</dt>
<dd class="pf-c-description-list__description">
@ -104,7 +105,7 @@ export class LDAPSourceViewPage extends AKElement {
<div class="pf-c-description-list__group">
<dt class="pf-c-description-list__term">
<span class="pf-c-description-list__text"
>${t`Base DN`}</span
>${msg("Base DN")}</span
>
</dt>
<dd class="pf-c-description-list__description">
@ -119,33 +120,35 @@ export class LDAPSourceViewPage extends AKElement {
</div>
<div class="pf-c-card__footer">
<ak-forms-modal>
<span slot="submit"> ${t`Update`} </span>
<span slot="header"> ${t`Update LDAP Source`} </span>
<span slot="submit"> ${msg("Update")} </span>
<span slot="header"> ${msg("Update LDAP Source")} </span>
<ak-source-ldap-form slot="form" .instancePk=${this.source.slug}>
</ak-source-ldap-form>
<button slot="trigger" class="pf-c-button pf-m-primary">
${t`Edit`}
${msg("Edit")}
</button>
</ak-forms-modal>
</div>
</div>
<div class="pf-c-card pf-l-grid__item pf-m-12-col">
<div class="pf-c-card__title">
<p>${t`Sync status`}</p>
<p>${msg("Sync status")}</p>
</div>
<div class="pf-c-card__body">
${this.syncState.length < 1
? html`<p>${t`Not synced yet.`}</p>`
? html`<p>${msg("Not synced yet.")}</p>`
: html`
<ul class="pf-c-list">
${this.syncState.map((task) => {
let header = "";
if (task.status === TaskStatusEnum.Warning) {
header = t`Task finished with warnings`;
header = msg("Task finished with warnings");
} else if (task.status === TaskStatusEnum.Error) {
header = t`Task finished with errors`;
header = msg("Task finished with errors");
} else {
header = t`Last sync: ${task.taskFinishTimestamp.toLocaleString()}`;
header = msg(
str`Last sync: ${task.taskFinishTimestamp.toLocaleString()}`,
);
}
return html`<li>
<p>${task.taskName}</p>
@ -179,7 +182,7 @@ export class LDAPSourceViewPage extends AKElement {
});
}}
>
${t`Run sync again`}
${msg("Run sync again")}
</ak-action-button>
</div>
</div>
@ -187,7 +190,7 @@ export class LDAPSourceViewPage extends AKElement {
</section>
<section
slot="page-changelog"
data-tab-title="${t`Changelog`}"
data-tab-title="${msg("Changelog")}"
class="pf-c-page__main-section pf-m-no-padding-mobile"
>
<div class="pf-l-grid pf-m-gutter">