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

Allow registering translations in the defaultConfig #15903

Merged
merged 4 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 6 additions & 5 deletions packages/ckeditor5-core/src/editor/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,13 @@ export default abstract class Editor extends ObservableMixin() {

const constructor = this.constructor as typeof Editor;

// Prefer the language passed as the argument to the constructor instead of the constructor's `defaultConfig`, if both are set.
const language = config.language || ( constructor.defaultConfig && constructor.defaultConfig.language );

// We don't pass translations to the config, because its behavior of splitting keys
// with dots (e.g. `resize.width` => `resize: { width }`) breaks the translations.
const { translations, ...rest } = config;
const { translations: defaultTranslations, ...defaultConfig } = constructor.defaultConfig || {};
const { translations = defaultTranslations, ...rest } = config;

// Prefer the language passed as the argument to the constructor instead of the constructor's `defaultConfig`, if both are set.
const language = config.language || defaultConfig.language;

this._context = config.context || new Context( { language, translations } );
this._context._addEditor( this, !config.context );
Expand All @@ -289,7 +290,7 @@ export default abstract class Editor extends ObservableMixin() {
// between editors and make the watchdog feature work correctly.
const availablePlugins = Array.from( constructor.builtinPlugins || [] );

this.config = new Config<EditorConfig>( rest, constructor.defaultConfig );
this.config = new Config<EditorConfig>( rest, defaultConfig );
this.config.define( 'plugins', availablePlugins );
this.config.define( this._context._getEditorConfig() );

Expand Down
48 changes: 47 additions & 1 deletion packages/ckeditor5-core/tests/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ describe( 'Context', () => {
} );
} );

describe( 'translations', () => {
describe( 'config.translations', () => {
let editor, element;

beforeEach( () => {
Expand Down Expand Up @@ -546,6 +546,52 @@ describe( 'Context', () => {
expect( editor.locale.translations.pl.dictionary[ 'a.b' ] ).to.equal( 'value' );
} );
} );

describe( 'defaultConfig.translations', () => {
let editor, element;

beforeEach( () => {
element = document.createElement( 'div' );
document.body.appendChild( element );

class TestEditor extends ClassicTestEditor {}

TestEditor.defaultConfig = {
translations: {
pl: {
dictionary: {
bold: 'Pogrubienie',
'a.b': 'value'
}
}
}
};

return TestEditor
.create( element )
.then( _editor => {
editor = _editor;
} );
} );

afterEach( () => {
document.body.removeChild( element );

return editor.destroy();
} );

it( 'should not set translations in the config', () => {
expect( editor.config.get( 'translations' ) ).to.equal( undefined );
} );

it( 'should properly get translations with the key', () => {
expect( editor.locale.translations.pl.dictionary.bold ).to.equal( 'Pogrubienie' );
} );

it( 'should properly get translations with dot in the key', () => {
expect( editor.locale.translations.pl.dictionary[ 'a.b' ] ).to.equal( 'value' );
} );
} );
} );

function getPlugins( editor ) {
Expand Down
16 changes: 16 additions & 0 deletions packages/ckeditor5-core/tests/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,22 @@ describe( 'Editor', () => {
expect( editor.config.get( 'translations' ) ).to.equal( undefined );
} );

it( 'should use translations set as the defaultConfig option on the constructor', () => {
TestEditor.defaultConfig = {
translations: {
pl: {
dictionary: {
Bold: 'Pogrubienie'
}
}
}
};

const editor = new TestEditor();

expect( editor.config.get( 'translations' ) ).to.equal( undefined );
} );

it( 'should bind editing.view.document#isReadOnly to the editor#isReadOnly', () => {
const editor = new TestEditor();

Expand Down