From d3aa0c00c259e8e016ca6d55679dc8dd83afdab6 Mon Sep 17 00:00:00 2001 From: Dmitry Mazurov Date: Wed, 1 Apr 2020 15:41:43 +0300 Subject: [PATCH] The language file is deleted and one localization variable is added. Should work with the old version of localization. Signed-off-by: Dmitry Mazurov --- README.md | 16 +++--- src/js/easymde.js | 112 +++++++++++++++++++++++++++++++----------- src/js/languages.json | 93 ----------------------------------- 3 files changed, 89 insertions(+), 132 deletions(-) delete mode 100644 src/js/languages.json diff --git a/README.md b/README.md index 7f31c9c..c5e9f41 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,6 @@ easyMDE.value('New input for **EasyMDE**'); ### Options list -- **locale**: Set locale. Default `en`. - **localization**: Set custom translate. - **imageTexts**: Texts displayed to the user (mainly on the status bar) for the import image feature, where `#image_name#`, `#image_size#` and `#image_max_size#` will replaced by their respective values, that can be used for customization or internationalization: - **sbInit**: Status message displayed initially if `uploadImage` is set to `true`. Defaults to `Attach files by drag and dropping or pasting from clipboard.`. @@ -201,11 +200,10 @@ Most options demonstrate the non-default behavior: ```JavaScript var editor = new EasyMDE({ - locale: 'en', localization: { - "status": { - "lines": "lines", - "words": "words", + "statusTexts": { + "lines": "lines: ", + "words": "words: ", "autosave": "Autosaved: ", }, }, @@ -306,9 +304,9 @@ var editor = new EasyMDE({ "link": "URL for the link:", "image": "URL of the image:" }, - "status": { - "lines": "lines", - "words": "words", + "statusTexts": { + "lines": "lines: ", + "words": "words: ", "autosave": "Autosaved: " }, "errorMessages": { @@ -325,7 +323,7 @@ var editor = new EasyMDE({ "sbOnUploaded": "Uploaded #image_name#", "sizeUnits": "b,Kb,Mb" }, - "toolbar": { + "toolbarTitle": { "bold": {"title": "Bold"}, "italic": {"title": "Italic"}, "strikethrough": {"title": "Strikethrough"}, diff --git a/src/js/easymde.js b/src/js/easymde.js index 9d38c52..e9311f7 100644 --- a/src/js/easymde.js +++ b/src/js/easymde.js @@ -12,7 +12,6 @@ require('codemirror/mode/gfm/gfm.js'); require('codemirror/mode/xml/xml.js'); var CodeMirrorSpellChecker = require('codemirror-spell-checker'); var marked = require('marked'); -var localization = require('./languages.json'); // Some variables @@ -731,7 +730,7 @@ function drawLink(editor) { var options = editor.options; var url = 'https://'; if (options.promptURLs) { - url = prompt(options.locale.promptTexts.link, 'https://'); + url = prompt(options.promptTexts.link, 'https://'); if (!url) { return false; } @@ -748,7 +747,7 @@ function drawImage(editor) { var options = editor.options; var url = 'https://'; if (options.promptURLs) { - url = prompt(options.locale.promptTexts.image, 'https://'); + url = prompt(options.promptTexts.image, 'https://'); if (!url) { return false; } @@ -777,9 +776,9 @@ function afterImageUploaded(editor, url) { var imageName = url.substr(url.lastIndexOf('/') + 1); _replaceSelection(cm, stat.image, options.insertTexts.uploadedImage, url); // show uploaded image filename for 1000ms - editor.updateStatusBar('upload-image', editor.options.locale.imageTexts.sbOnUploaded.replace('#image_name#', imageName)); + editor.updateStatusBar('upload-image', editor.options.imageTexts.sbOnUploaded.replace('#image_name#', imageName)); setTimeout(function () { - editor.updateStatusBar('upload-image', editor.options.locale.imageTexts.sbInit); + editor.updateStatusBar('upload-image', editor.options.imageTexts.sbInit); }, 1000); } @@ -1474,6 +1473,59 @@ var blockStyles = { 'italic': '*', }; +var localization = { + "promptTexts": { + "link": "URL for the link:", + "image": "URL of the image:", + }, + "statusTexts": { + "lines": "lines: ", + "words": "words: ", + "autosave": "Autosaved: ", + }, + "errorMessages": { + "noFileGiven": "You must select a file.", + "typeNotAllowed": "This image type is not allowed.", + "fileTooLarge": "Image #image_name# is too big (#image_size#).\nMaximum file size is #image_max_size#.", + "importError": "Something went wrong when uploading the image #image_name#.", + }, + "imageTexts": { + "sbInit": "Attach files by drag and dropping or pasting from clipboard.", + "sbOnDragEnter": "Drop image to upload it.", + "sbOnDrop": "Uploading image #images_names#...", + "sbProgress": "Uploading #file_name#: #progress#%", + "sbOnUploaded": "Uploaded #image_name#", + "sizeUnits": "b,Kb,Mb", + }, + "toolbarTitle": { + "bold": {"title": "Bold"}, + "italic": {"title": "Italic"}, + "strikethrough": {"title": "Strikethrough"}, + "heading": {"title": "Heading"}, + "heading-smaller": {"title": "Smaller Heading"}, + "heading-bigger": {"title": "Bigger Heading"}, + "heading-1": {"title": "Big Heading"}, + "heading-2": {"title": "Medium Heading"}, + "heading-3": {"title": "Small Heading"}, + "code": {"title": "Code"}, + "quote": {"title": "Quote"}, + "unordered-list": {"title": "Generic List"}, + "ordered-list": {"title": "Numbered List"}, + "clean-block": {"title": "Clean block"}, + "link": {"title": "Create Link"}, + "image": {"title": "Insert Image"}, + "upload-image": {"title": "Import an image"}, + "table": {"title": "Insert Table"}, + "horizontal-rule": {"title": "Insert Horizontal Line"}, + "preview": {"title": "Toggle Preview"}, + "side-by-side": {"title": "Toggle Side by Side"}, + "fullscreen": {"title": "Toggle Fullscreen"}, + "guide": {"title": "Markdown Guide"}, + "undo": {"title": "Undo"}, + "redo": {"title": "Redo"}, + }, +}; + /** * Interface of EasyMDE. */ @@ -1484,6 +1536,9 @@ function EasyMDE(options) { // Used later to refer to it"s parent options.parent = this; + // Merging localizations + options = extend(localization, options, options.localization || {}); + // Check if Font Awesome needs to be auto downloaded var autoDownloadFA = true; @@ -1571,9 +1626,6 @@ function EasyMDE(options) { }, options.parsingConfig || {}); - // Merging localizations - options.locale = extend(localization['en'], localization[options.locale], options.localization || {}); - options.insertTexts = extend({}, insertTexts, options.insertTexts || {}); @@ -1626,23 +1678,23 @@ function EasyMDE(options) { var self = this; this.codemirror.on('dragenter', function (cm, event) { - self.updateStatusBar('upload-image', self.options.locale.imageTexts.sbOnDragEnter); + self.updateStatusBar('upload-image', self.options.imageTexts.sbOnDragEnter); event.stopPropagation(); event.preventDefault(); }); this.codemirror.on('dragend', function (cm, event) { - self.updateStatusBar('upload-image', self.options.locale.imageTexts.sbInit); + self.updateStatusBar('upload-image', self.options.imageTexts.sbInit); event.stopPropagation(); event.preventDefault(); }); this.codemirror.on('dragleave', function (cm, event) { - self.updateStatusBar('upload-image', self.options.locale.imageTexts.sbInit); + self.updateStatusBar('upload-image', self.options.imageTexts.sbInit); event.stopPropagation(); event.preventDefault(); }); this.codemirror.on('dragover', function (cm, event) { - self.updateStatusBar('upload-image', self.options.locale.imageTexts.sbOnDragEnter); + self.updateStatusBar('upload-image', self.options.imageTexts.sbOnDragEnter); event.stopPropagation(); event.preventDefault(); }); @@ -1687,7 +1739,7 @@ EasyMDE.prototype.uploadImages = function (files, onSuccess, onError) { names.push(files[i].name); this.uploadImage(files[i], onSuccess, onError); } - this.updateStatusBar('upload-image', this.options.locale.imageTexts.sbOnDrop.replace('#images_names#', names.join(', '))); + this.updateStatusBar('upload-image', this.options.imageTexts.sbOnDrop.replace('#images_names#', names.join(', '))); }; /** @@ -1709,7 +1761,7 @@ EasyMDE.prototype.uploadImagesUsingCustomFunction = function (imageUploadFunctio names.push(files[i].name); this.uploadImageUsingCustomFunction(imageUploadFunction, files[i]); } - this.updateStatusBar('upload-image', this.options.locale.imageTexts.sbOnDrop.replace('#images_names#', names.join(', '))); + this.updateStatusBar('upload-image', this.options.imageTexts.sbOnDrop.replace('#images_names#', names.join(', '))); }; /** @@ -1967,7 +2019,7 @@ EasyMDE.prototype.autosave = function () { if (el != null && el != undefined && el != '') { var d = new Date(); var dd = new Intl.DateTimeFormat([this.options.autosave.timeFormat.locale, 'en-US'], this.options.autosave.timeFormat.format).format(d); - var save = this.options.locale.status.autosave; + var save = this.options.statusTexts.autosave; el.innerHTML = save + dd; } @@ -2034,7 +2086,7 @@ EasyMDE.prototype.uploadImage = function (file, onSuccess, onError) { self.updateStatusBar('upload-image', errorMessage); setTimeout(function () { - self.updateStatusBar('upload-image', self.options.locale.imageTexts.sbInit); + self.updateStatusBar('upload-image', self.options.imageTexts.sbInit); }, 10000); // run custom error handler @@ -2046,7 +2098,7 @@ EasyMDE.prototype.uploadImage = function (file, onSuccess, onError) { } function fillErrorMessage(errorMessage) { - var units = self.options.locale.imageTexts.sizeUnits.split(','); + var units = self.options.imageTexts.sizeUnits.split(','); return errorMessage .replace('#image_name#', file.name) .replace('#image_size#', humanFileSize(file.size, units)) @@ -2054,7 +2106,7 @@ EasyMDE.prototype.uploadImage = function (file, onSuccess, onError) { } if (file.size > this.options.imageMaxSize) { - onErrorSup(fillErrorMessage(this.options.locale.errorMessages.fileTooLarge)); + onErrorSup(fillErrorMessage(this.options.errorMessages.fileTooLarge)); return; } @@ -2069,7 +2121,7 @@ EasyMDE.prototype.uploadImage = function (file, onSuccess, onError) { request.upload.onprogress = function (event) { if (event.lengthComputable) { var progress = '' + Math.round((event.loaded * 100) / event.total); - self.updateStatusBar('upload-image', self.options.locale.imageTexts.sbProgress.replace('#file_name#', file.name).replace('#progress#', progress)); + self.updateStatusBar('upload-image', self.options.imageTexts.sbProgress.replace('#file_name#', file.name).replace('#progress#', progress)); } }; request.open('POST', this.options.imageUploadEndpoint); @@ -2079,20 +2131,20 @@ EasyMDE.prototype.uploadImage = function (file, onSuccess, onError) { var response = JSON.parse(this.responseText); } catch (error) { console.error('EasyMDE: The server did not return a valid json.'); - onErrorSup(fillErrorMessage(self.options.locale.errorMessages.importError)); + onErrorSup(fillErrorMessage(self.options.errorMessages.importError)); return; } if (this.status === 200 && response && !response.error && response.data && response.data.filePath) { onSuccess(window.location.origin + '/' + response.data.filePath); } else { - if (response.error && response.error in self.options.locale.errorMessages) { // preformatted error message - onErrorSup(fillErrorMessage(self.options.locale.errorMessages[response.error])); + if (response.error && response.error in self.options.errorMessages) { // preformatted error message + onErrorSup(fillErrorMessage(self.options.errorMessages[response.error])); } else if (response.error) { // server side generated error message onErrorSup(fillErrorMessage(response.error)); } else { //unknown error console.error('EasyMDE: Received an unexpected response after uploading the image.' + this.status + ' (' + this.statusText + ')'); - onErrorSup(fillErrorMessage(self.options.locale.errorMessages.importError)); + onErrorSup(fillErrorMessage(self.options.errorMessages.importError)); } } }; @@ -2100,7 +2152,7 @@ EasyMDE.prototype.uploadImage = function (file, onSuccess, onError) { request.onerror = function (event) { console.error('EasyMDE: An unexpected error occurred when trying to upload the image.' + event.target.status + ' (' + event.target.statusText + ')'); - onErrorSup(self.options.locale.errorMessages.importError); + onErrorSup(self.options.errorMessages.importError); }; request.send(formData); @@ -2125,7 +2177,7 @@ EasyMDE.prototype.uploadImageUsingCustomFunction = function(imageUploadFunction, self.updateStatusBar('upload-image', filledErrorMessage); setTimeout(function () { - self.updateStatusBar('upload-image', self.options.locale.imageTexts.sbInit); + self.updateStatusBar('upload-image', self.options.imageTexts.sbInit); }, 10000); // run error handler from options, this alerts the message. @@ -2133,7 +2185,7 @@ EasyMDE.prototype.uploadImageUsingCustomFunction = function(imageUploadFunction, } function fillErrorMessage(errorMessage) { - var units = self.options.locale.imageTexts.sizeUnits.split(','); + var units = self.options.imageTexts.sizeUnits.split(','); return errorMessage .replace('#image_name#', file.name) .replace('#image_size#', humanFileSize(file.size, units)) @@ -2207,7 +2259,7 @@ EasyMDE.prototype.createToolbar = function (items) { var i; for (i = 0; i < items.length; i++) { if (toolbarBuiltInButtons[items[i]] != undefined) { - items[i] = extend({}, toolbarBuiltInButtons[items[i]], this.options.locale.toolbar[items[i]] || {}); + items[i] = extend({}, toolbarBuiltInButtons[items[i]], this.options.toolbarTitle[items[i]] || {}); } } @@ -2336,7 +2388,7 @@ EasyMDE.prototype.createStatusbar = function (status) { var name = status[i]; if (name === 'words') { - dataSet = options.locale.status[name]; + dataSet = options.statusTexts[name]; defaultValue = function (el) { el.innerHTML = wordCount(cm.getValue()); @@ -2345,7 +2397,7 @@ EasyMDE.prototype.createStatusbar = function (status) { el.innerHTML = wordCount(cm.getValue()); }; } else if (name === 'lines') { - dataSet = options.locale.status[name]; + dataSet = options.statusTexts[name]; defaultValue = function (el) { el.innerHTML = cm.lineCount(); @@ -2375,7 +2427,7 @@ EasyMDE.prototype.createStatusbar = function (status) { }; } else if (name === 'upload-image') { defaultValue = function (el) { - el.innerHTML = options.locale.imageTexts.sbInit; + el.innerHTML = options.imageTexts.sbInit; }; } diff --git a/src/js/languages.json b/src/js/languages.json deleted file mode 100644 index b5b4194..0000000 --- a/src/js/languages.json +++ /dev/null @@ -1,93 +0,0 @@ - -{ - "en": { - "promptTexts": { - "link": "URL for the link:", - "image": "URL of the image:" - }, - "status": { - "lines": "lines: ", - "words": "words: ", - "autosave": "Autosaved: " - }, - "errorMessages": { - "noFileGiven": "You must select a file.", - "typeNotAllowed": "This image type is not allowed.", - "fileTooLarge": "Image #image_name# is too big (#image_size#).\nMaximum file size is #image_max_size#.", - "importError": "Something went wrong when uploading the image #image_name#." - }, - "imageTexts": { - "sbInit": "Attach files by drag and dropping or pasting from clipboard.", - "sbOnDragEnter": "Drop image to upload it.", - "sbOnDrop": "Uploading image #images_names#...", - "sbProgress": "Uploading #file_name#: #progress#%", - "sbOnUploaded": "Uploaded #image_name#", - "sizeUnits": "b,Kb,Mb" - }, - "toolbar": { - "bold": {"title": "Bold"}, - "italic": {"title": "Italic"}, - "strikethrough": {"title": "Strikethrough"}, - "heading": {"title": "Heading"}, - "heading-smaller": {"title": "Smaller Heading"}, - "heading-bigger": {"title": "Bigger Heading"}, - "heading-1": {"title": "Big Heading"}, - "heading-2": {"title": "Medium Heading"}, - "heading-3": {"title": "Small Heading"}, - "code": {"title": "Code"}, - "quote": {"title": "Quote"}, - "unordered-list": {"title": "Generic List"}, - "ordered-list": {"title": "Numbered List"}, - "clean-block": {"title": "Clean block"}, - "link": {"title": "Create Link"}, - "image": {"title": "Insert Image"}, - "upload-image": {"title": "Import an image"}, - "table": {"title": "Insert Table"}, - "horizontal-rule": {"title": "Insert Horizontal Line"}, - "preview": {"title": "Toggle Preview"}, - "side-by-side": {"title": "Toggle Side by Side"}, - "fullscreen": {"title": "Toggle Fullscreen"}, - "guide": {"title": "Markdown Guide"}, - "undo": {"title": "Undo"}, - "redo": {"title": "Redo"} - } - }, - "ru": { - "promptTexts": { - "link": "Введите гиперссылку:", - "image": "Вставьте ссылку на изображение:" - }, - "status": { - "lines": "строк: ", - "words": "слов: ", - "autosave": "Автосохранение: " - }, - "toolbar": { - "bold": {"title": "Полужирный"}, - "italic": {"title": "Курсив"}, - "strikethrough": {"title": "Зачёркнутый"}, - "heading": {"title": "Заголовок"}, - "heading-smaller": {"title": "Уменьшить заголовок"}, - "heading-bigger": {"title": "Увеличеть заголовок"}, - "heading-1": {"title": "Заголовок h1"}, - "heading-2": {"title": "Заголовок h2"}, - "heading-3": {"title": "Заголовок h3"}, - "code": {"title": "Код"}, - "quote": {"title": "Цитата"}, - "unordered-list": {"title": "Маркированный список"}, - "ordered-list": {"title": "Нумерованный список"}, - "clean-block": {"title": "Очистить блок"}, - "link": {"title": "Вставить ссылку"}, - "image": {"title": "Вставить изображение"}, - "upload-image": {"title": "Загрузка изображения"}, - "table": {"title": "Вставить таблицу"}, - "horizontal-rule": {"title": "Вставить горизонтальную линию"}, - "preview": {"title": "Предварительный просмотр"}, - "side-by-side": {"title": "Режим Side by Side"}, - "fullscreen": {"title": "На весь экран"}, - "guide": {"title": "Markdown Guide"}, - "undo": {"title": "Отменить"}, - "redo": {"title": "Повтор"} - } - } -} \ No newline at end of file