Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #12108 from fabi1cazenave/smsExplicitL10nArguments…
Browse files Browse the repository at this point in the history
…-bug915058

Bug 915058 - [SMS] use more explicit l10n arguments in dialog.js, r=julienw
  • Loading branch information
fabi1cazenave committed Sep 11, 2013
2 parents 71c86af + fd2d32c commit 590eb03
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 78 deletions.
37 changes: 19 additions & 18 deletions apps/sms/js/dialog.js
Expand Up @@ -2,33 +2,34 @@
'use strict';

/*
Generic confirm screen. Only 'cancel/default' is mandatory. For having l10n
you should define the key value and you have to set l10n to 'true' or an object
that contains parameters. Options should follow the following structure:
Generic confirm screen. Only 'cancel/default' is mandatory.
Text field are localized when their `l10nId' parameter is set -- in which case,
the optional `l10nArgs' parameter is applied. Without `l10nId', the `value'
parameter will be used: it can be either an HTML node or plain text.
Options should follow the following structure:
{
title: {
value: 'foo Title',
l10n: false
value: 'foo Title'
},
body: {
value: 'foo Body',
l10n: false
l10nId: 'showMessageCount',
l10nArgs: { n: count }
},
options: {
// Cancel is a mandatory option. You need to define at least the text
// Cancel is a mandatory option. You need to define at least the text.
cancel: {
text: {
value: 'cancel',
l10n: true
l10nId: 'cancel'
}
},
// Confirm is an optional one. As in cancel, you could add as well a method
// with params
// with params.
confirm: {
text: {
value: 'remove',
l10n: true
l10nId: 'remove'
},
method: function(params) {
fooFunction(params);
Expand All @@ -42,14 +43,14 @@
function createLocalizedElement(tagName, param) {
var element = document.createElement(tagName);

// if we passed an l10nId, use the l10n `localize' method
if (param.l10nId) {
navigator.mozL10n.localize(element, param.l10nId, param.l10nArgs);

// if we passed in a HTML Fragment, it is already localized
if (param.value.nodeType) {
} else if (param.value.nodeType) {
element.appendChild(param.value);

// otherwise if l10n is not false, we use the l10n localize method
} else if (param.l10n !== false) {
navigator.mozL10n.localize(element, param.value, param.l10n);

// otherwise - stuff text in here...
} else {
element.textContent = param.value;
Expand Down
12 changes: 4 additions & 8 deletions apps/sms/js/recipients.js
Expand Up @@ -990,25 +990,21 @@
var dialog = new Dialog(
{
title: {
value: recipient.name || recipient.number,
l10n: false
value: recipient.name || recipient.number
},
body: {
value: dialogBody,
l10n: false
value: dialogBody
},
options: {
cancel: {
text: {
value: 'cancel',
l10n: true
l10nId: 'cancel'
},
method: handler
},
confirm: {
text: {
value: 'remove',
l10n: true
l10nId: 'remove'
},
method: function() {
response.isConfirmed = true;
Expand Down
10 changes: 4 additions & 6 deletions apps/sms/js/thread_ui.js
Expand Up @@ -1675,18 +1675,16 @@ var ThreadUI = global.ThreadUI = {

var dialog = new Dialog({
title: {
value: messageTitle,
l10n: true
l10nId: messageTitle
},
body: {
value: messageBody,
l10n: messageBodyParams
l10nId: messageBody,
l10nArgs: messageBodyParams
},
options: {
cancel: {
text: {
value: buttonLabel,
l10n: true
l10nId: buttonLabel
}
}
}
Expand Down
52 changes: 25 additions & 27 deletions apps/sms/test/unit/dialog_test.js
Expand Up @@ -14,18 +14,15 @@ suite('Dialog', function() {
navigator.mozL10n = MockL10n;
params = {
title: {
value: 'Foo Title',
l10n: false
value: 'Foo Title'
},
body: {
value: 'Foo body',
l10n: false
value: 'Foo Body'
},
options: {
cancel: {
text: {
value: 'Foo Cancel',
l10n: false
value: 'Foo Cancel'
}
}
}
Expand Down Expand Up @@ -98,8 +95,7 @@ suite('Dialog', function() {
// We add the confirm
params.options.confirm = {
text: {
value: 'Foo Cancel',
l10n: false
value: 'Foo Cancel'
}
};
// Now we create the new element
Expand All @@ -120,18 +116,20 @@ suite('Dialog', function() {
});

test('Checking the localization.', function() {
params.title.l10n = true;
params.body.l10n = true;
params.title = {
l10nId: 'l10n Title'
},
params.body = {
l10nId: 'l10n Body'
},
params.options.cancel = {
text: {
value: 'keyCancel',
l10n: true
l10nId: 'l10n keyCancel'
}
};
params.options.confirm = {
text: {
value: 'keyConfirm',
l10n: true
l10nId: 'l10n keyConfirm'
}
};
var l10nSpy = this.sinon.spy(navigator.mozL10n, 'localize');
Expand All @@ -150,40 +148,40 @@ suite('Dialog', function() {
var formOptions = dialogForm.getElementsByTagName('button');
assert.equal(formOptions.length, 2);
// We check localization
assert.ok(l10nSpy.calledWith(titleDOM, params.title.value),
assert.ok(l10nSpy.calledWith(titleDOM, params.title.l10nId),
'Title DOM localized with proper string');
assert.ok(l10nSpy.calledWith(bodyDOM, params.body.value),
assert.ok(l10nSpy.calledWith(bodyDOM, params.body.l10nId),
'Body DOM localized with proper string');
assert.ok(l10nSpy.calledWith(formOptions[0],
params.options.confirm.text.value),
params.options.confirm.text.l10nId),
'Confirm DOM localized with proper string');
assert.ok(l10nSpy.calledWith(formOptions[1],
params.options.cancel.text.value),
params.options.cancel.text.l10nId),
'Cancel DOM localized with proper string');
});

test('Checking parametrized body localization.', function() {
params.title.l10n = true;

params.title = {
l10nId: 'l10n Title'
},
params.body = {
value: '',
l10n: {
l10nId: 'l10n Body',
l10nArgs: {
n: 3,
numbers: ['123', '456', '789']
}
};
params.options.cancel = {
text: {
value: 'keyCancel',
l10n: true
l10nId: 'l10n keyCancel'
}
};
params.options.confirm = {
text: {
value: 'keyConfirm',
l10n: true
l10nId: 'l10n keyConfirm'
}
};

var l10nSpy = this.sinon.spy(navigator.mozL10n, 'localize');
// Now we create the new element
var dialog = new Dialog(params);
Expand All @@ -197,7 +195,7 @@ suite('Dialog', function() {
// We check how many buttons we have (mandatory + confirm one)
var bodyDOM = dialogForm.querySelector('small');
// We check localization
assert.ok(l10nSpy.calledWith(bodyDOM, params.body.value),
assert.ok(l10nSpy.calledWith(bodyDOM, params.body.l10nId),
'Body DOM localized with proper string');
});

Expand Down
5 changes: 2 additions & 3 deletions apps/sms/test/unit/mock_dialog.js
Expand Up @@ -10,9 +10,8 @@ function MockDialog(params) {
params.options[option].method();
};

// Setup the initial |false| value, this
// prevents assert.isFalse from failing
// on |undefined| trigger properties
// Setup the initial |false| value, this prevents assert.isFalse from
// failing on |undefined| trigger properties
MockDialog.triggers[option].called = false;
});
}
Expand Down
32 changes: 16 additions & 16 deletions apps/sms/test/unit/thread_ui_test.js
Expand Up @@ -1073,63 +1073,63 @@ suite('thread_ui.js >', function() {
test('show general error for no signal error', function() {
ThreadUI.showSendMessageError('NoSignalError');
assert.isTrue(MockDialog.instances[0].show.called);
assert.equal(MockDialog.calls[0].title.value,
assert.equal(MockDialog.calls[0].title.l10nId,
'sendGeneralErrorTitle');
assert.equal(MockDialog.calls[0].body.value,
assert.equal(MockDialog.calls[0].body.l10nId,
'sendGeneralErrorBody');
});

test('show general error for not found error', function() {
ThreadUI.showSendMessageError('NotFoundError');
assert.isTrue(MockDialog.instances[0].show.called);
assert.equal(MockDialog.calls[0].title.value,
assert.equal(MockDialog.calls[0].title.l10nId,
'sendGeneralErrorTitle');
assert.equal(MockDialog.calls[0].body.value,
assert.equal(MockDialog.calls[0].body.l10nId,
'sendGeneralErrorBody');
});

test('show general error for unknown error', function() {
ThreadUI.showSendMessageError('UnknownError');
assert.isTrue(MockDialog.instances[0].show.called);
assert.equal(MockDialog.calls[0].title.value,
assert.equal(MockDialog.calls[0].title.l10nId,
'sendGeneralErrorTitle');
assert.equal(MockDialog.calls[0].body.value,
assert.equal(MockDialog.calls[0].body.l10nId,
'sendGeneralErrorBody');
});

test('show general error for internal error', function() {
ThreadUI.showSendMessageError('InternalError');
assert.isTrue(MockDialog.instances[0].show.called);
assert.equal(MockDialog.calls[0].title.value,
assert.equal(MockDialog.calls[0].title.l10nId,
'sendGeneralErrorTitle');
assert.equal(MockDialog.calls[0].body.value,
assert.equal(MockDialog.calls[0].body.l10nId,
'sendGeneralErrorBody');
});

test('show general error for invalid address error', function() {
ThreadUI.showSendMessageError('InvalidAddressError');
assert.isTrue(MockDialog.instances[0].show.called);
assert.equal(MockDialog.calls[0].title.value,
assert.equal(MockDialog.calls[0].title.l10nId,
'sendGeneralErrorTitle');
assert.equal(MockDialog.calls[0].body.value,
assert.equal(MockDialog.calls[0].body.l10nId,
'sendGeneralErrorBody');
});

test('show no SIM card', function() {
ThreadUI.showSendMessageError('NoSimCardError');
assert.isTrue(MockDialog.instances[0].show.called);
assert.equal(MockDialog.calls[0].title.value,
assert.equal(MockDialog.calls[0].title.l10nId,
'sendNoSimCardTitle');
assert.equal(MockDialog.calls[0].body.value,
assert.equal(MockDialog.calls[0].body.l10nId,
'sendNoSimCardBody');
});

test('show air plane mode', function() {
ThreadUI.showSendMessageError('RadioDisabledError');
assert.isTrue(MockDialog.instances[0].show.called);
assert.equal(MockDialog.calls[0].title.value,
assert.equal(MockDialog.calls[0].title.l10nId,
'sendAirplaneModeTitle');
assert.equal(MockDialog.calls[0].body.value,
assert.equal(MockDialog.calls[0].body.l10nId,
'sendAirplaneModeBody');
});

Expand All @@ -1138,9 +1138,9 @@ suite('thread_ui.js >', function() {
'FdnCheckError',
['123', '456', '789']
);
assert.equal(MockDialog.calls[0].title.value,
assert.equal(MockDialog.calls[0].title.l10nId,
'fdnBlockedTitle');
assert.equal(MockDialog.calls[0].body.value,
assert.equal(MockDialog.calls[0].body.l10nId,
'fdnBlockedBody');
});
});
Expand Down

0 comments on commit 590eb03

Please sign in to comment.