Language Support in Tooltips (#468)

* Add lang parameter to TextCompoennt

* Add 'mclang' key

* Add clearing old language cache when changed language

* Fetch language in background

* Remove auto-fetching mc language

* wrap mclang with a state

* update contributos

* Move 'mclang' to config.json

* Cleanup code to get mclang key and fix caching

---------

Co-authored-by: Misode <misoloo64@gmail.com>
This commit is contained in:
efekos
2024-02-24 23:55:41 +03:00
committed by GitHub
parent 7a7c7d675e
commit 231ecfe9c2
6 changed files with 51 additions and 29 deletions

View File

@@ -1,5 +1,6 @@
import type { BlockDefinitionProvider, BlockFlagsProvider, BlockModelProvider, BlockPropertiesProvider, ItemStack, TextureAtlasProvider, UV } from 'deepslate/render'
import { BlockDefinition, BlockModel, Identifier, ItemRenderer, TextureAtlas, upperPowerOfTwo } from 'deepslate/render'
import config from '../Config.js'
import { message } from '../Utils.js'
import { fetchLanguage, fetchResources } from './DataFetcher.js'
import type { VersionId } from './Schemas.js'
@@ -167,20 +168,22 @@ export type Language = Record<string, string>
const Languages: Record<string, Language | Promise<Language>> = {}
export async function getLanguage(version: VersionId) {
if (!Languages[version]) {
Languages[version] = (async () => {
export async function getLanguage(version: VersionId, lang: string = 'en') {
const mcLang = config.languages.find(l => l.code === lang)?.mc ?? 'en_us'
const cacheKey = `${version}_${mcLang}`
if (!Languages[cacheKey]) {
Languages[cacheKey] = (async () => {
try {
Languages[version] = await fetchLanguage(version)
return Languages[version]
Languages[cacheKey] = await fetchLanguage(version, mcLang)
return Languages[cacheKey]
} catch (e) {
console.error('Error: ', e)
throw new Error(`Cannot get language for version ${version}: ${message(e)}`)
throw new Error(`Cannot get language '${mcLang}' for version ${version}: ${message(e)}`)
}
})()
return Languages[version]
return Languages[cacheKey]
}
return Languages[version]
return Languages[cacheKey]
}
export function getTranslation(lang: Language, key: string, params?: string[]) {