fix: I18n improvements (#836)

This commit is contained in:
Joakim Repomaa 2023-06-08 19:57:36 +02:00 committed by GitHub
parent 7744df1103
commit 971720044b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 31 deletions

View File

@ -96,14 +96,23 @@ const EditorInterface = ({
slug, slug,
}: TranslatedProps<EditorInterfaceProps>) => { }: TranslatedProps<EditorInterfaceProps>) => {
const { locales, defaultLocale } = useMemo(() => getI18nInfo(collection), [collection]) ?? {}; const { locales, defaultLocale } = useMemo(() => getI18nInfo(collection), [collection]) ?? {};
const [selectedLocale, setSelectedLocale] = useState<string>(locales?.[1] ?? 'en'); const translatedLocales = useMemo(
() => locales?.filter(locale => locale !== defaultLocale) ?? [],
[locales, defaultLocale],
);
const [previewActive, setPreviewActive] = useState( const [previewActive, setPreviewActive] = useState(
localStorage.getItem(PREVIEW_VISIBLE) !== 'false', localStorage.getItem(PREVIEW_VISIBLE) !== 'false',
); );
const i18nEnabled = locales && locales.length > 0;
const [i18nActive, setI18nActive] = useState( const [i18nActive, setI18nActive] = useState(
Boolean(localStorage.getItem(I18N_VISIBLE) !== 'false' && locales && locales.length > 0), Boolean(localStorage.getItem(I18N_VISIBLE) !== 'false' && i18nEnabled),
);
const [selectedLocale, setSelectedLocale] = useState<string>(
(i18nActive ? translatedLocales?.[0] : defaultLocale) ?? 'en',
); );
useEffect(() => { useEffect(() => {
@ -132,8 +141,11 @@ const EditorInterface = ({
const handleToggleI18n = useCallback(() => { const handleToggleI18n = useCallback(() => {
const newI18nActive = !i18nActive; const newI18nActive = !i18nActive;
setI18nActive(newI18nActive); setI18nActive(newI18nActive);
setSelectedLocale(selectedLocale =>
newI18nActive && selectedLocale === defaultLocale ? translatedLocales?.[0] : selectedLocale,
);
localStorage.setItem(I18N_VISIBLE, `${newI18nActive}`); localStorage.setItem(I18N_VISIBLE, `${newI18nActive}`);
}, [i18nActive]); }, [i18nActive, setSelectedLocale, translatedLocales, defaultLocale]);
const handleLocaleChange = useCallback((locale: string) => { const handleLocaleChange = useCallback((locale: string) => {
setSelectedLocale(locale); setSelectedLocale(locale);
@ -169,7 +181,8 @@ const EditorInterface = ({
const collectHasI18n = hasI18n(collection); const collectHasI18n = hasI18n(collection);
const editor = ( const editor = useMemo(
() => (
<div <div
key={defaultLocale} key={defaultLocale}
id="control-pane" id="control-pane"
@ -190,13 +203,31 @@ const EditorInterface = ({
entry={entry} entry={entry}
fields={fields} fields={fields}
fieldsErrors={fieldsErrors} fieldsErrors={fieldsErrors}
locale={defaultLocale} locale={i18nActive ? defaultLocale : selectedLocale}
submitted={submitted} submitted={submitted}
hideBorder={!finalPreviewActive && !i18nActive} hideBorder={!finalPreviewActive && !i18nActive}
canChangeLocale={i18nEnabled && !i18nActive}
onLocaleChange={handleLocaleChange}
slug={slug} slug={slug}
t={t} t={t}
/> />
</div> </div>
),
[
collection,
defaultLocale,
selectedLocale,
entry,
fields,
fieldsErrors,
finalPreviewActive,
i18nActive,
i18nEnabled,
handleLocaleChange,
slug,
submitted,
t,
],
); );
const editorLocale = useMemo( const editorLocale = useMemo(
@ -219,6 +250,7 @@ const EditorInterface = ({
onLocaleChange={handleLocaleChange} onLocaleChange={handleLocaleChange}
submitted={submitted} submitted={submitted}
canChangeLocale canChangeLocale
context="i18nSplit"
hideBorder hideBorder
t={t} t={t}
/> />
@ -228,7 +260,7 @@ const EditorInterface = ({
); );
const previewEntry = collectHasI18n const previewEntry = collectHasI18n
? getPreviewEntry(entry, selectedLocale[0], defaultLocale) ? getPreviewEntry(entry, selectedLocale, defaultLocale)
: entry; : entry;
const editorWithPreview = ( const editorWithPreview = (

View File

@ -30,6 +30,7 @@ export interface EditorControlPaneProps {
hideBorder: boolean; hideBorder: boolean;
slug?: string; slug?: string;
onLocaleChange?: (locale: string) => void; onLocaleChange?: (locale: string) => void;
context?: 'default' | 'i18nSplit';
} }
const EditorControlPane = ({ const EditorControlPane = ({
@ -43,6 +44,7 @@ const EditorControlPane = ({
hideBorder, hideBorder,
slug, slug,
onLocaleChange, onLocaleChange,
context = 'default',
t, t,
}: TranslatedProps<EditorControlPaneProps>) => { }: TranslatedProps<EditorControlPaneProps>) => {
const pathField = useMemo( const pathField = useMemo(
@ -117,6 +119,7 @@ const EditorControlPane = ({
})} })}
canChangeLocale={canChangeLocale} canChangeLocale={canChangeLocale}
onLocaleChange={onLocaleChange} onLocaleChange={onLocaleChange}
excludeLocales={context === 'i18nSplit' ? [i18n.defaultLocale] : []}
/> />
</div> </div>
) : null} ) : null}

View File

@ -10,6 +10,7 @@ interface LocaleDropdownProps {
dropdownText: string; dropdownText: string;
canChangeLocale: boolean; canChangeLocale: boolean;
onLocaleChange?: (locale: string) => void; onLocaleChange?: (locale: string) => void;
excludeLocales?: string[];
} }
const LocaleDropdown = ({ const LocaleDropdown = ({
@ -18,6 +19,7 @@ const LocaleDropdown = ({
dropdownText, dropdownText,
canChangeLocale, canChangeLocale,
onLocaleChange, onLocaleChange,
excludeLocales = [defaultLocale],
}: LocaleDropdownProps) => { }: LocaleDropdownProps) => {
if (!canChangeLocale) { if (!canChangeLocale) {
return ( return (
@ -39,7 +41,7 @@ const LocaleDropdown = ({
<Menu label={dropdownText}> <Menu label={dropdownText}>
<MenuGroup> <MenuGroup>
{locales {locales
.filter(locale => locale !== defaultLocale) .filter(locale => !excludeLocales.includes(locale))
.map(locale => ( .map(locale => (
<MenuItemButton key={locale} onClick={() => onLocaleChange?.(locale)}> <MenuItemButton key={locale} onClick={() => onLocaleChange?.(locale)}>
{locale} {locale}