Type of issue
Feature request
Problem
fusion-plugin-i18n currently only works with string literals:
// Works
<Translate id="homepage.hero" />
// Doesn't work
const key = "homepage.hero";
<Translate id={key} />
This is because there is a custom babel plugin that extracts these strings at build time.
This limitation, however, leads to some very verbose and bloated logic in some cases. For example, when pulling translations for days of the week our old approach:
return Locale.get(`time.weekdays.${day}`);
now turns into the following with Fusion
switch (dayNumber) {
case 1:
return <Translate id="time.weekdays.1" />;
case 2:
return <Translate id="time.weekdays.2" />;
case 3:
return <Translate id="time.weekdays.3" />;
case 4:
return <Translate id="time.weekdays.4" />;
case 5:
return <Translate id="time.weekdays.5" />;
case 6:
return <Translate id="time.weekdays.6" />;
case 7:
return <Translate id="time.weekdays.7" />;
default:
return '';
}
The custom babel transform is awesome and seems necessary for efficient code splitting, but I'm hoping there's a strategy which gives us the best of both worlds.
Potential Solution
One solution that comes to mind is a comment that acts as a directive to the i18n babel transform.
<Translate id={
// fusion-i18n: time.weekdays.*
`time.weekdays.${id}`
} />
The transform would parse the comment and match it against keys found in the translation files. A asterisk wildcard match is probably sufficient, but in theory this could also be a regex if people wanted more flexibility.
This solution seems to offer the benefits of code splitting, without runtime overhead and code bloat.
Thoughts?