Use OS language to default localization #3018

Closed
peterjgrainger wants to merge 3 commits from master into master
peterjgrainger commented 2019-10-11 16:08:05 +02:00 (Migrated from github.com)

PR Checklist

Please check all that apply to this PR using "x":

  • I have checked that this PR is not a duplicate of an existing PR (open, closed or merged)
  • I have checked that this PR does not introduce a breaking change
  • This PR introduces breaking changes and I have provided a detailed explanation below

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting)
  • Refactoring (no functional changes)
  • Documentation changes
  • Other - Please describe:

Fixes

Issue Number: #2955

What is the current behavior?

When opening the app for the first time. The language is always English

What is the new behavior?

The language is set to the primary OS language on first use.

After that, the logic stays the same. Use the stored language in the window.localStorage

## PR Checklist Please check all that apply to this PR using "x": - [x] I have checked that this PR is not a duplicate of an existing PR (open, closed or merged) - [ ] I have checked that this PR does not introduce a breaking change - [ ] This PR introduces breaking changes and I have provided a detailed explanation below ## PR Type What kind of change does this PR introduce? - [ ] Bugfix - [x] Feature - [ ] Code style update (formatting) - [ ] Refactoring (no functional changes) - [ ] Documentation changes - [ ] Other - Please describe: ## Fixes Issue Number: #2955 ## What is the current behavior? When opening the app for the first time. The language is always English ## What is the new behavior? The language is set to the primary OS language on first use. After that, the logic stays the same. Use the stored language in the `window.localStorage`
peterjgrainger commented 2019-10-11 16:31:48 +02:00 (Migrated from github.com)

I'm new to the project (and electron) can whoever looks at this check I am on the right track?

I'm new to the project (and electron) can whoever looks at this check I am on the right track?
kauffj (Migrated from github.com) reviewed 2019-10-11 16:56:41 +02:00
kauffj (Migrated from github.com) left a comment

looks like the right direction

