Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Use JSON5 for en.json #1949

Merged
merged 20 commits into from
Nov 15, 2022
Merged

Use JSON5 for en.json #1949

merged 20 commits into from
Nov 15, 2022

Conversation

dhruvkb
Copy link
Member

@dhruvkb dhruvkb commented Oct 30, 2022

Fixes

Fixes #1815 by @dhruvkb

Description

This PR

  • replaces en.json with en.json5 and reformats it
  • adds a rudimentary JSON5 parser for the en.json5 file
    • could not find any parser that's capable of retaining comments when the JSON5 file is parsed so made a basic one specifically for reading i18n files
  • updates PO generation functionality to include comments
    • updates POT file to include all references to the i18n string
    • updates POT file to preserve the order of the en.json5 file
    • uses the right type of POT comment (#. vs #:)
  • updates scripts that used the en.json file
  • converts English en.json to be like any other language
    • added JSON5 → JSON conversion for English to i18n:get-translations script
  • makes general refactors, shortens code, adds docstrings with types, and cleans up code

Code in many places depends on en.json. This continues to work once the en.json file has been generated by running i18n:get-translations.

Screenshots

en.json5 openverse.pot
Screenshot 2022-10-30 at 6 11 15 PM Screenshot 2022-10-30 at 6 11 47 PM

Testing Instructions

  1. Check out the PR.
  2. Setup i18n pnpm i18n.
  3. Run the dev server and see that everything is translated as expected.

Additionally CI is passing and unit tests that depend on locales are passing. So it's good to start.

Checklist

  • My pull request has a descriptive title (not a vague title like Update index.md).
  • My pull request targets the default branch of the repository (main) or a parent feature branch.
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added or updated tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@dhruvkb dhruvkb added 🟩 priority: low Low priority and doesn't need to be rushed ✨ goal: improvement Improvement to an existing user-facing feature 💻 aspect: code Concerns the software code in the repository labels Oct 30, 2022
@github-actions
Copy link

github-actions bot commented Oct 30, 2022

Storybook and Tailwind configuration previews: Ready

Storybook: https://wordpress.github.io/openverse-frontend/_preview/1949
Tailwind: https://wordpress.github.io/openverse-frontend/_preview/1949/tailwind

Please note that GitHub pages takes a little time to deploy newly pushed code, if the links above don't work or you see old versions, wait 5 minutes and try again.

You can check the GitHub pages deployment action list to see the current status of the deployments.

@github-actions
Copy link

github-actions bot commented Oct 30, 2022

Size Change: +3 kB (0%)

Total Size: 817 kB

Filename Size Change
./.nuxt/dist/client/226.js 0 B -272 B (removed) 🏆
./.nuxt/dist/client/226.modern.js 0 B -277 B (removed) 🏆
./.nuxt/dist/client/227.js 0 B -1.85 kB (removed) 🏆
./.nuxt/dist/client/app.js 128 kB -3.15 kB (-2%)
./.nuxt/dist/client/app.modern.js 119 kB -3.15 kB (-3%)
./.nuxt/dist/client/commons/app.js 88.3 kB +194 B (0%)
./.nuxt/dist/client/commons/app.modern.js 77 kB +62 B (0%)
./.nuxt/dist/client/components/v-all-results-grid.js 5.15 kB +2.39 kB (+87%) 🆘
./.nuxt/dist/client/components/v-all-results-grid.modern.js 5.01 kB +2.29 kB (+84%) 🆘
./.nuxt/dist/client/components/v-all-results-grid/pages/search/audio/pages/search/index.js 0 B -3.02 kB (removed) 🏆
./.nuxt/dist/client/components/v-all-results-grid/pages/search/audio/pages/search/index.modern.js 0 B -2.92 kB (removed) 🏆
./.nuxt/dist/client/components/v-grid-skeleton.js 1.62 kB +10 B (+1%)
./.nuxt/dist/client/components/v-sources-table.js 15.7 kB +20 B (0%)
./.nuxt/dist/client/components/v-sources-table.modern.js 15.8 kB +23 B (0%)
./.nuxt/dist/client/pages/audio/_id.js 4.88 kB -12 B (0%)
./.nuxt/dist/client/pages/image/_id.js 5.25 kB -56 B (-1%)
./.nuxt/dist/client/pages/search/audio.js 3.76 kB +2.53 kB (+205%) 🆘
./.nuxt/dist/client/pages/search/audio.modern.js 3.65 kB +2.42 kB (+197%) 🆘
./.nuxt/dist/client/pages/search/index.js 509 B -2.46 kB (-83%) 🏆
./.nuxt/dist/client/pages/search/index.modern.js 513 B -2.42 kB (-83%) 🏆
./.nuxt/dist/client/runtime.js 2.56 kB -115 B (-4%)
./.nuxt/dist/client/runtime.modern.js 2.56 kB -116 B (-4%)
./.nuxt/dist/client/vendors/app.js 62.4 kB +5.18 kB (+9%) 🔍
./.nuxt/dist/client/vendors/app.modern.js 61.8 kB +5.29 kB (+9%) 🔍
./.nuxt/dist/client/207.js 272 B +272 B (new file) 🆕
./.nuxt/dist/client/207.modern.js 277 B +277 B (new file) 🆕
./.nuxt/dist/client/208.js 1.85 kB +1.85 kB (new file) 🆕
ℹ️ View Unchanged
Filename Size Change
./.nuxt/dist/client/components/loading-icon.js 745 B +1 B (0%)
./.nuxt/dist/client/components/loading-icon.modern.js 748 B -3 B (0%)
./.nuxt/dist/client/components/table-sort-icon.js 509 B 0 B
./.nuxt/dist/client/components/table-sort-icon.modern.js 513 B 0 B
./.nuxt/dist/client/components/v-audio-cell.js 357 B +1 B (0%)
./.nuxt/dist/client/components/v-audio-cell.modern.js 359 B 0 B
./.nuxt/dist/client/components/v-audio-details.js 1.81 kB 0 B
./.nuxt/dist/client/components/v-audio-details.modern.js 1.79 kB 0 B
./.nuxt/dist/client/components/v-audio-track-skeleton.js 1.01 kB +1 B (0%)
./.nuxt/dist/client/components/v-audio-track-skeleton.modern.js 1.01 kB -1 B (0%)
./.nuxt/dist/client/components/v-audio-track.js 5.13 kB +3 B (0%)
./.nuxt/dist/client/components/v-audio-track.modern.js 5.07 kB -2 B (0%)
./.nuxt/dist/client/components/v-back-to-search-results-link.js 537 B 0 B
./.nuxt/dist/client/components/v-back-to-search-results-link.modern.js 542 B +1 B (0%)
./.nuxt/dist/client/components/v-bone.js 685 B 0 B
./.nuxt/dist/client/components/v-bone.modern.js 690 B 0 B
./.nuxt/dist/client/components/v-box-layout.js 1.2 kB -1 B (0%)
./.nuxt/dist/client/components/v-box-layout.modern.js 1.2 kB +1 B (0%)
./.nuxt/dist/client/components/v-content-link.js 1.11 kB -2 B (0%)
./.nuxt/dist/client/components/v-content-link.modern.js 1.09 kB +2 B (0%)
./.nuxt/dist/client/components/v-content-page.js 467 B 0 B
./.nuxt/dist/client/components/v-content-page.modern.js 471 B 0 B
./.nuxt/dist/client/components/v-content-report-button.js 778 B +2 B (0%)
./.nuxt/dist/client/components/v-content-report-button.modern.js 784 B 0 B
./.nuxt/dist/client/components/v-content-report-form.js 3.76 kB +7 B (0%)
./.nuxt/dist/client/components/v-content-report-form.modern.js 3.56 kB +2 B (0%)
./.nuxt/dist/client/components/v-content-report-popover.js 4.41 kB +6 B (0%)
./.nuxt/dist/client/components/v-content-report-popover.modern.js 4.22 kB +3 B (0%)
./.nuxt/dist/client/components/v-copy-button.js 3.99 kB +2 B (0%)
./.nuxt/dist/client/components/v-copy-button.modern.js 4 kB +1 B (0%)
./.nuxt/dist/client/components/v-copy-license.js 999 B 0 B
./.nuxt/dist/client/components/v-copy-license.modern.js 1 kB 0 B
./.nuxt/dist/client/components/v-copy-license/components/v-error-image/components/v-media-reuse/components/v-search-grid/09090664.js 9.5 kB -1 B (0%)
./.nuxt/dist/client/components/v-copy-license/components/v-error-image/components/v-media-reuse/components/v-search-grid/09090664.modern.js 9.48 kB +2 B (0%)
./.nuxt/dist/client/components/v-dmca-notice.js 747 B -1 B (0%)
./.nuxt/dist/client/components/v-dmca-notice.modern.js 753 B +1 B (0%)
./.nuxt/dist/client/components/v-error-image.js 1.69 kB -2 B (0%)
./.nuxt/dist/client/components/v-error-image.modern.js 1.68 kB -2 B (0%)
./.nuxt/dist/client/components/v-error-section.js 372 B 0 B
./.nuxt/dist/client/components/v-error-section.modern.js 375 B -1 B (0%)
./.nuxt/dist/client/components/v-external-search-form.js 3.09 kB +4 B (0%)
./.nuxt/dist/client/components/v-external-search-form.modern.js 3.06 kB 0 B
./.nuxt/dist/client/components/v-external-source-list.js 2.55 kB +2 B (0%)
./.nuxt/dist/client/components/v-external-source-list.modern.js 2.52 kB 0 B
./.nuxt/dist/client/components/v-full-layout.js 1.49 kB +1 B (0%)
./.nuxt/dist/client/components/v-full-layout.modern.js 1.49 kB -3 B (0%)
./.nuxt/dist/client/components/v-grid-skeleton.modern.js 1.62 kB +6 B (0%)
./.nuxt/dist/client/components/v-image-cell-square.js 1.02 kB +1 B (0%)
./.nuxt/dist/client/components/v-image-cell-square.modern.js 1.02 kB -1 B (0%)
./.nuxt/dist/client/components/v-image-cell.js 1.43 kB 0 B
./.nuxt/dist/client/components/v-image-cell.modern.js 1.42 kB 0 B
./.nuxt/dist/client/components/v-image-details.js 1.44 kB -2 B (0%)
./.nuxt/dist/client/components/v-image-details.modern.js 1.44 kB +1 B (0%)
./.nuxt/dist/client/components/v-image-grid.js 2.54 kB +2 B (0%)
./.nuxt/dist/client/components/v-image-grid.modern.js 2.42 kB -3 B (0%)
./.nuxt/dist/client/components/v-license-tab-panel.js 521 B 0 B
./.nuxt/dist/client/components/v-license-tab-panel.modern.js 525 B 0 B
./.nuxt/dist/client/components/v-load-more.js 790 B 0 B
./.nuxt/dist/client/components/v-load-more.modern.js 682 B 0 B
./.nuxt/dist/client/components/v-media-license.js 800 B 0 B
./.nuxt/dist/client/components/v-media-license.modern.js 808 B +1 B (0%)
./.nuxt/dist/client/components/v-media-reuse.js 1.62 kB -4 B (0%)
./.nuxt/dist/client/components/v-media-reuse.modern.js 1.62 kB -2 B (0%)
./.nuxt/dist/client/components/v-media-tag.js 430 B 0 B
./.nuxt/dist/client/components/v-media-tag.modern.js 434 B 0 B
./.nuxt/dist/client/components/v-no-results.js 2.75 kB +3 B (0%)
./.nuxt/dist/client/components/v-no-results.modern.js 2.72 kB 0 B
./.nuxt/dist/client/components/v-radio.js 1.51 kB 0 B
./.nuxt/dist/client/components/v-radio.modern.js 1.47 kB +2 B (0%)
./.nuxt/dist/client/components/v-related-audio.js 1.23 kB +3 B (0%)
./.nuxt/dist/client/components/v-related-audio.modern.js 1.23 kB -5 B (0%)
./.nuxt/dist/client/components/v-related-images.js 3.1 kB +1 B (0%)
./.nuxt/dist/client/components/v-related-images.modern.js 2.98 kB -2 B (0%)
./.nuxt/dist/client/components/v-report-desc-form.js 965 B -2 B (0%)
./.nuxt/dist/client/components/v-report-desc-form.modern.js 964 B -1 B (0%)
./.nuxt/dist/client/components/v-row-layout.js 1.7 kB -2 B (0%)
./.nuxt/dist/client/components/v-row-layout.modern.js 1.71 kB +4 B (0%)
./.nuxt/dist/client/components/v-scroll-button.js 812 B -1 B (0%)
./.nuxt/dist/client/components/v-scroll-button.modern.js 818 B -1 B (0%)
./.nuxt/dist/client/components/v-search-grid.js 5.43 kB +6 B (0%)
./.nuxt/dist/client/components/v-search-grid.modern.js 5.38 kB -3 B (0%)
./.nuxt/dist/client/components/v-search-results-title.js 658 B 0 B
./.nuxt/dist/client/components/v-search-results-title.modern.js 651 B 0 B
./.nuxt/dist/client/components/v-search-type-radio.js 793 B -2 B (0%)
./.nuxt/dist/client/components/v-search-type-radio.modern.js 768 B -1 B (0%)
./.nuxt/dist/client/components/v-server-timeout.js 298 B -1 B (0%)
./.nuxt/dist/client/components/v-server-timeout.modern.js 303 B 0 B
./.nuxt/dist/client/components/v-sketch-fab-viewer.js 997 B 0 B
./.nuxt/dist/client/components/v-sketch-fab-viewer.modern.js 895 B 0 B
./.nuxt/dist/client/components/v-skip-to-content-container.js 888 B 0 B
./.nuxt/dist/client/components/v-skip-to-content-container.modern.js 894 B +1 B (0%)
./.nuxt/dist/client/components/v-snackbar.js 1.18 kB -2 B (0%)
./.nuxt/dist/client/components/v-snackbar.modern.js 1.19 kB -1 B (0%)
./.nuxt/dist/client/components/v-warning-suppressor.js 298 B 0 B
./.nuxt/dist/client/components/v-warning-suppressor.modern.js 303 B +1 B (0%)
./.nuxt/dist/client/pages/about.js 1.06 kB -1 B (0%)
./.nuxt/dist/client/pages/about.modern.js 1.07 kB 0 B
./.nuxt/dist/client/pages/audio/_id.modern.js 4.72 kB -3 B (0%)
./.nuxt/dist/client/pages/external-sources.js 1.45 kB 0 B
./.nuxt/dist/client/pages/external-sources.modern.js 1.46 kB +1 B (0%)
./.nuxt/dist/client/pages/feedback.js 1.24 kB 0 B
./.nuxt/dist/client/pages/feedback.modern.js 1.24 kB +1 B (0%)
./.nuxt/dist/client/pages/image/_id.modern.js 7.26 kB -4 B (0%)
./.nuxt/dist/client/pages/index.js 5 kB 0 B
./.nuxt/dist/client/pages/index.modern.js 4.86 kB 0 B
./.nuxt/dist/client/pages/preferences.js 1.21 kB +2 B (0%)
./.nuxt/dist/client/pages/preferences.modern.js 1.21 kB -2 B (0%)
./.nuxt/dist/client/pages/search-help.js 1.54 kB -1 B (0%)
./.nuxt/dist/client/pages/search-help.modern.js 1.55 kB 0 B
./.nuxt/dist/client/pages/search.js 2.72 kB -2 B (0%)
./.nuxt/dist/client/pages/search.modern.js 2.57 kB +2 B (0%)
./.nuxt/dist/client/pages/search/image.js 2.79 kB +1 B (0%)
./.nuxt/dist/client/pages/search/image.modern.js 2.67 kB -2 B (0%)
./.nuxt/dist/client/pages/search/model-3d.js 243 B 0 B
./.nuxt/dist/client/pages/search/model-3d.modern.js 246 B 0 B
./.nuxt/dist/client/pages/search/search-page.types.js 266 B +1 B (0%)
./.nuxt/dist/client/pages/search/search-page.types.modern.js 271 B +1 B (0%)
./.nuxt/dist/client/pages/search/video.js 239 B 0 B
./.nuxt/dist/client/pages/search/video.modern.js 243 B 0 B
./.nuxt/dist/client/pages/sources.js 1.44 kB -3 B (0%)
./.nuxt/dist/client/pages/sources.modern.js 1.45 kB -3 B (0%)

compressed-size-action

@WordPress WordPress deleted a comment from github-actions bot Oct 30, 2022
@dhruvkb dhruvkb marked this pull request as ready for review October 30, 2022 19:18
@dhruvkb dhruvkb requested a review from a team as a code owner October 30, 2022 19:18
@dhruvkb dhruvkb requested review from zackkrida and obulat October 30, 2022 19:18
Copy link
Contributor

@obulat obulat left a comment

Choose a reason for hiding this comment

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

Having comments in the POT files will be really great for translators! The file generation works well.

Does it also save the en.json to the branch that we use for translations? I'm not sure how to test that, but we definitely need to check (if you haven't yet) that it's working, and the translations are uploaded to GlotPress correctly.

Could you also add some documentation saying that en.json5 is the main file where we should put all the strings and comments, and en.json and all other translated files should not be edited?

I think we also need to add the en.json5 -> en.json conversion step to pnpm dev. en.json is git-ignored. So, when anyone downloads the repo and try to run pnpm dev, they will not have en.json, so will not be able to run the app. I tried removing all of the locale JSON files, and confirmed that pnpm dev cannot run without them.

One more thing: to make sure that the Playwright tests always use the same translation files, we saved 3 locale JSON files to tests/locales. When running the tests, we run i18n:copy-test-locales to copy them to the locales folder. Could you add en.json creation to that step, too, so that the tests always have the English locales as well?

@dhruvkb
Copy link
Member Author

dhruvkb commented Nov 10, 2022

Does it also save the en.json to the branch that we use for translations?

The WordPress/openverse-frontend@translations branch only has the POT file. So this PR doesn't add the en.json file to the branch.

Thanks @obulat for pointing out the usages I missed! I've made the other improvements you suggested.

package.json Show resolved Hide resolved
Comment on lines 1 to 22
/**
* In `en.json` we use a limited version of JSON5. It is valid JSON5 but only
* uses a limited set of features and syntax. This package provides functions
* to read this format and convert it into regular JSON.
*
* - All values must be strings. This is okay because we're only dealing with
* i18n translation files.
* - Only single line `// ...` comments are allowed. These comments describe the
* content that follows it. Inline comments are not allowed.
*
* ```json5
* {
* // documentation about `key-a`
* key: 'value',
* 'key-b': {
* 'key-c': 'value-c',
* },
* }
* ```
*
* @typedef {{ [key: string]: string | SimJson }} SimJson
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

This module is clever. Reading it, however, it struck me that you've essentially re-written a very basic version of a JavaScript AST parser. We could use instead use an existing AST parser and treat the json5 file as regular JavaScript and get the following benefits:

  1. No need to maintain a file parser
  2. Typed AST
  3. Support for multi-line comments

@babel/parser does a great job at all of these and is thoroughly documented. Check out this AST Explorer example: https://astexplorer.net/#/gist/fd2baa6817ccaa87c23704ead7d3e7f8/fb97d60a32d361ab1da42d9a19c88a2b22762733

You'll notice the object had to be wrapped in parens. This could be done in code by opening the file and adding an open and closing paren to the object. Alternatively, we could write the file as plain JavaScript and have it like so:

module.exports = {
  // ... the translation object

Instead of using the JSON5 extension but only supporting a limited subset of it.

If you click on the object properties in the AST explorer link, you'll see a leadingComments array that includes all the leading comments. Navigating through nested blocks is also easy, thanks to the uniform AST. We could transform this into the Entry AST for simplicity’s sake and to pare down the unnecessary parts of the babel AST: the goal of using babel's parser is mostly to avoid having to maintain our own AST parser 🙂

Finally, for generating the en.json, if we use json5 we can use the json5 reference implementation to get the comment-free version of the object. If we use JavaScript for the file instead, then we can just import the object and write it out as JSON without the need for an additional package.

None of these suggestions are blocking, but I am hesitant to approve without further clarification for why we should add a simplified JSON5 parser to our codebase.

Copy link
Member Author

@dhruvkb dhruvkb Nov 15, 2022

Choose a reason for hiding this comment

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

I searched everywhere for a JSON-AST package but didn't find any that supported JSON5 and had been actively maintained. The JSON5 parsers I found didn't have the option for reading to AST, only parsing it which strips all comments.

I hadn't considered the idea of treating the file as JS and using @babel/parser on it. Thank you for the suggestion! That might be worth investigating for the consistency of having a standard AST, although

  • the rudimentary parser is only a tiny bit of a maintenance burden because it's only a bunch of if statements
  • Babel's parser is overkill for a JSON file and then getting the information we want from the AST it outputs will also add a fair bit of code

For generating the en.json I didn't want to use the json5 parser because we already have all the information we need to write the JSON in the Entry object so it made sense to me to just use that to output the JSON.

To be honest, I'm being biased towards the hack parser because I spent a lot of time writing and testing it but I'm open to using a full-fledged parser like Babel if it improves the quality of the PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

I refactored read-i18n.js to use the @babel/parser package and it while the LoC didn't change much, the code looks much cleaner. Thanks again for the suggestion, wish I had asked you for pointers sooner!

@sarayourfriend
Copy link
Contributor

sarayourfriend commented Nov 14, 2022

Not necessarily requesting changes yet, just blocking the PR for further discussion.

Copy link
Contributor

@sarayourfriend sarayourfriend left a comment

Choose a reason for hiding this comment

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

LGTM! I had a comment typed out yesterday responding to some of your points (which I think are all pretty good) but the one I will still share is that yes, I think while it doesn't reduce the overall amount of code it is code that is (a) more readable and (b) more familiar to most people. Parsers are just a bit more niche compared to tree-walking code, which most people learn pretty early on. It's nice to support multi-line comments as well 😁 I appreciate your willingness to explore other avenues here 🚀

src/locales/scripts/read-i18n.js Show resolved Hide resolved
@dhruvkb dhruvkb merged commit ef8e656 into main Nov 15, 2022
@dhruvkb dhruvkb deleted the en_json5 branch November 15, 2022 22:54
github-actions bot pushed a commit that referenced this pull request Nov 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
💻 aspect: code Concerns the software code in the repository ✨ goal: improvement Improvement to an existing user-facing feature 🟩 priority: low Low priority and doesn't need to be rushed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using JSON5 for en.json
3 participants