Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve i18n utility, add English fallbacks #186

Merged
merged 8 commits into from Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
121 changes: 51 additions & 70 deletions scripts/contacts-book.js
@@ -1,8 +1,8 @@
import { Buffer } from 'buffer';
import { Account } from './accounts';

Check warning on line 2 in scripts/contacts-book.js

View workflow job for this annotation

GitHub Actions / ESLint

scripts/contacts-book.js#L2

'Account' is defined but never used (@typescript-eslint/no-unused-vars)
import { Database } from './database';
import { doms, toClipboard } from './global';
import { ALERTS, translation } from './i18n';
import { ALERTS, tr, translation } from './i18n';
import {
confirmPopup,
createAlert,
Expand Down Expand Up @@ -274,7 +274,7 @@
const cDB = await Database.getInstance();
const cAccount = await cDB.getAccount();
if (!cAccount || (cAccount.contacts && cAccount.contacts.length === 0))
return createAlert('warning', ALERTS.CONTACTS_YOU_HAVE_NONE, [], 2500);
return createAlert('warning', ALERTS.CONTACTS_YOU_HAVE_NONE, 2500);
return renderContacts(cAccount, true);
}

Expand All @@ -300,8 +300,9 @@
if (!cAccount || !cAccount.contacts) {
return createAlert(
'warning',
ALERTS.CONTACTS_ENCRYPT_FIRST,
[{ button: translation.secureYourWallet }],
tr(ALERTS.CONTACTS_ENCRYPT_FIRST, [
{ button: translation.secureYourWallet },
]),
3500
);
}
Expand Down Expand Up @@ -395,10 +396,9 @@
doms.domModalQR.innerHTML = `
<center>
<b>${translation.secureYourWallet}</b>
<p>${translation.encryptFirstForContacts.replace(
'{button}',
translation.secureYourWallet
)}</p>
<p>${tr(translation.encryptFirstForContacts, [
{ button: translation.secureYourWallet },
])}</p>
</center>
`;
}
Expand Down Expand Up @@ -507,16 +507,15 @@

// Verify the name
if (strName.length < 1)
return createAlert('warning', ALERTS.CONTACTS_NAME_REQUIRED, [], 2500);
return createAlert('warning', ALERTS.CONTACTS_NAME_REQUIRED, 2500);
if (strName.length > 32)
return createAlert('warning', ALERTS.CONTACTS_NAME_TOO_LONG, [], 2500);
return createAlert('warning', ALERTS.CONTACTS_NAME_TOO_LONG, 2500);

// Verify the address
if (!isStandardAddress(strAddr) && !isXPub(strAddr))
return createAlert(
'warning',
ALERTS.INVALID_ADDRESS,
[{ address: strAddr }],
tr(ALERTS.INVALID_ADDRESS, [{ address: strAddr }]),
3000
);

Expand All @@ -533,7 +532,6 @@
createAlert(
'warning',
ALERTS.CONTACTS_CANNOT_ADD_YOURSELF,
[],
3500
);
return false;
Expand All @@ -542,12 +540,7 @@
} else {
// Ensure we're not adding (one of) our own address(es)
if (await masterKey.isOwnAddress(strAddr)) {
createAlert(
'warning',
ALERTS.CONTACTS_CANNOT_ADD_YOURSELF,
[],
3500
);
createAlert('warning', ALERTS.CONTACTS_CANNOT_ADD_YOURSELF, 3500);
return false;
}
}
Expand All @@ -562,22 +555,24 @@

// If both Name and Key are saved, then they just tried re-adding the same Contact twice
if (cContactByName && cContactByPubkey) {
createAlert('warning', ALERTS.CONTACTS_ALREADY_EXISTS, [], 3000);
createAlert('warning', ALERTS.CONTACTS_ALREADY_EXISTS, 3000);
return true;
}

// If the Name is saved, but not key, then this *could* be a kind of Username-based phishing attempt
if (cContactByName && !cContactByPubkey) {
createAlert('warning', ALERTS.CONTACTS_NAME_ALREADY_EXISTS, [], 4000);
createAlert('warning', ALERTS.CONTACTS_NAME_ALREADY_EXISTS, 4000);
return true;
}

// If the Key is saved, but not the name: perhaps the Contact changed their name?
if (!cContactByName && cContactByPubkey) {
createAlert(
'warning',
ALERTS.CONTACTS_KEY_ALREADY_EXISTS,
[{ newName: strName }, { oldName: cContactByPubkey.label }],
tr(ALERTS.CONTACTS_KEY_ALREADY_EXISTS, [
{ newName: strName },
{ oldName: cContactByPubkey.label },
]),
7500
);
return true;
Expand Down Expand Up @@ -608,16 +603,15 @@
) {
// Verify the name
if (strName.length < 1)
return createAlert('warning', ALERTS.CONTACTS_NAME_REQUIRED, [], 2500);
return createAlert('warning', ALERTS.CONTACTS_NAME_REQUIRED, 2500);
if (strName.length > 32)
return createAlert('warning', ALERTS.CONTACTS_NAME_TOO_LONG, [], 2500);
return createAlert('warning', ALERTS.CONTACTS_NAME_TOO_LONG, 2500);

// Verify the address
if (!isStandardAddress(strPubkey) && !isXPub(strPubkey))
return createAlert(
'warning',
ALERTS.INVALID_ADDRESS,
[{ address: strPubkey }],
tr(ALERTS.INVALID_ADDRESS, [{ address: strPubkey }]),
4000
);

Expand All @@ -635,7 +629,6 @@
createAlert(
'warning',
ALERTS.CONTACTS_CANNOT_ADD_YOURSELF,
[],
3500
);
return false;
Expand All @@ -644,12 +637,7 @@
} else {
// Ensure we're not adding (one of) our own address(es)
if (await masterKey.isOwnAddress(strPubkey)) {
createAlert(
'warning',
ALERTS.CONTACTS_CANNOT_ADD_YOURSELF,
[],
3500
);
createAlert('warning', ALERTS.CONTACTS_CANNOT_ADD_YOURSELF, 3500);
return false;
}
}
Expand All @@ -664,19 +652,14 @@
// If both Name and Key are saved, then they just tried re-adding the same Contact twice
if (cContactByName && cContactByPubkey) {
if (fDuplicateNotif)
createAlert('warning', ALERTS.CONTACTS_ALREADY_EXISTS, [], 3000);
createAlert('warning', ALERTS.CONTACTS_ALREADY_EXISTS, 3000);
return true;
}