looks like the right direction
@ -0,0 +95,4 @@
event.preventDefault();
if (window.isFullScreen()) {
window.once('leave-full-screen', () => {
window.hide();
kauffj (Migrated from github.com) commented 2019-10-11 16:56:02 +02:00

If this is an async function that happens after loadURL, couldn't that create a race condition where some strings are rendered in English before the translation files are loaded?

If this is an async function that happens after `loadURL`, couldn't that create a race condition where some strings are rendered in English before the translation files are loaded?
@ -0,0 +141,4 @@
window.webContents.on('new-window', (event, url) => {
event.preventDefault();
shell.openExternal(url);
kauffj (Migrated from github.com) commented 2019-10-11 16:51:36 +02:00

good call to move language fetching here

good call to move language fetching here
kauffj (Migrated from github.com) commented 2019-10-11 16:54:14 +02:00

I'd like to avoid double hard-coding of these value (DRY violation).

Currently they are hard-coded in settingsLanguage/view.jsx

Can you abstract these to a shared location, presumably somewhere in ui/constants?

I'd like to avoid double hard-coding of these value (DRY violation). Currently they are hard-coded in settingsLanguage/view.jsx Can you abstract these to a shared location, presumably somewhere in `ui/constants`?
kauffj (Migrated from github.com) commented 2019-10-11 16:55:12 +02:00

I'm assuming this has to be done this way?

(I'm not an Electron expert either)

I'm assuming this has to be done this way? (I'm not an Electron expert either)
peterjgrainger (Migrated from github.com) reviewed 2019-10-11 16:59:00 +02:00
@ -0,0 +95,4 @@
event.preventDefault();
if (window.isFullScreen()) {
window.once('leave-full-screen', () => {
window.hide();
peterjgrainger (Migrated from github.com) commented 2019-10-11 16:59:00 +02:00

translations seem to happen on the fly. The original code was also Async.

My main issue that I had was the i18n.js file is loaded with the index and before the this file.

Not sure how to do it any other way :(

translations seem to happen on the fly. The original code was also Async. My main issue that I had was the `i18n.js` file is loaded with the `index` and before the this file. Not sure how to do it any other way :(
kauffj (Migrated from github.com) reviewed 2019-10-11 17:00:42 +02:00
@ -0,0 +95,4 @@
event.preventDefault();
if (window.isFullScreen()) {
window.once('leave-full-screen', () => {
window.hide();
kauffj (Migrated from github.com) commented 2019-10-11 17:00:42 +02:00

Hmm I may be misunderstanding how this file works then. I have a meeting with @seanyesmunt momentarily, let me check with him and we'll get an answer to you.

Hmm I may be misunderstanding how this file works then. I have a meeting with @seanyesmunt momentarily, let me check with him and we'll get an answer to you.
peterjgrainger (Migrated from github.com) reviewed 2019-10-11 17:02:23 +02:00
@ -0,0 +141,4 @@
window.webContents.on('new-window', (event, url) => {
event.preventDefault();
shell.openExternal(url);
peterjgrainger (Migrated from github.com) commented 2019-10-11 17:02:22 +02:00

Yeah, BrowserWindow doesn't have localStorage :(

Yeah, `BrowserWindow` doesn't have `localStorage` :(
kauffj (Migrated from github.com) reviewed 2019-10-11 18:06:35 +02:00
@ -0,0 +95,4 @@
event.preventDefault();
if (window.isFullScreen()) {
window.once('leave-full-screen', () => {
window.hide();
kauffj (Migrated from github.com) commented 2019-10-11 18:06:35 +02:00

I think you need to move setLanguage before loadURL and you will be fine, since you have an await on language fetch. By calling it after, I do think there is a chance some translations will fail. You should be able to confirm/test this by arbitrarily delaying the call to setLanguage or the duration of the time to download the translation file.

I think you need to move `setLanguage` before `loadURL` and you will be fine, since you have an `await` on language fetch. By calling it after, I do think there is a chance some translations will fail. You should be able to confirm/test this by arbitrarily delaying the call to `setLanguage` or the duration of the time to download the translation file.
tzarebczan commented 2019-10-15 15:51:17 +02:00 (Migrated from github.com)

@peterjgrainger, thanks for the PR! Looks like there are some additional comments for you to review, please take a peek.

Can we show you some appreciation for the contribution?

Also, we're giving away some Hacktoberfest bonuses and goodies for this month. We'll get it all figured out after you shoot us an email after this is reviewed/merged.

@peterjgrainger, thanks for the PR! Looks like there are some additional comments for you to review, please take a peek. Can we show you some [appreciation](https://lbry.com/faq/appreciation) for the contribution? Also, we're giving away some [Hacktoberfest](https://lbry.com/news/hacktoberfest-2019) bonuses and goodies for this month. We'll get it all figured out after you shoot us an email after this is reviewed/merged.
peterjgrainger (Migrated from github.com) reviewed 2019-10-15 17:23:14 +02:00
@ -0,0 +95,4 @@
event.preventDefault();
if (window.isFullScreen()) {
window.once('leave-full-screen', () => {
window.hide();
peterjgrainger (Migrated from github.com) commented 2019-10-15 17:23:14 +02:00

Only async funcitons will wait for the function to finish. Putting it before loadURL won't do anything I don't think.

I could put it in index.js? That is an async function? I'm not sure that delaying startup is the best option though!

Only async funcitons will wait for the function to finish. Putting it before loadURL won't do anything I don't think. I could put it in `index.js`? That is an async function? I'm not sure that delaying startup is the best option though!
kauffj (Migrated from github.com) reviewed 2019-10-17 00:07:12 +02:00
@ -0,0 +95,4 @@
event.preventDefault();
if (window.isFullScreen()) {
window.once('leave-full-screen', () => {
window.hide();
kauffj (Migrated from github.com) commented 2019-10-17 00:07:12 +02:00

@peterjgrainger it's possible I'm misunderstanding something.

What I'm fairly confident of is that if index.html loads and runs ui.js before the JSON translations are returned from lbry.com and stored in window.i18n_messages, then some translations could return in English.

Because you have an await in front of the translations download, I thought moving setLanguage up would be sufficient. Either way, the key is making sure the language resource is loaded and stored before ui.js runs.

This unfortunately does cause a ~100ms load hit, but avoiding it would be a substantial refactor that I don't think we can swallow right now (although if you're itching for more... 😁).

@peterjgrainger it's possible I'm misunderstanding something. What I'm fairly confident of is that if `index.html` loads and runs `ui.js` _before_ the JSON translations are returned from lbry.com and stored in `window.i18n_messages`, then some translations could return in English. Because you have an `await` in front of the translations download, I thought moving `setLanguage` up would be sufficient. Either way, the key is making sure the language resource is loaded and stored before `ui.js` runs. This unfortunately does cause a ~100ms load hit, but avoiding it would be a substantial refactor that I don't think we can swallow right now (although if you're itching for more... :grin:).
peterjgrainger (Migrated from github.com) reviewed 2019-10-17 12:40:18 +02:00
@ -0,0 +95,4 @@
event.preventDefault();
if (window.isFullScreen()) {
window.once('leave-full-screen', () => {
window.hide();
peterjgrainger (Migrated from github.com) commented 2019-10-17 12:40:18 +02:00

What I'm fairly confident of is that if index.html loads and runs ui.js before the JSON translations are returned from lbry.com and stored in window.i18n_messages, then some translations could return in English.

Yes agree completely. The translations definitely will be loaded after the UI starts to render. Looking again at the index it already adds this delay. 005aad6fdb/static/index-electron.html (L26-L29). So at least I wouldn't be slowing things down for non-english users.

Because you have an await in front of the translations download, I thought moving setLanguage up would be sufficient.

I'll have to relocate the code somewhere I can block the thread to stop ui.js running. I'll have to take a look.

This unfortunately does cause a ~100ms load hit, but avoiding it would be a substantial refactor that I don't think we can swallow right now

I'll split up the logic so it gets the language from localStorage and if it's English then doesn't do the fetch (like the current logic)

(although if you're itching for more... 😁).

Maybe we could speed this up by storing the translations in localStorage? Maybe for the scope of this change I'll keep it simple :)

> What I'm fairly confident of is that if index.html loads and runs ui.js before the JSON translations are returned from lbry.com and stored in window.i18n_messages, then some translations could return in English. Yes agree completely. The translations definitely will be loaded after the UI starts to render. Looking again at the index it already adds this delay. https://github.com/lbryio/lbry-desktop/blob/005aad6fdb83678c4e2093ee84da9c4a2848d11d/static/index-electron.html#L26-L29. So at least I wouldn't be slowing things down for non-english users. > Because you have an await in front of the translations download, I thought moving setLanguage up would be sufficient. I'll have to relocate the code somewhere I can block the thread to stop `ui.js` running. I'll have to take a look. > This unfortunately does cause a ~100ms load hit, but avoiding it would be a substantial refactor that I don't think we can swallow right now I'll split up the logic so it gets the language from `localStorage` and if it's English then doesn't do the `fetch` (like the current logic) > (although if you're itching for more... 😁). Maybe we could speed this up by storing the translations in `localStorage`? Maybe for the scope of this change I'll keep it simple :)
peterjgrainger (Migrated from github.com) reviewed 2019-10-17 12:41:39 +02:00
@ -0,0 +141,4 @@
window.webContents.on('new-window', (event, url) => {
event.preventDefault();
shell.openExternal(url);
peterjgrainger (Migrated from github.com) commented 2019-10-17 12:41:39 +02:00

@kauffj can you look at the latest code and see if your happy with how I did this?

@kauffj can you look at the latest code and see if your happy with how I did this?
peterjgrainger commented 2019-10-17 17:23:40 +02:00 (Migrated from github.com)

@kauffj Can you have a look at my new edits. I had to put some logic back in the index.electron.html but I think it's the only way to do it without a major rewrite.

Let me know what you think?

I tested it with Polish and German on the web and the desktop and the translations come through when in window.localStorage or from the OS.

@kauffj Can you have a look at my new edits. I had to put some logic back in the `index.electron.html` but I think it's the only way to do it without a major rewrite. Let me know what you think? I tested it with Polish and German on the web and the desktop and the translations come through when in `window.localStorage` or from the OS.
kauffj commented 2019-10-17 21:49:32 +02:00 (Migrated from github.com)

@peterjgrainger it passes the eyeball test. I suspect if we find any additional issues we can simply address them ourselves when we merge, which will possibly be tomorrow but is more likely to be next week.

Make sure to check out @tzarebczan's comments above and send us an email, we'd love to send you some LBC and a T-shirt.

@peterjgrainger it passes the eyeball test. I suspect if we find any additional issues we can simply address them ourselves when we merge, which will possibly be tomorrow but is more likely to be next week. Make sure to check out @tzarebczan's comments above and send us an email, we'd love to send you some LBC and a T-shirt.
peterjgrainger commented 2019-10-17 22:12:04 +02:00 (Migrated from github.com)

Thanks @kauffj it was fun!

Thanks @kauffj it was fun!
kauffj commented 2019-11-05 20:00:18 +01:00 (Migrated from github.com)

Closing in favor of #3180 but the commit history on that PR should still credit @peterjgrainger. Thanks for your work on this Peter!

Closing in favor of #3180 but the commit history on that PR should still credit @peterjgrainger. Thanks for your work on this Peter!

Pull request closed

Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: LBRYCommunity/lbry-desktop#3018
No description provided.