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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Mark translated props as optional
connor-baer committed Dec 3, 2024
commit 7d3efe29057f53881e3dc032615a00b6a86b5bcf
4 changes: 2 additions & 2 deletions packages/circuit-ui/components/Calendar/Calendar.tsx
Original file line number Diff line number Diff line change
@@ -129,11 +129,11 @@ export interface CalendarProps
/**
* Text label for the button to navigate to the previous month.
*/
prevMonthButtonLabel: string;
prevMonthButtonLabel?: string;
/**
* Text label for the button to navigate to the next month.
*/
nextMonthButtonLabel: string;
nextMonthButtonLabel?: string;
/**
* The number of months to display at a time. Default: `1`.
*/
14 changes: 7 additions & 7 deletions packages/circuit-ui/components/DateInput/DateInput.tsx
Original file line number Diff line number Diff line change
@@ -103,31 +103,31 @@ export interface DateInputProps
/**
* Visually hidden label for the year input.
*/
yearInputLabel: string;
yearInputLabel?: string;
/**
* Visually hidden label for the month input.
*/
monthInputLabel: string;
monthInputLabel?: string;
/**
* Visually hidden label for the day input.
*/
dayInputLabel: string;
dayInputLabel?: string;
/**
* Label for the trailing button that opens the calendar dialog.
*/
openCalendarButtonLabel: string;
openCalendarButtonLabel?: string;
/**
* Label for the button to close the calendar dialog.
*/
closeCalendarButtonLabel: string;
closeCalendarButtonLabel?: string;
/**
* Label for the button to apply the selected date and close the calendar dialog.
*/
applyDateButtonLabel: string;
applyDateButtonLabel?: string;
/**
* Label for the button to clear the date value and close the calendar dialog.
*/
clearDateButtonLabel: string;
clearDateButtonLabel?: string;
/**
* The minimum selectable date in the [ISO-8601](https://en.wikipedia.org/wiki/ISO_8601)
* format (`YYYY-MM-DD`) (inclusive).
18 changes: 12 additions & 6 deletions packages/circuit-ui/hooks/useI18n/useI18n.ts
Original file line number Diff line number Diff line change
@@ -22,23 +22,29 @@ import {
} from '../../util/i18n.js';
import { useLocale } from '../useLocale/useLocale.js';

type I18nProps<Key extends string | number | symbol> = {
[key in Key]: string;
} & {
locale: Locale;
};

export function useI18n<
P extends { [key in K]: string } & { locale?: Locale },
K extends string | number | symbol,
>(props: P, translations: Translations<K>): P {
Props extends Partial<I18nProps<Key>>,
Key extends string | number | symbol,
>(props: Props, translations: Translations<Key>): Props & I18nProps<Key> {
const locale = useLocale(props.locale);

const supportedLocale = findSupportedLocale(locale);
const strings = translations[supportedLocale];
const keys = Object.keys(strings) as K[];
const keys = Object.keys(strings) as Key[];

const translatedProps = keys.reduce(
(acc, key) => {
acc[key] = props[key] || strings[key];
return acc;
},
{} as Record<K, string>,
{} as Record<Key, string>,
);

return { ...props, ...translatedProps };
return { ...props, ...translatedProps, locale };
}