Skip to content
Open
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
50 changes: 32 additions & 18 deletions jquery.ajaxchimp.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ For e.g. 'http://blahblah.us1.list-manage.com/subscribe/post-json?u=5afsdhfuhdsi

$.ajaxChimp = {
responses: {
'We have sent you a confirmation email' : 0,
'Please enter a value' : 1,
'An email address must contain a single @' : 2,
'The domain portion of the email address is invalid (the portion after the @: )' : 3,
'The username portion of the email address is invalid (the portion before the @: )' : 4,
'This email address looks fake or invalid. Please enter a real email address' : 5
0: 'We have sent you a confirmation email',
1: 'Please enter a value',
2: 'An email address must contain a single @',
3: 'The domain portion of the email address is invalid',
4: 'The username portion of the email address is invalid',
5: 'This email address looks fake or invalid. Please enter a real email address',
6: 'is already subscribed to list'
},
translations: {
'en': null
Expand Down Expand Up @@ -93,15 +94,31 @@ For e.g. 'http://blahblah.us1.list-manage.com/subscribe/post-json?u=5afsdhfuhdsi
}
}


// Translate and display message
var length = 0, msgnr;

if(Object.keys){
length = Object.keys($.ajaxChimp.responses).length
}
else {
for (var key in $.ajaxChimp.responses){
if ($.ajaxChimp.responses.hasOwnProperty(key)) {
length++;
}
}
}

while(length--) {
if (msg.indexOf($.ajaxChimp.responses[length])!==-1) {
msgnr = length;
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a similar fix for this locally, but I didn't change the responses structure.
My fix looks like:

var i18n = $.ajaxChimp.translations;
// Translate and display message
if (settings.language !== 'en' && i18n && i18n[settings.language]) {
    var translation = i18n[settings.language][$.ajaxChimp.responses[msg]];
    if (!translation) {
        // try partial match
        $.each($.ajaxChimp.responses, function(resp, key) {
            if (msg.indexOf(resp) > -1) {
                translation = i18n[settings.language][key];
                return;
            }
        });
    }
    if (translation) {
        msg = translation;
    }
}

This way we only do partial matches if the string doesn't exist in the hash map.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I think the fix needs to be a lot better. Meaning, there are cases where the response is like:

{"result":"error","msg":"[email protected] is already subscribed to list My Newsletter. <a href=\"http:\/\/address.list-manage1.com\/subscribe\/send-email?u=uuu&id=ddd&e=xxx==\">Click here to update your profile.<\/a>"})

I believe the best fix would be not to do partial matches (like I did also) but rather regular expressions to extract the information we need.

BTW, any luck on requesting Mailchimp to return these errors already translated from the server? Basically the translations exist and can be tweaked from the Mailchimp webpage. I still don't understand why they don't translate this before sending the response. Perhaps we could send a language param to get the response from them directly?

The reason why I'm saying this is because I might have customers that want to translate the same errors in different ways and that way I wouldn't have to change the translations per customer, and rather just tell them how to do on Mailchimp interface.


if (
settings.language !== 'en'
&& $.ajaxChimp.responses[msg] !== undefined
&& $.ajaxChimp.translations
&& $.ajaxChimp.translations[settings.language]
&& $.ajaxChimp.translations[settings.language][$.ajaxChimp.responses[msg]]
settings.language !== 'en' && msgnr > -1 && $.ajaxChimp.translations && $.ajaxChimp.translations[settings.language] && $.ajaxChimp.translations[settings.language][msgnr]
) {
msg = $.ajaxChimp.translations[settings.language][$.ajaxChimp.responses[msg]];
msg = $.ajaxChimp.translations[settings.language][msgnr];
}
label.html(msg);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also add msg variable to the settings.callback(resp, msg); so It will be possible to make use of this messages.
BTW I look forward to merge it to master branch


Expand All @@ -123,19 +140,16 @@ For e.g. 'http://blahblah.us1.list-manage.com/subscribe/post-json?u=5afsdhfuhdsi
success: successCallback,
dataType: 'jsonp',
error: function (resp, text) {
console.log('mailchimp ajax submit error: ' + text);
window.console.log('mailchimp ajax submit error: ' + text);
}
});

// Translate and display submit message
var submitMsg = 'Submitting...';
if(
settings.language !== 'en'
&& $.ajaxChimp.translations
&& $.ajaxChimp.translations[settings.language]
&& $.ajaxChimp.translations[settings.language]['submit']
settings.language !== 'en' && $.ajaxChimp.translations && $.ajaxChimp.translations[settings.language] && $.ajaxChimp.translations[settings.language].submit
) {
submitMsg = $.ajaxChimp.translations[settings.language]['submit'];
submitMsg = $.ajaxChimp.translations[settings.language].submit;
}
label.html(submitMsg).show(2000);

Expand Down
2 changes: 1 addition & 1 deletion jquery.ajaxchimp.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.