Kaynağa Gözat

Added basic form of translation-service.

Maciej Bukowski 9 yıl önce
ebeveyn
işleme
328cc9391f

+ 44 - 0
packages/ckeditor5-utils/src/translation-service.js

@@ -0,0 +1,44 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/**
+ * @module utils/translation-service
+ */
+
+const translations = {};
+
+export function define( lang, fileDictionary ) {
+	if ( !( lang in translations ) ) {
+		translations[ lang ] = {};
+	}
+
+	const dictionary = translations[ lang ];
+
+	for ( const packageName in fileDictionary ) {
+		if ( !( packageName in dictionary ) ) {
+			dictionary[ packageName ] = {};
+		}
+
+		Object.assign( dictionary[ packageName ], fileDictionary[ packageName ] );
+	}
+}
+
+export function translate( lang, str ) {
+	const [ contextId, englishWord ] = str.split( ': ' );
+	const [ packageName, translationKey ] = contextId.split( '/' );
+
+	if (
+		!( lang in translations ) ||
+		!( packageName in translations[ lang ] ) ||
+		!( translationKey in translations[ lang ][ packageName ] )
+	) {
+		return englishWord;
+	}
+
+	return translations[ lang ][ packageName ][ translationKey ];
+
+	// development
+	// return str.replace( /^[^:]+: /, '' );
+}