Skip to content

fix: rename messages.ts to messages.js to fix translation extraction #1801

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

Closed

Conversation

igobranco
Copy link
Contributor

Renamed all messages.ts files to messages.js to fix the translation extraction process using fedx-scripts.js and formatjs tool. This change increases the number of extracted strings, improving localization.

Description

Currently the translation extract process uses the fedx-scripts.js that calls the formatjs tool.

The fedx-scripts.js assumes that all the i18n messages are on .js file extension.

By just changing the file extension on those files the extract translation process is fixed. Then more strings are sent to Transifex and this MFE is better localized for each language.

Before it extracts/ detect 1730 strings to be translated:

$ npm run i18n_extract && grep '"id"' temp/babel-plugin-formatjs/Default.messages.json | wc -l

> @edx/[email protected] i18n_extract
> fedx-scripts formatjs extract

Running with resolved config:
/home/ibranco/projects/openedx/frontend-app-authoring/node_modules/@openedx/frontend-build/config/babel.config.js

1730

After extracts/ detect 2309 strings to be translated:

$ npm run i18n_extract && grep '"id"' temp/babel-plugin-formatjs/Default.messages.json | wc -l

> @edx/[email protected] i18n_extract
> fedx-scripts formatjs extract

Running with resolved config:
/home/ibranco/projects/openedx/frontend-app-authoring/node_modules/@openedx/frontend-build/config/babel.config.js

[@formatjs/cli] [WARN] [FormatJS CLI] Duplicate message id: "course-authoring.course-libraries.tab.home.description-no-links", but the `description` and/or `defaultMessage` are different.[@formatjs/cli] [WARN] [FormatJS CLI] Duplicate message id: "course-authoring.library-authoring.component.advanced.olx-save", but the `description` and/or `defaultMessage` are different.[@formatjs/cli] [WARN] [FormatJS CLI] Duplicate message id: "course-authoring.library-authoring.create-library", but the `description` and/or `defaultMessage` are different.[@formatjs/cli] [WARN] [FormatJS CLI] Duplicate message id: "course-authoring.library-authoring.publish.error", but the `description` and/or `defaultMessage` are different.



2309

Supporting information

Execute the script to find how many strings are extracted:

 run i18n_extract && grep '"id"' temp/babel-plugin-formatjs/Default.messages.json | wc -l

Testing instructions

For example the message with id authoring.problemEditor.selectType.title wasn't extracted.
With this PR it extracts, you can confirm by running:

grep "authoring.problemEditor.selectType.title" temp/babel-plugin-formatjs/Default.messages.json

To simulate the sent and download from Transifex you can execute:

make pull_translations

Edit manually a language, example pt-pt locale, edit the file src/i18n/messages/frontend-app-course-authoring/pt_PT.json and add:

"authoring.problemEditor.selectType.title": "Selecionar o tipo de problema",

This will fix the header when editing problems:
image

Other information

Related to: #1798

Renamed all messages.ts files to messages.js to fix the translation extraction process using fedx-scripts.js and formatjs tool. This change increases the number of extracted strings, improving localization.
@igobranco igobranco self-assigned this Apr 10, 2025
@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Apr 10, 2025
@openedx-webhooks
Copy link

Thanks for the pull request, @igobranco!

This repository is currently maintained by @openedx/2u-tnl.

Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review.

🔘 Get product approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
🔘 Provide context

To help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.


Where can I find more information?

If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:

When can I expect my changes to be merged?

Our goal is to get community contributions seen and reviewed as efficiently as possible.

However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

Copy link

codecov bot commented Apr 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.48%. Comparing base (04faf54) to head (b973bef).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1801      +/-   ##
==========================================
- Coverage   93.51%   93.48%   -0.03%     
==========================================
  Files        1129     1129              
  Lines       22957    22867      -90     
  Branches     4862     4861       -1     
==========================================
- Hits        21468    21378      -90     
  Misses       1421     1421              
  Partials       68       68              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@brian-smith-tcril
Copy link
Contributor

Good catch! We definitely want to make sure we're not missing strings during the extraction process!

I'm less sure that this is the approach we should use to fix it though.

FormatJS supports message extraction from typescript files (docs), so I think a better solution would be to update the script in frontend-build to include .ts.

I think that would just mean updating this line

      '--', 'src/**/*.js*',

to be

      '--', 'src/**/*.{j,t}s*',

but I haven't tested that.

@igobranco
Copy link
Contributor Author

Good catch! We definitely want to make sure we're not missing strings during the extraction process!

I'm less sure that this is the approach we should use to fix it though.

FormatJS supports message extraction from typescript files (docs), so I think a better solution would be to update the script in frontend-build to include .ts.

I think that would just mean updating this line

      '--', 'src/**/*.js*',

to be

      '--', 'src/**/*.{j,t}s*',

but I haven't tested that.

@brian-smith-tcril
Yes, I think you are right. I tested adding your suggestion directly on the script, but I had to also skip the .d.ts files, ref: formatjs/formatjs#2044.

Edit the:

code  node_modules/.bin/fedx-scripts

Changed to

  case 'formatjs': {
    const commonArgs = [
      '--format', 'node_modules/@openedx/frontend-build/lib/formatter.js',
      '--ignore', 'src/**/*.json',
      '--ignore', 'src/**/*.d.ts',
      '--out-file', './temp/babel-plugin-formatjs/Default.messages.json',
      '--', 'src/**/*.{j,t}s*',
    ];

Then when I run it works and finds more translations:

 npm run i18n_extract && grep '"id"' temp/babel-plugin-formatjs/Default.messages.json | wc -l

> @edx/[email protected] i18n_extract
> fedx-scripts formatjs extract

Running with resolved config:
/home/ibranco/projects/openedx/frontend-app-authoring/node_modules/@openedx/frontend-build/config/babel.config.js

[@formatjs/cli] [WARN] [FormatJS CLI] Duplicate message id: "course-authoring.course-libraries.tab.home.description-no-links", but the `description` and/or `defaultMessage` are different.[@formatjs/cli] [WARN] [FormatJS CLI] Duplicate message id: "course-authoring.library-authoring.component.advanced.olx-save", but the `description` and/or `defaultMessage` are different.[@formatjs/cli] [WARN] [FormatJS CLI] Duplicate message id: "course-authoring.library-authoring.create-library", but the `description` and/or `defaultMessage` are different.[@formatjs/cli] [WARN] [FormatJS CLI] Duplicate message id: "course-authoring.library-authoring.publish.error", but the `description` and/or `defaultMessage` are different.



2309

When I included the plugins folder it increased to 2454 strings.

@igobranco
Copy link
Contributor Author

Replaced by openedx/frontend-build#650

@igobranco igobranco closed this Apr 10, 2025
@github-project-automation github-project-automation bot moved this from Needs Triage to Done in Contributions Apr 10, 2025
@bradenmacdonald
Copy link
Contributor

bradenmacdonald commented Apr 10, 2025

Thanks for finding a better way. I definitely don't want to see those renamed. In fact we should do a mass-rename of all messages.js to messages.ts because the syntax is identical but in the latter case, the TypeScript check during CI will warn if you've used any invalid message IDs in the React code (I've seen this happen), but the .JS version will silently have errors (no message will appear in the UI at all, if I remember correctly). Although you only get this benefit if the React code is in a .tsx file or has // @ts-check at the top of a .jsx file. (Plus you get code-autocompletion in VS Code when typing message IDs, which is very helpful!)

Screenshot 2025-04-10 at 10 17 55 AM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
open-source-contribution PR author is not from Axim or 2U
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants