synacor / preact-i18n

Simple localization for Preact.
BSD 3-Clause "New" or "Revised" License
205 stars 18 forks source link

Fallback behaviour: only render fallback text if key is undefined or null? #49

Closed rubenkuipers closed 3 years ago

rubenkuipers commented 3 years ago

Would it be possible to only render the fallback when the translation key value is undefined or null?

<Text id="general.messages.onSuccess.title">Fallback</Text>

In my usecase sometimes a user wants to set an empty string to hide the text. So the key would be defined but empty..

Or is there another way for me to hook into the fallback functionality to customize this behaviour?

pl12133 commented 3 years ago

You should be able to make a wrapper around <Text> that accomplishes this by looking up the text with useText and then manually falling back:

import { useText, Text } from 'preact-i18n';

function ModifiedFallbackText({ children, ...props }) {
  const { text } = useText({ text: <Text {...props} /> });
  return typeof text === 'undefined' ? children : text;
}
rubenkuipers commented 3 years ago

@pl12133 Allright thanks, that could be a solution indeed. Thanks for now :)