function - Is i possible to call t() instead of I18n.t() in javascript? -
i using i18n-js load locales in js app. when use translate function in view able t('hello')
instead of i18n.t('hello')
. how can such alias work?
import i18n 'i18n'; const hello = () => { return ( <div>{i18n.t("hello", {name: "john"})}</div> ) }
you could assign t
function window
:
window.t = i18n.t;
then can use t('hello')
. however, @thilo mentioned, may result in complications.
a better alternative write small wrapper:
function t(key){ return i18n.t(key); }
the best option, suggested @deceze, single line:
var t = i18n.t.bind(i18n);
single-line, , should work regardless of scope it's called in.
Comments
Post a Comment