Ir al contenido

Reading texts

View .md

Esta página aún no está disponible en tu idioma.

Binds a translation tree to one language and returns useLang:

const useLang = createUseLang('de', translations)
useLang() // the whole tree in German
useLang((all) => all.login) // only the `login` subtree
  • language must be one of languages (a type error, and a runtime error for unknown values).
  • The scope function must return a subtree (an object), not a single text. Read single texts from the result: useLang((all) => all.login).title.
  • Results are cached per subtree and language and frozen. Calling useLang again returns the same object, so it is cheap to call often and safe as a React dependency.

createUseLang has a fixed language. To switch languages at runtime, call it again with the new language, or use an adapter:

  • Vue: useLang() returns a ComputedRef that follows a ref or getter.
  • React: useLang() reads the language from LangProvider.