From fc8008b7e464dfc6393cdffc58a043401d6cefff Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 9 Nov 2016 23:04:49 +0100 Subject: [PATCH] =?UTF-8?q?Fixed=20issue##388=20-=20Added=20translation=20?= =?UTF-8?q?support=20for=20forms=20based=20on=20=20tag.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/assets/form/form.js | 76 ++++++++++++++++++++++++++++++++++---- 1 file changed, 68 insertions(+), 8 deletions(-) diff --git a/public/assets/form/form.js b/public/assets/form/form.js index e053987fe..cbb655f1d 100644 --- a/public/assets/form/form.js +++ b/public/assets/form/form.js @@ -16,6 +16,7 @@ $(function() { messageThankYou: 'Thank you for your inquiry! We\'ll contact you soon as possible.', // optional messageNoConfig: 'Unable to load form config from server. Maybe featrue is disabled.', // optional showTitle: true, + lang: 'de', // optional, will be used per default modal: true, attributes: [ { @@ -48,6 +49,7 @@ $(function() { var pluginName = 'ZammadForm', defaults = { + lang: undefined, debug: false, noCSS: false, prefixCSS: 'zammad-form-', @@ -78,15 +80,39 @@ $(function() { placeholder: 'Your Message...', rows: 7, }, - ] + ], + translations: { + de: { + 'Name': 'Name', + 'Your Name': 'Ihr Name', + 'Email': 'E-Mail', + 'Your Email': 'Ihre E-Mail', + 'Message': 'Nachricht', + 'Your Message...': 'Ihre Nachricht...', + }, + es: { + 'Name': 'Nombre', + 'Your Name': 'tu Nombre', + 'Email': 'correo electrónico', + 'Your Email': 'Tu correo electrónico', + 'Message': 'Mensaje', + 'Your Message...': 'tu Mensaje...', + }, + fr: { + 'Name': 'Prénom', + 'Your Name': 'Votre nom', + 'Email': 'Email', + 'Your Email': 'Votre Email', + 'Message': 'Message', + 'Your Message...': 'Votre message...', + }, + } }; function Plugin(element, options) { - this.element = element; + this.element = element this.$element = $(element) - this.options = $.extend({}, defaults, options); - this._defaults = defaults; this._name = pluginName; @@ -100,9 +126,18 @@ $(function() { this.endpoint_config = this._src.replace(this._script_location, this._endpoint_config) this.endpoint_submit = this._src.replace(this._script_location, this._endpoint_submit) + this.options = $.extend({}, defaults, options) + if (!this.options.lang) { + this.options.lang = $('html').attr('lang') + } + if (this.options.lang) { + this.options.lang = this.options.lang.replace(/-.+?$/, '') + this.log('debug', "lang: " + this.options.lang) + } + this._config = {} - this.init(); + this.init() } Plugin.prototype.init = function () { @@ -260,12 +295,12 @@ $(function() { $form.append('

' + this.options.messageTitle + '

') } $.each(this.options.attributes, function(index, value) { - var item = $('
') + var item = $('
') if (value.tag == 'input') { - item.append('') + item.append('') } else if (value.tag == 'textarea') { - item.append('') + item.append('') } $form.append(item) }) @@ -341,6 +376,31 @@ $(function() { $('.js-logDisplay').prepend('
' + logString + '
') } + // translation method + Plugin.prototype.T = function() { + var string = arguments[0] + var items = 2 <= arguments.length ? slice.call(arguments, 1) : [] + if (this.options.lang && this.options.lang !== 'en') { + if (!this.options.translations[this.options.lang]) { + this.log('debug', "Translation '" + this.options.lang + "' needed!") + } + else { + translations = this.options.translations[this.options.lang] + if (!translations[string]) { + this.log('debug', "Translation needed for '" + this.options.lang + "' " + string + "'") + } + string = translations[string] || string + } + } + if (items) { + for (i = 0, len = items.length; i < len; i++) { + item = items[i] + string = string.replace(/%s/, item) + } + } + return string + } + $.fn[pluginName] = function (options) { return this.each(function () { var instance = $.data(this, 'plugin_' + pluginName)