Wrap components in a nested LangContext
that establishes a new
language stack. By default any children will be wrapped in a div
with
a lang=
property that indicates the language of the wrapped content.
Within this context any content which can't be translated into the requested
languages will have it's own lang=
property to reflect the fact that it
is in a different language than expected.
// Renders as <div lang="cy">....</div>
const Welsh: ComponentType<{ children: ReactNode }> = ({ children }) => (
<Translate lang="cy">{children}</Translate>
);
// Renders as <section lang="cy">....</section>
const WelshSection: ComponentType<{ children: ReactNode }> = ({ children }) => (
<Translate as="section" lang="cy">
{children}
</Translate>
);
Unlike TranslateLocal
Translate
always wraps the translated
content in an element with a lang=
property.
Wrap components in a nested LangContext
. Used to override settings in
the context. For example we can add an additional dictionary.
const Miscount = ({ children }: { children: ReactNode }) => {
// pretend one is three
const dict = { $$dict: { one: { en: "three" } } };
return <TranslateLocal dictionary={dict}>{children}</TranslateLocal>;
};
The root of a translation dictionary
the translations
optional metadata - ignored by Interminimal
Hook that gets the currently active translation context. Here's an example
of a component that wraps the Intl.DateTimeFormat
API using the translation
context.
const TDateFormat: ComponentType<{ date: Date }> = ({ date }) => {
// Get the context
const ctx = useTranslation();
// Use context's languages stack to find a format for our locale
const dtf = new Intl.DateTimeFormat(ctx.languages);
// Find out which language was matched...
const { locale } = dtf.resolvedOptions();
const ts = TString.literal(dtf.format(date), locale);
return <T text={ts} />;
};
the active translation context
A global empty locale stack which equals [].
Given a set of BCP 47 language tags and a list of locales in descending preference order find the tag that best satisfies the locale preference.
import { bestLocale } from "interminimal";
const tags = ["en", "en-GB", "fr", "fr-BE"];
console.log(bestLocale(tags, ["en-GB"])); // en-GB
console.log(bestLocale(tags, ["en-AU", "fr-BE"])); // en
console.log(bestLocale(tags, ["de", "fr-BE", "en"])); // fr-BE
console.log(bestLocale(tags, ["cy", "fr-BE-x-foo", "en"])); // fr-BE
console.log(bestLocale(tags, ["de", "de-AT"])); // undefined
an array of available language tags - order unimportant
an array of locales to match
a language tag or undefined
if no match found
Canonicalise a language tag. Canonicalisation is cached so don't
call this function on untrusted input. Use
safeCanonicaliseLanguage
for user input.
the language to canonicalise
the canonical version or undefined if tag is invalid
Canonicalise a list of languages.
console.log(
canonicaliseLocales(["en", "fr", "en", "de", "de", "fr", "cy", "de"])
);
// ["en", "fr", "de", "cy"]
Equivalent language lists canonicalise to the same array object and can therefore
be used as the key for a Map
or Set
.
the list of languages to canonicalise
the canonical language stack
Parse an HTTP Accept-Language header. Badly formed languages are dropped, languages are canonicalised.
const stack = parseAcceptLanguage("fr;b=9,en-GB;q=0.9,en-AU;q=0.8");
console.log(stack); // [ "en-GB", "en-AU" ]
the contents of the header
a priority ordered language stack
Return the stack that is the result of prepending a (possibly empty) list of locales to a locale stack
const ls1 = resolveLocales(localeRoot, ["en", "fr"]);
const ls2 = resolveLocales(ls1, ["fr"]); // now ["fr", "en"]
const ls3 = resolveLocales(localeRoot, ["fr", "en"]); // also ["fr", "en"]
if (ls2 === ls3) console.log("Same thing!");
a list of langs to prepend to the stack
a stack node with the prepended langs
Canonicalise a language tag. No caching - safe to use on user input.
the language to canonicalise
the canonical version or undefined if tag is invalid
Properties for the <T>
component.
Dictionary type guard: checks that an object looks like a dictionary.
Specifically does it have a $$dict
key?
maybe a dictionary
true if d
looks like a dictionary
Create a new component that behaves like <T>
but with a different default
as
element.
const Toption = tBind("option");
// later
return <Toption value="1" tag="one" />
It's also possible to wrap React components.
const TImage = tBind(Image as FunctionComponent);
The need for the cast is ugly. Not sure how to fix that. PRs welcome...
The generated components are cached - so whenever you call tBind("p")
you
will get the same component.
Generated using TypeDoc
A wrapper for content that should be translated. It attempts to translate the content you give it according to the active
LangContext
. It can translate content looked up in the translation dictionary and fat strings (orTString
s).It can optionally perform template substitution on the translated text, allowing child components to render portions of the translated text with arbitrary wrappers.
By default translated text is wrapped in a
span
. Render a different element using theas
property.If the wrapped content can't be translated into the context's preferred language it will have a
lang=
property specifying its actual language.The simplest usage is to render translatable content without template substition:
Template substitution allows you to build whole component trees from a translated string:
You can also look up and translate dictionary tags:
See Using T for more examples.