8
0
Эх сурвалжийг харах

Add utils with localization of common labels.

Mateusz Samsel 6 жил өмнө
parent
commit
c129ec730b

+ 2 - 2
packages/ckeditor5-link/src/linkediting.js

@@ -10,7 +10,7 @@
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import LinkCommand from './linkcommand';
 import UnlinkCommand from './unlinkcommand';
-import { createLinkElement, ensureSafeUrl } from './utils';
+import { createLinkElement, ensureSafeUrl, getLocalizedDecorators } from './utils';
 import AutomaticDecorators from './utils/automaticdecorators';
 import ManualDecorator from './utils/manualdecorator';
 import bindTwoStepCaretToAttribute from '@ckeditor/ckeditor5-engine/src/utils/bindtwostepcarettoattribute';
@@ -77,7 +77,7 @@ export default class LinkEditing extends Plugin {
 		editor.commands.add( 'link', new LinkCommand( editor ) );
 		editor.commands.add( 'unlink', new UnlinkCommand( editor ) );
 
-		const linkDecorators = editor.config.get( 'link.decorators' ) || [];
+		const linkDecorators = getLocalizedDecorators( editor );
 
 		this._enableAutomaticDecorators( linkDecorators.filter( item => item.mode === DECORATOR_AUTOMATIC ) );
 		this._enableManualDecorators( linkDecorators.filter( item => item.mode === DECORATOR_MANUAL ) );

+ 32 - 0
packages/ckeditor5-link/src/utils.js

@@ -59,3 +59,35 @@ function isSafeUrl( url ) {
 
 	return normalizedUrl.match( SAFE_URL );
 }
+
+/**
+ * Returns configuration options as defined in {@link module:link/link~LinkConfig#decorators `editor.config.decorators`} but processed
+ * to respect localization of the editor, i.e. to display {@link module:link/link~LinkDecoratorManualDefinition label}
+ * in the correct language.
+ *
+ * **Note:** Only few most commonly used labels has provided translations. In all other cases decorators configuration should be
+ * directly translated in configuration.
+ *
+ * @param {module:core/editor/editor~Editor} editor An editor instance
+ * @returns {Array.<module:link/link~LinkDecoratorAutomaticDefinition|module:link/link~LinkDecoratorManualDefinition>}
+ */
+export function getLocalizedDecorators( editor ) {
+	const t = editor.t;
+	const decorators = editor.config.get( 'link.decorators' );
+
+	if ( decorators ) {
+		const localizedDecoratorsLabels = {
+			'Open in a new window': t( 'Open in a new window' ),
+			'Downloadable': t( 'Downloadable' )
+		};
+
+		return decorators.map( decorator => {
+			if ( decorator.label && localizedDecoratorsLabels[ decorator.label ] ) {
+				decorator.label = localizedDecoratorsLabels[ decorator.label ];
+			}
+			return decorator;
+		} );
+	} else {
+		return [];
+	}
+}