Skip to content

feat(@clayui/localized-input): LPD-50722 Use Language Picker for switching languages #6049

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

pat270
Copy link
Member

@pat270 pat270 commented May 2, 2025

Copy link
Member

@ethib137 ethib137 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Patrick... just a couple things.

Also, the ticket is missing this, but we had discussed that we should use the trigger-without-language-label version of the Language selector since it takes up less space. Can you update to use that? With that change it will also be important to ensure that version of the Language Selector has a tooltip so that it's clear for accessibility.

Thanks.

displayType = 'info';
label = messages.default;
} else if (translation) {
if (translation) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are now checking if translation exists, we should make it an optional prop. translation?

<LanguagePicker
defaultLocaleId={defaultLanguage.id}
locales={languagePickerLocales}
onSelectedLocaleChange={(locale: any) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's call this localeId to make it clearer.

const languagePickerLocales = useMemo(() => {
locales.forEach((locale, index) => {
if (!locale.id) {
locales[index]!['id'] = locale.label;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently this is mutating the array, which can cause big problems in React. Instead we can simplify this with the following:

locales.forEach((locale, index) => {
	return locales.map(locale => ({
		id: locale.label,
		...locale,
	}))
}, []);

This will create a new array and also let a passed id take precedence over using the label as the id.

} else {
displayType = 'secondary';
label = sub(messages.translating, [translated, total]);
if (total !== 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic doesn't seem right. This means the only way to display the untranslated message would be if total is 0. But I think total refers to "total available translations", so this isn't correct. It should probably be if (translated !== 0) since translated refers to which of the available translations have been translated.

total: 1,
translated: translations[selectedLocale.label] ? 1 : 0,
};
}, [translations]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can all be updated to something like this:

const languagePickerTranslations = useMemo(() => {
	const languagePickerTranslations = {};

	locales.forEach((locale) => {
		languagePickerTranslations[locale.symbol] = {
			total: 1, // this should always be one because we just have one input being translated.
			translated: translations[locale.symbol] ? 1 : 0],
		};
	}

	return languagePickerTranslations;
}, [locales, translations]);

For localized input the status label should never be "translating", so make sure to check that.

Also, an empty string should be rendered as untranslated, not translated. Test this by adding a translation and then removing it.

Copy link
Member Author

@pat270 pat270 May 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ethib137 in the storybook, translations keys match locales label values. The symbol values are lower case. translations[locale.symbol] returns undefined because en-us should be en-US should lower case keys still be valid or should it be an exact match?

@pat270
Copy link
Member Author

pat270 commented May 13, 2025

I'm also getting an error when trying to apply ariaLabels

ariaLabels?: {
default: string;
openLocalizations: string;
translated: string;
untranslated: string;
};
to LanguagePicker messages.
type Messages = {
default: string;
option: string;
trigger: string;
translated: string;
translating: string;
untranslated: string;
};

TypeError: Cannot read properties of undefined (reading 'split')
    at sub (http://localhost:8888/packages_clay-shared_src_index_tsx.iframe.bundle.js:1508:26)
    at children (http://localhost:8888/packages_clay-core_src_language-picker_index_ts.iframe.bundle.js:222:75)
    at http://localhost:8888/packages_clay-core_src_collection_index_ts.iframe.bundle.js:467:125
    at http://localhost:8888/packages_clay-core_src_collection_index_ts.iframe.bundle.js:583:5
    at mountMemo (http://localhost:8888/vendors-node_modules_storybook_addon-a11y_preview_js-node_modules_storybook_addon-actions_pre-a27f80.iframe.bundle.js:71290:19)
    at Object.useMemo (http://localhost:8888/vendors-node_modules_storybook_addon-a11y_preview_js-node_modules_storybook_addon-actions_pre-a27f80.iframe.bundle.js:71586:16)
    at useMemo (http://localhost:8888/vendors-node_modules_storybook_addon-a11y_preview_js-node_modules_storybook_addon-actions_pre-a27f80.iframe.bundle.js:84626:21)
    at useCollection (http://localhost:8888/packages_clay-core_src_collection_index_ts.iframe.bundle.js:575:50)
    at Picker (http://localhost:8888/packages_clay-core_src_language-picker_index_ts.iframe.bundle.js:779:79)
    at renderWithHooks (http://localhost:8888/vendors-node_modules_storybook_addon-a11y_preview_js-node_modules_storybook_addon-actions_pre-a27f80.iframe.bundle.js:70651:18)

I get the same error when trying to apply directly to the LanguagePicker story.

<LanguagePicker
hideTriggerText={args.hideTriggerText}
id="languagePicker"
locales={localesLong}
onSelectedLocaleChange={setSelectedLocaleId}
selectedLocaleId={selectedLocaleId}
shrink={args.shrink}
small={args.small}
translations={translations}
/>

I can't figure out how the data should be organized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants