localize

This is an alternative to getLocalizedString that can be used with tagged templates. Using this function, you can localize template strings without running getLocalizedString on all the static strings. It will automatically replace the dynamic values with $n, where n is the index of the value, and then use getLocalizedString to translate the string.

In this example, the string that is translated by Appframe is "You are on page $0 of $1". If the user session has UI culture "nb-NO", the translated string will then be "Du er på side $0 av $1", where localize will replace $0 with the value of page, and $1 with the value of pageCount.

import { localize } from "@olenbetong/appframe-core";
import { usePaging } from "@olenbetong/appframe-react";

function CurrentPageText() {
  let { page, pageCount } = usePaging(dsMyDataObject);
  
  return localize`You are on page ${page} of ${pageCount}.`;
}

If this method was accidentally used as a normal function, e.g. localize(`Page ${page} of ${pageCount}`) instead of localize`Page ${page} of ${pageCount}` , the base string table could potentially be spammed with strings like "Page 1 of 5", "Page 2 of 5", "Page 3 of 5" etc. To avoid this, there is some type checking of the parameters, and a TypeError will be thrown if the arguments don't match the expected arguments of a tag function.

Last updated

Was this helpful?