klaro-js icon indicating copy to clipboard operation
klaro-js copied to clipboard

Help understanding i18n.js t function

Open timohausmann opened this issue 4 years ago • 1 comments

Hey, thanks for open sourcing this project!

I'm currently adding a bit of customization to this codebase.

I want to support multilanguage, but I struggle to understand the i18n.js t function because it has no documentation and is kinda cryptic with the hget etc.

My main question is what does the '!' as first parameter mean? Should I use it? The '?' at end probably means "optional"?

Also, sometimes lang is passed in, sometimes not. Why? Should I always pass it in for multilanguage?

(In general I'd be in favor of using an i18n lib. I also tried to change language on the fly, but is it possible without klaro.show() (reset state)?)

https://github.com/kiprotect/klaro/blob/ec6e36934db10afdac0183721ddfbcb9c79e7dc3/src/utils/i18n.js#L78

Here are a few examples:

https://github.com/kiprotect/klaro/blob/ec6e36934db10afdac0183721ddfbcb9c79e7dc3/src/components/consent-modal.jsx#L29

https://github.com/kiprotect/klaro/blob/ec6e36934db10afdac0183721ddfbcb9c79e7dc3/src/components/consent-notice.jsx#L94

https://github.com/kiprotect/klaro/blob/ec6e36934db10afdac0183721ddfbcb9c79e7dc3/src/components/service-item.jsx#L30

https://github.com/kiprotect/klaro/blob/ec6e36934db10afdac0183721ddfbcb9c79e7dc3/src/components/contextual-consent-notice.jsx#L37

timohausmann avatar Aug 12 '21 13:08 timohausmann

Starting a translation string with '!'

t(['!', 'privacyPolicyUrl'], {lang: lang}) This will result in the function returning undefined if privacyPolicyUrl is not set. Otherwise it will return the string [missing translation: de/privacyPolicyUrl]

https://github.com/kiprotect/klaro/blob/ec6e36934db10afdac0183721ddfbcb9c79e7dc3/src/utils/i18n.js#L92-L93

Ending a property with '?'

t(['!', 'purposes', purpose, 'title?']) I think this will make it hget look up an optional sub-property. e.g. look up purposes.marketing.title, but if that's undefined use purposes.marketing, allowing for both these configs:

{
  purposes: {
   marketing: {
     title: 'Marketing',
     description: 'Analytics for our Marketing team'
   }
}
{
  purposes: {
   marketing: 'Marketing',
  }
}

jaller94 avatar Jan 12 '22 12:01 jaller94