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

[i18n] Remove i18n token skipping from EUI pluralization defString functions #129144

Merged
merged 3 commits into from
Apr 1, 2022

Conversation

cee-chen
Copy link
Contributor

Summary

This PR handles the pluralization functions in EUI's i18n tokens (see #128825 (comment)). I was hoping to open this PR in after #128825 merged but security tests are being flakey so I'm just going to open this now and rebase later. I did run these tests against all the new EuiSuperDatePicker tokens and can confirm they pass/work as expected.

This PR closes #110132 by correctly checking that the output of EUI's function defStrings output the same strings as i18n's pluralization handling. This requires:

  • evaling EUI's passed function strings
  • running the actual i18n.translate util to get final output

Example:

EUI: ({ duration }) => `Last ${duration} second${duration === 1 ? '' : 's'}`
Kibana: 'Last {duration, plural, one {# second} other {# seconds}}'

If `duration` is 1, both should evaluate to `Last 1 second`
If `duration` is 2, both should evaluate to `Last 2 seconds`

Checklist

…ana output

+ add comments/catches for possible future function cases that aren't pluralization
- caught by new unit tests. hooray!
@cee-chen cee-chen requested review from afharo and Bamieh March 31, 2022 21:05
@cee-chen cee-chen requested review from a team as code owners March 31, 2022 21:05
@cee-chen cee-chen added v8.3.0 release_note:skip Skip the PR/issue when compiling release notes labels Mar 31, 2022
@@ -966,7 +966,7 @@ export const getEuiContextMapping = (): EuiTokensObject => {
),
'euiSelectable.searchResults': ({ resultsLength }: EuiValues) =>
i18n.translate('core.euiSelectable.searchResults', {
defaultMessage: '{resultsLength, plural, one {# result} other {# results}}',
defaultMessage: '{resultsLength, plural, one {# result} other {# results}} available',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The new tests caught this default message mistake from a few upgrades ago. Hooray! :)

Comment on lines +119 to +122
throw new Error(
`We currently only have logic written for EUI pluralization def functions.
This is a new type of def function that will need custom logic written for it.`
);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can't imagine when EUI will ever add function interpolation that isn't for pluralization (see #110132 (comment) to confirm), but if we ever do, I thought it would be prudent to leave this thrown error in for us so that whoever is upgrading the EUI version catches it

// pluralization). To check EUI's pluralization against Kibana's pluralization, we
// need to eval the defString and then actually i18n.translate & compare the 2 outputs
const defFunction = eval(defString); // eslint-disable-line no-eval
const defFunctionArg = normalizedDefString.split('({ ')[1].split('})')[0]; // TODO: All EUI pluralization fns currently only pass 1 arg. If this changes in the future and 2 args are passed, we'll need to do some extra splitting by ','
Copy link
Contributor Author

@cee-chen cee-chen Mar 31, 2022

Choose a reason for hiding this comment

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

Just want to highlight this TODO - I'm not sure when we would ever pass multiple args into an EUI pluralization fn, but if we ever do in the future this logic will need to be reworked because all I'm essentially doing here is isolating the text in '({ someVar })' to get a single variable name

@@ -17,6 +17,7 @@ import { getEuiContextMapping } from './i18n_eui_mapping';
const VALUES_REGEXP = /\{\w+\}/;

describe('@elastic/eui i18n tokens', () => {
const i18nTranslateActual = jest.requireActual('@kbn/i18n').i18n.translate;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted this to actually compare final string outputs to each other as I didn't think it was as useful / readable to try and compare the EUI output to some contrived pre-pluralization Kibana version.

Copy link
Member

@afharo afharo left a comment

Choose a reason for hiding this comment

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

This is great! Thank you for doing this! 🧡 LGTM!

@cee-chen
Copy link
Contributor Author

cee-chen commented Apr 1, 2022

Thanks for the speedy review @afharo! I'll aim to merge by EOD Friday to give @Bamieh some time to look at this and leave any feedback!

@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
core 309.1KB 309.1KB +10.0B
Unknown metric groups

ESLint disabled line counts

id before after diff
core 48 49 +1

Total ESLint disabled count

id before after diff
core 58 59 +1

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

Copy link
Member

@Bamieh Bamieh left a comment

Choose a reason for hiding this comment

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

LGTM. Thank you this is very valuable to have in Kibana after the date picker changes

@cee-chen
Copy link
Contributor Author

cee-chen commented Apr 1, 2022

Thanks y'all!

@cee-chen cee-chen merged commit 68630e4 into elastic:main Apr 1, 2022
@cee-chen cee-chen deleted the 110132/i18n-eui-mapping branch April 1, 2022 15:01
@kibanamachine
Copy link
Contributor

Friendly reminder: Looks like this PR hasn’t been backported yet.
To create backports run node scripts/backport --pr 129144 or prevent reminders by adding the backport:skip label.

@kibanamachine kibanamachine added backport missing Added to PRs automatically when the are determined to be missing a backport. backport:skip This commit does not require backporting and removed backport missing Added to PRs automatically when the are determined to be missing a backport. labels Apr 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting release_note:skip Skip the PR/issue when compiling release notes v8.3.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[EUI i18n token mapping] Long-term approach for complex defStrings
5 participants