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

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -