Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embed default translations #2786

Merged
merged 16 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .changeset/proud-pillows-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
'@sumup-oss/circuit-ui': minor
---

Added default translations for labels that don't change with the usage of the component. Translations are included for all locales where SumUp operates, namely `bg-BG`, `cs-CZ`, `da-DK`, `de-AT`, `de-CH`, `de-DE`, `de-LU`, `el-CY`, `el-GR`, `en-AU`, `en-GB`, `en-IE`, `en-MT`, `en-US`, `es-CL`, `es-CO`, `es-ES`, `es-MX`, `es-PE`, `es-US`, `et-EE`, `fi-FI`, `fr-BE`, `fr-CH`, `fr-FR`, `fr-LU`, `hr-HR`, `hu-HU`, `it-CH`, `it-IT`, `lt-LT`, `lv-LV`, `nb-NO`, `nl-BE`, `nl-NL`, `pl-PL`, `pt-BR`, `pt-PT`, `ro-RO`, `sk-SK`, `sl-SI`, and `sv-SE`. The current locale is determined based on the `locale` prop or the `navigator.language` API in environments that support it. If no supported locale is found, `en-US` is used as a fallback.

The following component props are now optional:

- Button, IconButton: `loadingLabel`
- Calendar: `prevMonthButtonLabel`, `nextMonthButtonLabel`
- DateInput: `yearInputLabel`, `monthInputLabel`, `dayInputLabel`, `openCalendarButtonLabel`, `closeCalendarButtonLabel`, `applyDateButtonLabel`, `clearDateButtonLabel`
- Toggletip: `closeButtonLabel`

We'll add default translations to more components in the future.
1 change: 0 additions & 1 deletion packages/circuit-ui/components/Button/Button.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,5 @@ export const Loading = (args: ButtonProps) => {
};

Loading.args = {
loadingLabel: 'Loading',
isLoading: true,
};
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ export const Loading = (args: IconButtonProps) => {
};

Loading.args = {
loadingLabel: 'Loading',
isLoading: true,
icon: Plus,
};
14 changes: 5 additions & 9 deletions packages/circuit-ui/components/Button/base.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('Button', () => {
});

it('should render loading button with loading label', () => {
const loadingLabel = 'Loading';
const loadingLabel = 'Submitting form';
render(<Button isLoading loadingLabel={loadingLabel} />);
expect(screen.getByText(loadingLabel)).toBeVisible();
});
Expand All @@ -76,15 +76,15 @@ describe('Button', () => {
});

it('should render as disabled when loading', () => {
render(<Button isLoading loadingLabel="Loading" />);
render(<Button isLoading />);

const button = screen.getByRole('button');

expect(button).toHaveAttribute('aria-disabled', 'true');
});

it('should render as disabled when not loading', () => {
render(<Button disabled isLoading={false} loadingLabel="Loading" />);
render(<Button disabled isLoading={false} />);

const button = screen.getByRole('button');

Expand Down Expand Up @@ -134,17 +134,13 @@ describe('Button', () => {
});

it('should meet accessibility guidelines for Loading button', async () => {
const { container } = render(
<Button isLoading={true} loadingLabel="Loading">
Button
</Button>,
);
const { container } = render(<Button isLoading={true}>Button</Button>);
const actual = await axe(container);
expect(actual).toHaveNoViolations();
});

it('should have aria-busy and aria-live for a loading button', () => {
render(<Button isLoading loadingLabel="Loading" />);
render(<Button isLoading />);
const button = screen.getByRole('button');

expect(button).toHaveAttribute('aria-live', 'polite');
Expand Down
25 changes: 12 additions & 13 deletions packages/circuit-ui/components/Button/base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ import {
} from '../../util/errors.js';
import { utilClasses } from '../../styles/utility.js';
import { clsx } from '../../styles/clsx.js';
import { useI18n } from '../../hooks/useI18n/useI18n.js';
import type { Locale } from '../../util/i18n.js';

import classes from './base.module.css';
import { translations } from './translations/index.js';

type LinkElProps = Omit<AnchorHTMLAttributes<HTMLAnchorElement>, 'onClick'>;
type ButtonElProps = Omit<ButtonHTMLAttributes<HTMLButtonElement>, 'onClick'>;
Expand Down Expand Up @@ -89,6 +92,13 @@ export type SharedButtonProps = LinkElProps &
* Render the Button using any element.
*/
'as'?: AsPropType;
/**
* One or more [IETF BCP 47](https://en.wikipedia.org/wiki/IETF_language_tag)
* locale identifiers such as `'de-DE'` or `['GB', 'en-US']`.
* When passing an array, the first supported locale is used.
* Defaults to `navigator.language` in supported environments.
*/
locale?: Locale;
};

export type CreateButtonComponentProps = SharedButtonProps & {
Expand Down Expand Up @@ -141,8 +151,9 @@ export function createButtonComponent<Props>(
icon: LeadingIcon,
navigationIcon: TrailingIcon,
as,
locale,
...sharedProps
} = mapProps(props);
} = useI18n(mapProps(props), translations);

const components = useComponents();
const Link = components.Link as AsPropType;
Expand All @@ -163,18 +174,6 @@ export function createButtonComponent<Props>(
event.nativeEvent.stopImmediatePropagation();
};

if (
process.env.NODE_ENV !== 'production' &&
process.env.NODE_ENV !== 'test' &&
hasLoadingState &&
!isSufficientlyLabelled(loadingLabel)
) {
throw new AccessibilityError(
componentName,
"The `loadingLabel` prop is missing or invalid. Remove the `isLoading` prop if you don't intend to use the Button's loading state.",
);
}

if (
process.env.NODE_ENV !== 'production' &&
process.env.NODE_ENV !== 'test' &&
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Зареждане"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Načítám"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Henter"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Lade"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Lade"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Lade"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Lade"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Φόρτωση"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Φόρτωση"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Loading"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Loading"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Loading"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Loading"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Loading"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Cargando"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Cargando"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Cargando"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Cargando"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Cargando"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Cargando"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Laeb"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Lataa..."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Chargement"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Chargement"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Chargement"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Chargement"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Učitavanje"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Betöltés"
}
26 changes: 26 additions & 0 deletions packages/circuit-ui/components/Button/translations/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright 2024, SumUp Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { transformModulesToTranslations } from '../../../util/i18n.js';

export const translations = transformModulesToTranslations<
typeof import('./en-US.json')
>(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore import.meta.glob is supported by Vite
import.meta.glob('./*.json', {
eager: true,
}),
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "In caricamento"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "In caricamento"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Įkeliama"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Ielādē"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Laster"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Laden"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Laden"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Wczytuje dane"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Carregando"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "A carregar"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Se încarcă"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Prebieha načítavanie…"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Nalaganje"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"loadingLabel": "Hämtar"
}
2 changes: 0 additions & 2 deletions packages/circuit-ui/components/Calendar/Calendar.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ function wait(ms: number) {
describe('Calendar', () => {
const baseProps = {
selection: new Temporal.PlainDate(2020, 3, 15),
prevMonthButtonLabel: 'Previous month',
nextMonthButtonLabel: 'Next month',
};

function getDayElement(day: number) {
Expand Down
31 changes: 3 additions & 28 deletions packages/circuit-ui/components/Calendar/Calendar.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,38 +46,15 @@ Base.args = {
selection: today.add({ days: 3 }),
minDate: today.subtract({ days: 7 }),
maxDate: today.add({ months: 3 }),
prevMonthButtonLabel: 'Previous month',
nextMonthButtonLabel: 'Next month',
};

export const Localized = ({ selection, ...args }: CalendarProps) => {
const [date, setDate] = useState(selection);
return (
<Stack>
<Calendar
{...args}
selection={date}
onSelect={setDate}
locale="de-DE"
prevMonthButtonLabel="Vorheriger Monat"
nextMonthButtonLabel="Nächster Monat"
/>
<Calendar
{...args}
selection={date}
onSelect={setDate}
locale="bg-BG"
prevMonthButtonLabel="Предишния месец"
nextMonthButtonLabel="Следващият месец"
/>
<Calendar
{...args}
selection={date}
onSelect={setDate}
locale="pt-BR"
prevMonthButtonLabel="Mês anterior"
nextMonthButtonLabel="Mês próximo"
/>
<Calendar {...args} selection={date} onSelect={setDate} locale="de-DE" />
<Calendar {...args} selection={date} onSelect={setDate} locale="bg-BG" />
<Calendar {...args} selection={date} onSelect={setDate} locale="pt-BR" />
</Stack>
);
};
Expand All @@ -98,8 +75,6 @@ Modifiers.args = {
selection: today.add({ days: 3 }),
minDate: today.subtract({ days: 7 }),
maxDate: today.add({ months: 3 }),
prevMonthButtonLabel: 'Previous month',
nextMonthButtonLabel: 'Next month',
modifiers: {
[today.subtract({ days: 10 }).toString()]: {
description: 'Booked',
Expand Down
Loading
Loading