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

Determine language based on the OS' language [Rework] #262

Merged
merged 6 commits into from
Feb 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/interface/collections/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import simplifiedChinese from '../../../languages/chinese-simplified.json'
import greek from '../../../languages/greek.json'

export default {
default: { name: 'Default' },
english,
catalan,
classical_latin,
Expand Down
19 changes: 18 additions & 1 deletion src/interface/core/lang_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Languages from '../collections/languages'
import StaticConfig from 'StaticConfig'
import throwError from '../utils/throw_error'
import { PuffinState } from 'Types/puffin.state'
const globalAny: any = global

let initialTranslations = {}

Expand All @@ -28,7 +29,23 @@ function setFallback(notFoundLang: string): void {
}

StaticConfig.keyChanged('appLanguage', (newLanguage: string) => {
if (Languages[newLanguage]) {
if (Languages[newLanguage].name === 'Default') {
// Get the language part from the locale (e.g. en-GB -> en)
const newLocale = globalAny.navigator.language.substr(0, globalAny.navigator.language.indexOf('-'))
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be better to use this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale

Example:

const { language } = new Intl.Locale(globalThis.navigator.language)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using Intl.Locale returns Property 'Locale' does not exist on type 'typeof Intl'. Do you have any solutions for this in mind?

Copy link
Member

Choose a reason for hiding this comment

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

Using Intl.Locale returns Property 'Locale' does not exist on type 'typeof Intl'. Do you have any solutions for this in mind?

Where did you test it? It worked fine for me.

Copy link
Contributor Author

@midyh midyh Feb 8, 2021

Choose a reason for hiding this comment

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

Where did you test it? It worked fine for me.

I am currently testing on Ubuntu 20.10.

Edit: I just found a workaround. Using new (Intl as any).Locale(...) actually worked, so it's probably an error in the .d.ts file of Intl.

Copy link
Member

Choose a reason for hiding this comment

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

Where did you test it? It worked fine for me.

I am currently testing on Ubuntu 20.10.

I mean, where are you executing that code? Node? Electron? Firefox?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mean, where are you executing that code? Node? Electron? Firefox?

Electron.

Copy link
Member

Choose a reason for hiding this comment

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

I mean, where are you executing that code? Node? Electron? Firefox?

Electron.

Show me the error, please.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here is the console output:

↳✨ Watching in WatchWebpackRenderer
   ↳❌ [webpack] Error in: ./src/interface/core/lang_config.ts

 [tsl] ERROR in /home/user/Coding/Graviton-App/src/interface/core/lang_config.ts(33,33)
      TS2339: Property 'Locale' does not exist on type 'typeof Intl'.

And line 33:

const { language } = new Intl.Locale(globalThis.navigator.language)

Also, using (Intl as any).Locale(...) works normally.

Copy link
Member

Choose a reason for hiding this comment

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

Here is the console output:

↳✨ Watching in WatchWebpackRenderer
   ↳❌ [webpack] Error in: ./src/interface/core/lang_config.ts

 [tsl] ERROR in /home/user/Coding/Graviton-App/src/interface/core/lang_config.ts(33,33)
      TS2339: Property 'Locale' does not exist on type 'typeof Intl'.

And line 33:

const { language } = new Intl.Locale(globalThis.navigator.language)

Also, using (Intl as any).Locale(...) works normally.

Related to microsoft/TypeScript#37326 .

I think it would be better to just leave it as it is for now

// Used to detect if the language was found
let found = false

Object.keys(Languages).some(i => {
if (Languages[i].locale === newLocale) {
LanguageState.data.translations = Languages[i].translations
found = true
}
})

if (!found) {
setFallback(newLocale)
}
} else if (Languages[newLanguage]) {
LanguageState.data.translations = Languages[newLanguage].translations
} else {
//Fallback to english if the configured language is not found
Expand Down