Skip to content
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
2 changes: 1 addition & 1 deletion .size-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module.exports = [
{
name: 'adapter-react',
path: 'react/index.min.mjs',
limit: '1618 b',
limit: '1604 b',
ignore: ['react'],
},
{
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

# Version 5

## 5.24.4 (2023-07-10)

Fix:
- avoid recomputing react context [#684](https://github.com/ivanhofer/typesafe-i18n/issues/684)

## 5.24.3 (2023-03-26)

Fix:
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Contributions are welcome. New ideas, improvements and optimizations are collect

This project should keep following values:

- lightweight and performat i18n solution
- lightweight and performant i18n solution
- best possible TypeScript support
- easy to use (DX)
- should work well together with other tools and services
Expand Down
21 changes: 11 additions & 10 deletions packages/adapter-react/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,18 @@ export const initI18nReact = <
const context = React.createContext({} as I18nContextType<L, T, TF>)

const component: React.FunctionComponent<TypesafeI18nProps<L>> = (props) => {
const [locale, _setLocale] = React.useState<L>(null as unknown as L)
const [LL, setLL] = React.useState<TF>(getFallbackProxy<TF>())
const [locale, setLocale] = React.useState<L>(props.locale)

const setLocale = (newLocale: L): void => {
_setLocale(newLocale)
setLL(() => i18nObject<L, T, TF, F>(newLocale, translations[newLocale], formatters[newLocale]))
}

!locale && setLocale(props.locale)

const ctx = { setLocale, locale, LL } as I18nContextType<L, T, TF>
const ctx = React.useMemo<I18nContextType<L, T, TF>>(
() => ({
setLocale,
locale,
LL: !locale
? getFallbackProxy<TF>()
: i18nObject<L, T, TF, F>(locale, translations[locale], formatters[locale]),
}),
[setLocale, locale, translations, formatters],
)

return <context.Provider value={ctx}>{props.children}</context.Provider>
}
Expand Down
11 changes: 10 additions & 1 deletion packages/adapter-solid/src/index.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { batch, createComponent, createContext, createSignal, useContext, type Accessor, type Component, type JSX } from 'solid-js'
import {
batch,
createComponent,
createContext,
createSignal,
useContext,
type Accessor,
type Component,
type JSX,
} from 'solid-js'
import { getFallbackProxy } from '../../runtime/src/core-utils.mjs'
import type { BaseFormatters, BaseTranslation, Locale, TranslationFunctions } from '../../runtime/src/core.mjs'
import { i18nObject } from '../../runtime/src/util.object.mjs'
Expand Down
9 changes: 7 additions & 2 deletions packages/runtime/src/index.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
export { type BaseTranslation, type FormattersInitializer, type LocaleTranslations, type LocalizedString, type RequiredParams } from './core.mjs'
export {
type BaseTranslation,
type FormattersInitializer,
type LocaleTranslations,
type LocalizedString,
type RequiredParams,
} from './core.mjs'
export { i18n, type LocaleTranslationFunctions } from './util.instance.mjs'
export { i18nObject, typesafeI18nObject } from './util.object.mjs'
export { i18nString, typesafeI18nString, type TranslateByString } from './util.string.mjs'