Skip to content

[General] Improvements in language selector when installing game #1127

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

Merged
merged 1 commit into from
Mar 17, 2022
Merged
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
51 changes: 46 additions & 5 deletions src/screens/Library/components/InstallModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
install,
writeConfig
} from 'src/helpers'
import React, { useContext, useEffect, useState } from 'react'
import React, { useContext, useEffect, useMemo, useState } from 'react'

import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faFolderOpen, faXmark } from '@fortawesome/free-solid-svg-icons'
Expand Down Expand Up @@ -43,6 +43,24 @@ type Props = {

const storage: Storage = window.localStorage

function getInstallLanguage(
availableLanguages: string[],
preferredLanguages: readonly string[]
) {
const foundPreffered = preferredLanguages.find((plang) =>
availableLanguages.some((alang) => alang.startsWith(plang))
)
if (foundPreffered) {
const foundAvailable = availableLanguages.find((alang) =>
alang.startsWith(foundPreffered)
)
if (foundAvailable) {
return foundAvailable
}
}
return availableLanguages[0]
}

export default function InstallModal({
appName,
backdropClick,
Expand All @@ -52,6 +70,7 @@ export default function InstallModal({
storage.getItem(appName) || '{}'
) as InstallProgress

const { i18n } = useTranslation()
const { libraryStatus, handleGameStatus, platform } =
useContext(ContextProvider)
const gameStatus: GameStatus = libraryStatus.filter(
Expand Down Expand Up @@ -160,7 +179,9 @@ export default function InstallModal({
setGameInfo(gameInfo)
if (gameInfo.manifest?.languages) {
setInstallLanguages(gameInfo.manifest.languages)
setInstallLanguage(gameInfo.manifest.languages[0])
setInstallLanguage(
getInstallLanguage(gameInfo.manifest.languages, i18n.languages)
)
}
setIsLinuxNative(gameData.is_linux_native && isLinux)
setIsMacNative(gameData.is_mac_native && isMac)
Expand All @@ -170,7 +191,9 @@ export default function InstallModal({
appName
)) as string[]
setInstallLanguages(installer_languages)
setInstallLanguage(installer_languages[0])
setInstallLanguage(
getInstallLanguage(installer_languages, i18n.languages)
)
}
const regexp = new RegExp(/[:|/|*|?|<|>|\\|&|{|}|%|$|@|`|!|™|+|']/, 'gi')
const fixedTitle = gameInfo.game.title
Expand All @@ -181,7 +204,7 @@ export default function InstallModal({
setWinePrefix(sugestedWinePrefix)
}
getInfo()
}, [appName])
}, [appName, i18n.languages])

const haveDLCs = gameInfo?.game?.owned_dlc?.length > 0
const DLCList = gameInfo?.game?.owned_dlc
Expand Down Expand Up @@ -215,6 +238,24 @@ export default function InstallModal({
}
}

const getLanguageName = useMemo(() => {
return (language: string) => {
try {
const locale = language.replace('_', '-')
const displayNames = new Intl.DisplayNames(
[locale, ...i18n.languages, 'en'],
{
type: 'language',
style: 'long'
}
)
return displayNames.of(locale)
} catch (e) {
return language
}
}
}, [i18n.language])

return (
<span className="modalContainer">
{gameInfo?.game?.title ? (
Expand Down Expand Up @@ -255,7 +296,7 @@ export default function InstallModal({
{installLanguages &&
installLanguages.map((value) => (
<option value={value} key={value}>
{value}
{getLanguageName(value)}
</option>
))}
</select>
Expand Down