Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const PLUGIN = 'ExtractKeysWebpackPlugin';
*
* @type {RegExp}
*/
const TRANSLATE_CALL = /(?:^|[^\w'-])(?:I18n\.)?t\)?\(\s*['"](.+?)['"][,\s)]/g;
const TRANSLATE_CALL = /(?:^|[^\w'-])t\)?\(\[?(['"][a-z\d\s_.,'"]+['"])]?[,\s)]/g;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I hope we can continue to use regexes for this and don't have to get into parser/lexer/AST land lol

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good luck to whoever has to try to interpret this regex in the future! (My future self included) 😅


/**
* Given an original file name and locale, returns a modified file name with the locale injected
Expand All @@ -42,7 +42,9 @@ function getAdditionalAssetFilename(filename, locale) {
* @return {string[]} Translation keys.
*/
const getTranslationKeys = (source) =>
Array.from(source.matchAll(TRANSLATE_CALL)).map(([, key]) => key);
Array.from(source.matchAll(TRANSLATE_CALL)).flatMap(([, keys]) =>
keys.split(',').map((key) => key.replace(/[ '"]/g, '')),
);

/**
* Given a file name, returns true if the file is a JavaScript file, or false otherwise.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,33 @@ describe('getAdditionalAssetFilename', () => {

describe('getTranslationKeys', () => {
const source = `
import { t } from '@18f/identity-i18n';
const text = t('forms.button.submit');
const message = t('forms.messages', { count: 2 });
const values = t(['forms.key1', 'forms.key2']);

const text = t('explicit.call');

// i18n-tasks-use t('comment.added.1')
/* i18n-tasks-use t('comment.added.2') */
// i18n-tasks-use t('item.1')
/* i18n-tasks-use t('item.2') */
/**
* i18n-tasks-use t('comment.added.3')
* i18n-tasks-use t('item.3')
*/
Array.from({ length: 3 }, (_, i) => t(\`comment.added.\${i + 1}\`))
Array.from({ length: 3 }, (_, i) => t(\`item.\${i + 1}\`));
// Emulate Babel template literal transpilation
// 1. https://babeljs.io/repl#?browsers=ie%2011&code_lz=C4CgBglsCmC2B0ASA3hABAajQRgL5gEog
Array.from({ length: 3 }, (_, i) => t('item.'.concat(i + 1)));
// 2. https://babeljs.io/repl#?browsers=ie%2011&code_lz=C4CgBgZg9gTgtgZwHQBIDeBrApgTwL5JQB2WYAlEA
t("forms.".concat(key, ".one"));
t(["forms.".concat(key, ".one")]);
`;

it('returns keys', () => {
expect(getTranslationKeys(source)).to.deep.equal([
'explicit.call',
'comment.added.1',
'comment.added.2',
'comment.added.3',
'forms.button.submit',
'forms.messages',
'forms.key1',
'forms.key2',
'item.1',
'item.2',
'item.3',
]);
});
});
Expand Down

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

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import './resolved';

const text = t('forms.button.submit');
const message = t('forms.messages', { count: 2 });
const values = t(['forms.key1', 'forms.key2']);

// i18n-tasks-use t('item.1')
/* i18n-tasks-use t('item.2') */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ en:
submit: Submit
cancel: Cancel
reset: Reset
key1: value1-en
key2: value2-en
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ es:
submit: Enviar
cancel: Cancelar
reset: Reiniciar
key1: value1-es
key2: value2-es
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ fr:
button:
cancel: Annuler
reset: Réinitialiser
key1: value1-fr
key2: value2-fr
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function ForgotPassword({ goBack }: ForgotPasswordProps) {
/>
<PageHeading>{t('idv.forgot_password.modal_header')}</PageHeading>
<ul className="usa-list">
{(t('idv.forgot_password.warnings') as unknown as string[]).map((warning) => (
{t(['idv.forgot_password.warnings']).map((warning) => (
<li key={warning}>{warning}</li>
))}
</ul>
Expand Down