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] Load "en" translations parallely #3102

Merged
merged 19 commits into from Oct 25, 2023

Conversation

rajatvijay
Copy link
Contributor

@rajatvijay rajatvijay commented Jul 27, 2023

Relates #2927

Technical details

This PR does the following things -

  1. Hard code the language as en in the backend
  2. Load translations file in a script tag in index.html
  3. Injects translations in the window global
  4. Show the loader till the translations file is downloaded.

Loom
https://www.loom.com/share/9689a91822094772851277d61bd376c8

Checklist

  • My pull request has a descriptive title (not a vague title like Update index.md).
  • My pull request targets the develop branch of the repository
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added 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.

@rajatvijay rajatvijay added the pr-status: review A PR awaiting review label Jul 27, 2023
@rajatvijay rajatvijay added this to the v0.1.3 milestone Jul 27, 2023
@rajatvijay rajatvijay marked this pull request as ready for review July 27, 2023 04:37
Base automatically changed from i18n-django-templates-translatable to develop August 9, 2023 19:21
Copy link
Contributor

@seancolsen seancolsen left a comment

Choose a reason for hiding this comment

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

I have some questions about this code for @rajatvijay.

@pavish I'd like you review this as well since you understand this area of the app much better than me.

Also, I haven't tried running this in prod mode. I've been holding off on doing that while reviewing these PRs in hopes that someone would do it once all the i18n changes are in. But this work definitely needs some testing in prod mode by someone at some point. I'm not not sure when the best time to do that is, given how much is still in flux with the i18n work.

@@ -30,3 +30,11 @@ export async function loadLocaleAsync(locale: Locales): Promise<void> {
updateTranslationsDictionary(locale, await importLocaleAsync(locale));
loadFormatters(locale);
}

export function loadTranslationsIntoMemory(
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this function be named loadTranslations instead? The name loadTranslationsIntoMemory throws me off a bit because it's in the same file as other functions which seem to be also loading things into memory but are not named in a similar manner.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment on lines 27 to 43
const checkTranslationsInterval = setInterval(() => {
// eslint-disable-next-line prefer-destructuring
const translations:
| { lang: Locales; translationStrings: string }
| undefined =
// @ts-expect-error added by index.html
window.translations;
if (translations) {
loadTranslationsIntoMemory(
translations.lang,
JSON.parse(translations.translationStrings),
);
setLocale(translations.lang);
isTranslationsLoaded = true;
return true;
});
})();
clearInterval(checkTranslationsInterval);
}
}, 100);
Copy link
Contributor

Choose a reason for hiding this comment

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

This approach seems strange to me. If I understand correctly, in index.html we're importing the translation data and sticking it on window. Then here we are looking at window every 100ms to see if it has loaded. This seems really hacky. Why can't we do await import and let vite sort it out?

I wouldn't expect us to need to deal with window. But if we absolutely do, then I'd prefer to use a .d.ts file to provide our own typings for window.translation instead of the @ts-expect-error approach here.

Copy link
Contributor Author

@rajatvijay rajatvijay Sep 6, 2023

Choose a reason for hiding this comment

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

If we do await import vite will download App.svelte first & then start the download for the translations. The code here enables the parallel download for both of the things.

I have added the translations object in global.d.ts 855da7b

Copy link
Member

@pavish pavish Oct 16, 2023

Choose a reason for hiding this comment

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

With the latest set of changes, this approach of checking the data with setInterval is no longer used.

@seancolsen seancolsen assigned pavish and rajatvijay and unassigned seancolsen Aug 9, 2023
@rajatvijay rajatvijay modified the milestones: v0.1.3, v0.1.4 Aug 17, 2023
@pavish
Copy link
Member

pavish commented Aug 31, 2023

@rajatvijay Could you please resolve the conflicts in this PR?

@rajatvijay rajatvijay removed their assignment Sep 6, 2023
Copy link
Member

@pavish pavish left a comment

Choose a reason for hiding this comment

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

@rajatvijay I've added my review comments.

I'm not sure if you've tested the PR thoroughly before raising it. The changes do not work. Please ensure that you've tested any new changes in both development and production modes.

mathesar/utils/frontend.py Outdated Show resolved Hide resolved
mathesar/utils/frontend.py Outdated Show resolved Hide resolved
mathesar_ui/src/App.svelte Show resolved Hide resolved
mathesar/templates/mathesar/index.html Outdated Show resolved Hide resolved
config/context_processors.py Outdated Show resolved Hide resolved
@pavish pavish assigned rajatvijay and unassigned pavish Sep 7, 2023
@pavish pavish added pr-status: revision A PR awaiting follow-up work from its author after review and removed pr-status: review A PR awaiting review labels Sep 7, 2023
};
import { loadTranslations } from './i18n/i18n-load';

let isTranslationsLoaded = false;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

from @seancolsen - adding a comment here explaining "why this approach?" since this approach is weird.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added

Copy link
Member

@pavish pavish left a comment

Choose a reason for hiding this comment

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

@rajatvijay and I had a review call and these were the changes that were discussed.

  1. Move the window.translations initialization into the translation ts files.
  2. Remove promise from index.html and change it to <script type="module" src={src}>.
  3. Add legacy support back.
  4. Don't stringify translation strings.
  5. Use "Mathesar" instead of "mathesar".
  6. Remove '.js' extension in all i18n import statements.

(Optional):

  1. Namespace translation files if possible.
  2. Move objects that are only used to store translations from 'i18n-util' into a separate file.
  3. Use a single point of access to set and retrieve locale store values.

@pavish pavish assigned rajatvijay and unassigned pavish Sep 21, 2023
@pavish pavish added pr-status: revision A PR awaiting follow-up work from its author after review and removed pr-status: review A PR awaiting review labels Sep 21, 2023
@seancolsen seancolsen modified the milestones: v0.1.4, Next release Oct 2, 2023
@rajatvijay rajatvijay requested a review from pavish October 9, 2023 12:03
@rajatvijay rajatvijay assigned pavish and unassigned rajatvijay Oct 9, 2023
@rajatvijay rajatvijay added pr-status: review A PR awaiting review and removed pr-status: revision A PR awaiting follow-up work from its author after review labels Oct 9, 2023
Copy link
Member

@pavish pavish left a comment

Choose a reason for hiding this comment

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

@rajatvijay Looks good to me, approved! Thanks for addressing all my concerns.

@seancolsen Based on your comment here, I assume you might be interested in taking a look at this PR. I'm holding off merging for you and @rajatvijay to sort it out.

@pavish pavish assigned seancolsen and rajatvijay and unassigned pavish Oct 16, 2023
@pavish pavish added pr-status: revision A PR awaiting follow-up work from its author after review and removed pr-status: review A PR awaiting review labels Oct 16, 2023
@seancolsen seancolsen added this pull request to the merge queue Oct 25, 2023
Merged via the queue into develop with commit da201e0 Oct 25, 2023
38 of 41 checks passed
@seancolsen seancolsen deleted the i18n-load-en-translations-parallely branch October 25, 2023 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-status: revision A PR awaiting follow-up work from its author after review
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

3 participants