// If the Name is saved, but not key, then this *could* be a kind of Username-based phishing attempt
if (cContactByName && !cContactByPubkey) {
if (fDuplicateNotif)
createAlert(
'warning',
ALERTS.CONTACTS_NAME_ALREADY_EXISTS,
[],
4000
);
createAlert('warning', ALERTS.CONTACTS_NAME_ALREADY_EXISTS, 4000);
return true;
}

Expand All @@ -685,8 +668,10 @@
if (fDuplicateNotif)
createAlert(
'warning',
ALERTS.CONTACTS_KEY_ALREADY_EXISTS,
[{ newName: strName }, { oldName: cContactByPubkey.label }],
tr(ALERTS.CONTACTS_KEY_ALREADY_EXISTS, [
{ newName: strName },
{ oldName: cContactByPubkey.label },
]),
7500
);
return true;
Expand All @@ -695,19 +680,18 @@
// Render an 'Add to Contacts' UI
const strHTML = `
<p>
${translation.addContactSubtext.replace('{strName}', strName)}
${tr(translation.addContactSubtext, [{ strName: strName }])}
<br>
<br>
<i style="opacity: 0.75">${translation.addContactWarning.replace(
'{strName}',
strName
)}</i>
<i style="opacity: 0.75">${tr(translation.addContactWarning, [
{ strName: strName },
])}</i>
</p>
`;

// Hook the Contact Prompt to the Popup UI, which resolves when the user has interacted with the Contact Prompt
const fAdd = await confirmPopup({
title: translation.addContactTitle.replace('{strName}', strName),
title: tr(translation.addContactTitle, [{ strName: strName }]),
html: strHTML,
});

Expand All @@ -723,8 +707,7 @@
// Notify
createAlert(
'success',
ALERTS.CONTACTS_ADDED,
[{ strName: strName }],
tr(ALERTS.CONTACTS_ADDED, [{ strName: strName }]),
3000
);
}
Expand Down Expand Up @@ -753,22 +736,21 @@

// Hook the Contact Prompt to the Popup UI, which resolves when the user has interacted with the Contact Prompt
const fContinue = await confirmPopup({
title: translation.editContactTitle.replace(
'{strName}',
sanitizeHTML(cContact.label)
),
title: tr(translation.editContactTitle, [
{ strName: sanitizeHTML(cContact.label) },
]),
html: strHTML,
});
if (!fContinue) return false;

// Verify the name
const strNewName = document.getElementById('contactsNewNameInput').value;
if (strNewName.length < 1) {
createAlert('warning', ALERTS.CONTACTS_NAME_REQUIRED, [], 2500);
createAlert('warning', ALERTS.CONTACTS_NAME_REQUIRED, 2500);
return false;
}
if (strNewName.length > 32) {
createAlert('warning', ALERTS.CONTACTS_NAME_TOO_LONG, [], 2500);
createAlert('warning', ALERTS.CONTACTS_NAME_TOO_LONG, 2500);
return false;
}

Expand All @@ -777,8 +759,9 @@
if (cContactByNewName) {
createAlert(
'warning',
ALERTS.CONTACTS_EDIT_NAME_ALREADY_EXISTS,
[{ strNewName: strNewName }],
tr(ALERTS.CONTACTS_EDIT_NAME_ALREADY_EXISTS, [
{ strNewName: strNewName },
]),
4500
);
return false;
Expand Down Expand Up @@ -865,7 +848,7 @@
return fAdded;
}
} else {
createAlert('warning', ALERTS.CONTACTS_NOT_A_CONTACT_QR, [], 2500);
createAlert('warning', ALERTS.CONTACTS_NOT_A_CONTACT_QR, 2500);
return false;
}
}
Expand All @@ -881,16 +864,14 @@

// Confirm the deletion
const fConfirmed = await confirmPopup({
title: translation.removeContactTitle.replace(
'{strName}',
sanitizeHTML(cContact.label)
),
title: tr(translation.removeContactTitle, [
{ strName: sanitizeHTML(cContact.label) },
]),
html: `
<p>
${translation.removeContactSubtext.replace(
'{strName}',
sanitizeHTML(cContact.label)
)}
${tr(translation.removeContactSubtext, [
{ strName: sanitizeHTML(cContact.label) },
])}
<br>
<br>
<i style="opacity: 0.65">${translation.removeContactNote}</i>
Expand All @@ -913,11 +894,11 @@
// Verify the name
const strNewName = domInput.value.trim();
if (strNewName.length < 1) {
createAlert('warning', ALERTS.CONTACTS_NAME_REQUIRED, [], 2500);
createAlert('warning', ALERTS.CONTACTS_NAME_REQUIRED, 2500);
return false;
}
if (strNewName.length > 32) {
createAlert('warning', ALERTS.CONTACTS_NAME_TOO_LONG, [], 2500);
createAlert('warning', ALERTS.CONTACTS_NAME_TOO_LONG, 2500);
return false;
}

Expand Down