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

docs(LocaleCompare): add localization #3471

Merged
merged 8 commits into from
Aug 8, 2024
Merged
46 changes: 46 additions & 0 deletions packages/react-ui/.storybook/decorators/Locale/LocaleDecorator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from 'react';
import { Decorator } from '@storybook/react';

import { LangCodes, LocaleContext } from '../../../lib/locale';
import { defaultLangCode } from '../../../lib/locale/constants';

export const toolbarItems: { [key: string]: LangCodes } = {
ru: LangCodes.ru_RU,
en: LangCodes.en_GB,
};

const supportedLocaleControls = [
'Autocomplete',
'Calendar',
'DateInput',
'DatePicker',
'FileUploader',
'Modal',
'Paging',
'PasswordInput',
'Select',
'SidePage',
'Toast',
'Token',
'TokenInput',
];
Copy link
Member

Choose a reason for hiding this comment

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

Необходимо поддерживать следующие компоненты:

  • Autocomplete
  • Calendar
  • DateInput
  • DatePicker
  • FileUploader
  • Modal
  • Paging
  • PasswordInput
  • Select
  • SidePage
  • Toast
  • Token
  • TokenInput

Spinner -- теперь не имеет подписи и, соответственно, выбора языка. Его поддерживать не надо.

const hideLocaleBtnInUnsupportedControls = (activeControl: string) => {
const localeBtn = window.parent.document.querySelector('button[title="React UI Locale"]');
const localeBtnWrapper = localeBtn?.closest('div');
if (localeBtnWrapper) {
localeBtnWrapper.style.display = supportedLocaleControls.includes(activeControl) ? 'inline-block' : 'none';
}
};

export const LocaleDecorator: Decorator = (Story, context) => {
const { locale } = context.globals;
const activeControl = context.title.split('/')[1];
const storybookLocale = toolbarItems[locale] || defaultLangCode;
hideLocaleBtnInUnsupportedControls(activeControl);

return (
<LocaleContext.Provider value={{ langCode: storybookLocale }}>
<Story />
</LocaleContext.Provider>
);
};
15 changes: 13 additions & 2 deletions packages/react-ui/.storybook/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { THEME_2022 } from '../lib/theming/themes/Theme2022';
import { THEME_2022_DARK } from '../lib/theming/themes/Theme2022Dark';
import { ThemeFactory } from '../lib/theming/ThemeFactory';

import { LocaleDecorator, toolbarItems } from './decorators/Locale/LocaleDecorator';
import FeatureFlagsDecorator from './decorators/Features/FeatureFlagsDecorator';
import { featureFlagsConfig } from './featureFlagsConfig/featureFlagsConfig';

Expand Down Expand Up @@ -98,7 +99,6 @@ const preview: Preview = {
} else {
document.body.classList.remove('dark');
}

if (storybookTheme !== DEFAULT_THEME) {
return (
<ThemeContext.Consumer>
Expand All @@ -112,7 +112,6 @@ const preview: Preview = {
</ThemeContext.Consumer>
);
}

return <Story />;
},
(Story) => (
Expand Down Expand Up @@ -140,6 +139,7 @@ const preview: Preview = {
</ThemeContext.Consumer>
);
},
LocaleDecorator,
FeatureFlagsDecorator,
],
};
Expand All @@ -157,6 +157,17 @@ export const globalTypes = {
showName: true,
},
},
locale: {
name: 'Locale',
description: 'React UI Locale',
defaultValue: 'ru',
toolbar: {
icon: 'globe',
items: Object.keys(toolbarItems),
showName: true,
dynamicTitle: true,
},
},
};

if (isTestEnv) {
Expand Down