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 parser to support console-extensions.json #8363

Merged
merged 2 commits into from Mar 12, 2021

Conversation

christianvogt
Copy link
Contributor

@christianvogt christianvogt commented Mar 10, 2021

Dynamic plugins contribute extensions through the console-extensions.json. To localize a string in json, the string must match the format /%.+%/.
eg.

{
  "foo": "%bar%"
}

This PR adds a custom lexer which parses *.json files and matches string values against the localization pattern.

Added unit test for custom JSON lexer.

Update the i18n-scripts/.eslintrc.js rules to reuse those defined in the console eslint plugin.

cc @rohitkrai03 @vojtechszocs @rebeccaalpert @spadgett

Before this change we would require a comment to pick up the localized string. Although there is no lexer currently for json files.

  [
    {
      "type": "AddAction",
      "flags": {
        "required": ["OPENSHIFT_HELM"]
      },
      "properties": {
        "id": "helm",
        "url": "/catalog?catalogType=HelmChart",
        // t('helm-plugin~Helm Chart')
        "label": "%helm-plugin~Helm Chart%",
        // t('helm-plugin~Browse the catalog to discover and install Helm Charts')
        "description": "%helm-plugin~Browse the catalog to discover and install Helm Charts%"
      }
    }
  ]

After this change, the lexer will pickup the string without the comment:

  [
    {
      "type": "AddAction",
      "flags": {
        "required": ["OPENSHIFT_HELM"]
      },
      "properties": {
        "id": "helm",
        "url": "/catalog?catalogType=HelmChart",
        "label": "%helm-plugin~Helm Chart%",
        "description": "%helm-plugin~Browse the catalog to discover and install Helm Charts%"
      }
    }
  ]

locale file: helm-plugin.json

{
  "Helm Chart": "Helm Chart",
  "Browse the catalog to discover and install Helm Charts": "Browse the catalog to discover and install Helm Charts"
}

@openshift-ci-robot openshift-ci-robot added the kind/i18n Indicates issue or PR relates to internationalization or has content that needs to be translated label Mar 10, 2021
@openshift-ci-robot openshift-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Mar 10, 2021
Copy link
Contributor

@rebeccaalpert rebeccaalpert left a comment

Choose a reason for hiding this comment

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

Looks good to me. Just one comment about a console.log that looked like it may not be intentional. Thanks for cleaning up the linter file and making some of the other scripts easier to read.

JSON.parse(content, (key, value) => {
if (typeof value === 'string') {
const match = value.match(/%(.+)%/);
console.log('testing:', match[1]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Was this console.log intentional?

Copy link
Contributor

Choose a reason for hiding this comment

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

Most likely it's just to test the lexer as it was developed, I think we can remove it now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed

globals: {
process: 'readonly',
'no-console': 'off',
// fs.promises requires a newer version of node however our compliance is set to node >=10
Copy link
Contributor

Choose a reason for hiding this comment

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

FYI once #7306 is merged, our compliance will be Node.js >= 14.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

right, but not there yet :P

),
);
)
.catch((e) => console.error(e, fileName));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
.catch((e) => console.error(e, fileName));
.catch((e) => console.error(fileName, e));

We can print context-specific bits (like fileName) before printing error & its stack trace.

language,
),
)
.catch((e) => console.error(e, fileName));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
.catch((e) => console.error(e, fileName));
.catch((e) => console.error(fileName, e));

try {
JSON.parse(content, (key, value) => {
if (typeof value === 'string') {
const match = value.match(/%(.+)%/);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const match = value.match(/%(.+)%/);
const match = value.match(/^%(.+)%$/);

JSON.parse(content, (key, value) => {
if (typeof value === 'string') {
const match = value.match(/%(.+)%/);
console.log('testing:', match[1]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Most likely it's just to test the lexer as it was developed, I think we can remove it now.

gettextToI18next(language, fs.readFileSync(fileName)).then(save(newFilePath));
gettextToI18next(language, fs.readFileSync(fileName))
.then(save(newFilePath))
.catch((e) => console.error(e, fileName));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
.catch((e) => console.error(e, fileName));
.catch((e) => console.error(fileName, e));

});
fs.promises
.writeFile(fileName, JSON.stringify(updatedFile, null, 2))
.catch((e) => console.error(e));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
.catch((e) => console.error(e));
.catch((e) => console.error(fileName, e));

@christianvogt
Copy link
Contributor Author

@vojtechszocs thanks for the review. I've updated all the suggestions.

@christianvogt
Copy link
Contributor Author

updated lexer parser to use comment-json to properly parse json files with comments as this is supported by dynamic plugin extensions files.

@vojtechszocs
Copy link
Contributor

/lgtm

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Mar 11, 2021
@openshift-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: christianvogt, rebeccaalpert, vojtechszocs

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@rohitkrai03
Copy link
Contributor

/retest

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-merge-robot openshift-merge-robot merged commit 5f9508b into openshift:master Mar 12, 2021
@spadgett spadgett added this to the v4.8 milestone Mar 18, 2021
@christianvogt christianvogt deleted the i18n-json branch June 2, 2021 19:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. component/sdk Related to console-plugin-sdk kind/i18n Indicates issue or PR relates to internationalization or has content that needs to be translated lgtm Indicates that a PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants