Skip to content

Commit

Permalink
feat: change i18n configuration method (#190)
Browse files Browse the repository at this point in the history
Changes the localization behavior to apply only for fields explicitly marked as localized (those with "Enable localization for this field" enabled in the Content-Types Builder). The previous logic, which applied localization automatically when i18n was detected (unless ignorei18n was set to true), has been replaced.

BREAKING CHANGE: Localization is no longer applied automatically for all fields when i18n is detected. It now applies only to fields explicitly configured for localization in the Content-Types Builder.
  • Loading branch information
nshenderov committed Dec 12, 2024
1 parent d24d9a9 commit 5a189c4
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 31 deletions.
12 changes: 9 additions & 3 deletions admin/src/components/EditorProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react';
import type { InputProps } from '@strapi/strapi/admin';

import { type Preset, getConfiguredPreset } from '../config';
import { type Preset, getClonedPreset, setUpLanguage } from '../config';
import type { WordCountPluginStats } from './CKEReact';

type EditorProviderBaseProps = Pick<
Expand All @@ -12,6 +12,7 @@ type EditorProviderBaseProps = Pick<
presetName: string;
wordsLimit?: number;
charsLimit?: number;
isFieldLocalized: boolean;
};

type EditorContextValue = EditorProviderBaseProps & {
Expand Down Expand Up @@ -46,21 +47,24 @@ export function EditorProvider({
wordsLimit,
charsLimit,
children,
isFieldLocalized,
}: EditorProviderProps) {
const [preset, setPreset] = useState<Preset | null>(null);
const [error, setError] = useState<string | null>(fieldError ?? null);

useEffect(() => {
(async () => {
const currentPreset = await getConfiguredPreset(presetName);
const currentPreset = getClonedPreset(presetName);

await setUpLanguage(currentPreset.editorConfig, isFieldLocalized);

if (placeholder) {
currentPreset.editorConfig.placeholder = placeholder;
}

setPreset(currentPreset);
})();
}, [presetName, placeholder]);
}, [presetName, placeholder, isFieldLocalized]);

useEffect(() => {
setError(fieldError ?? null);
Expand Down Expand Up @@ -106,6 +110,7 @@ export function EditorProvider({
wordsLimit,
charsLimit,
validateInputLength,
isFieldLocalized,
}),
[
name,
Expand All @@ -121,6 +126,7 @@ export function EditorProvider({
preset,
error,
validateInputLength,
isFieldLocalized,
]
);

Expand Down
7 changes: 7 additions & 0 deletions admin/src/components/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ type CKEditorFieldProps = Readonly<
maxLengthWords: number;
maxLengthCharacters: number;
};
pluginOptions?: {
i18n?: {
localized?: boolean;
};
};
};
}
>;
Expand All @@ -30,6 +35,7 @@ function Field({
required = false,
}: CKEditorFieldProps) {
const { preset, maxLengthWords, maxLengthCharacters } = attribute.options;
const isFieldLocalized = attribute?.pluginOptions?.i18n?.localized ?? false;

return (
<EditorProvider
Expand All @@ -44,6 +50,7 @@ function Field({
presetName={preset}
wordsLimit={maxLengthWords}
charsLimit={maxLengthCharacters}
isFieldLocalized={isFieldLocalized}
>
<Editor />
</EditorProvider>
Expand Down
1 change: 1 addition & 0 deletions admin/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './types';
export * from './language';
export * from './pluginConfig';
export * from './colors';
export * from './defaultPreset';
Expand Down
23 changes: 11 additions & 12 deletions admin/src/config/language.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import type { EditorConfig } from './types';
import { getPreferedLanguage } from '../utils/localStorage';

export async function setUpLanguage(config: EditorConfig): Promise<void> {
const i18nLang = detecti18n();
const preferedLanguage = getPreferedLanguage();

if (typeof config.language === 'object') {
if (Boolean(i18nLang) && !config.language.ignorei18n) {
config.language.content = i18nLang;
}
} else {
export async function setUpLanguage(
config: EditorConfig,
isFieldLocalized: boolean
): Promise<void> {
if (typeof config.language !== 'object') {
config.language = {
ui: config.language,
content: i18nLang,
ignorei18n: false,
content: config.language,
textPartLanguage: undefined,
};
}

config.language.ui ||= preferedLanguage;
config.language.ui ||= getPreferedLanguage();

if (isFieldLocalized) {
config.language.content = detecti18n();
}

if (config.language.ui !== 'en') {
await importLang(config, config.language.ui);
Expand Down
5 changes: 1 addition & 4 deletions admin/src/config/presets.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { defaultPreset } from './defaultPreset';
import { setUpLanguage } from './language';
import type { Field, PluginConfig, Preset } from './types';

export const pluginPresets: Record<string, Preset> = {
default: defaultPreset,
};

export async function getConfiguredPreset(presetName: string): Promise<Preset> {
export function getClonedPreset(presetName: string): Preset {
const { presets: userPresets, dontMergePresets } = window.SH_CKE_CONFIG || {};

if (dontMergePresets && !userPresets) {
Expand All @@ -23,8 +22,6 @@ export async function getConfiguredPreset(presetName: string): Promise<Preset> {
},
};

await setUpLanguage(clonedPreset.editorConfig);

return clonedPreset;
}

Expand Down
13 changes: 2 additions & 11 deletions admin/src/config/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import type {
EditorConfig as CKE5EditorConfig,
LanguageConfig as CKE5LanguageConfig,
} from 'ckeditor5';
import type { EditorConfig as CKE5EditorConfig } from 'ckeditor5';
import type { Interpolation } from 'styled-components';
import type { ExportToGlobal } from './expToGlobal';

Expand Down Expand Up @@ -46,13 +43,7 @@ export type PartialIsNotAllowedForNewPresets = {
editorConfig?: Partial<EditorConfig>;
};

export type EditorConfig = Omit<CKE5EditorConfig, 'language'> & {
language?: string | Language;
};

export type Language = CKE5LanguageConfig & {
ignorei18n?: boolean;
};
export type EditorConfig = CKE5EditorConfig;

export type Field = {
metadatas: Metadata;
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@
"@ckeditor/ckeditor5-engine" "43.3.1"
ckeditor5 "43.3.1"

"@ckeditor/ckeditor5-react@^9.4.0":
"@ckeditor/ckeditor5-react@^9.3.1":
version "9.4.0"
resolved "https://registry.yarnpkg.com/@ckeditor/ckeditor5-react/-/ckeditor5-react-9.4.0.tgz#2d750ed585a4668931be089da7a02e823c706f16"
integrity sha512-8PU7YUV0ZKYP10akKgarT8nq5QxEDPmuj6Wn5dl/DJ7qroDx6VuC/ysCYcgljseimbDNYDjPQKhatNEcsMd7Ew==
Expand Down

0 comments on commit 5a189c4

Please sign in to comment.