Added internationalization of factory names
This commit is contained in:
61
components/contexts/FactoryProvider/index.tsx
Normal file
61
components/contexts/FactoryProvider/index.tsx
Normal file
@@ -0,0 +1,61 @@
|
||||
import { createContext, FC, useContext, useMemo } from 'react'
|
||||
import { EnrichedEntity } from '../../../src/types'
|
||||
import { ReactNodeLike } from 'prop-types'
|
||||
import { factories } from './prepare'
|
||||
import { useLocale } from '../../../src/hooks/useLocale'
|
||||
import de from '../../../res/translation-de.json'
|
||||
import nl from '../../../res/translation-nl.json'
|
||||
import { Dict } from '../../../src/types'
|
||||
|
||||
const factoryNames: Record<'de' | 'nl', Dict<string>> = {
|
||||
de,
|
||||
nl
|
||||
}
|
||||
|
||||
interface Props {
|
||||
children: ReactNodeLike
|
||||
}
|
||||
|
||||
interface FactoryContextType {
|
||||
factories: EnrichedEntity[]
|
||||
findFactory(uid: string): EnrichedEntity | undefined
|
||||
}
|
||||
|
||||
const defaultValues: FactoryContextType = {
|
||||
factories: [],
|
||||
findFactory() {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
const FactoryContext = createContext<FactoryContextType>(defaultValues)
|
||||
export const useFactories = () => useContext(FactoryContext)
|
||||
|
||||
export const FactoryProvider: FC<Props> = ({ children }) => {
|
||||
const locale = useLocale()
|
||||
const internationalizedFactories = useMemo(() => {
|
||||
if (locale !== 'en')
|
||||
factories.map(
|
||||
factory => (factory.name = factoryNames[locale]?.[factory.href] ?? factory.name)
|
||||
)
|
||||
return factories
|
||||
}, [locale])
|
||||
|
||||
const findFactory = useMemo(() => {
|
||||
const detailsMap = Object.fromEntries(
|
||||
factories.map((detail: EnrichedEntity) => [detail.href, detail])
|
||||
)
|
||||
return (uid: string): EnrichedEntity | undefined => {
|
||||
return detailsMap[uid]
|
||||
}
|
||||
}, [])
|
||||
|
||||
const value: FactoryContextType = useMemo(
|
||||
() => ({
|
||||
factories: internationalizedFactories,
|
||||
findFactory
|
||||
}),
|
||||
[findFactory, internationalizedFactories]
|
||||
)
|
||||
return <FactoryContext.Provider value={value}>{children}</FactoryContext.Provider>
|
||||
}
|
||||
19
components/contexts/FactoryProvider/prepare.ts
Normal file
19
components/contexts/FactoryProvider/prepare.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { EnrichedEntity, Entity } from '../../../src/types'
|
||||
import details from '../../../res/details.json'
|
||||
import manual from '../../../res/manual.json'
|
||||
import exclude from '../../../res/exclude.json'
|
||||
|
||||
const manualEntities = manual as Entity[]
|
||||
const manualKeys = new Set(manualEntities.map(entity => entity.href))
|
||||
const detailEntities = (details as Entity[]).filter(detail => !manualKeys.has(detail.href))
|
||||
|
||||
const joined = [...detailEntities, ...manualEntities]
|
||||
|
||||
export const factories = joined
|
||||
.map((detail: EnrichedEntity) => {
|
||||
detail.usedBy = joined.filter(f =>
|
||||
Object.keys(f.recipe?.prerequisites ?? {}).includes(detail.href)
|
||||
)
|
||||
return detail
|
||||
})
|
||||
.filter(detail => !(exclude as string[]).includes(detail.href))
|
||||
Reference in New Issue
Block a user