slang-i18n / slang

Type-safe i18n for Dart and Flutter
https://pub.dev/packages/slang
MIT License
459 stars 39 forks source link

Allow escaping linked translations #248

Open Fasust opened 7 hours ago

Fasust commented 7 hours ago

I ran into an issue with linking translations and having them immediately followed by another word character.

Example:

{
  "common": {
    "nbsp": "\u00a0",
    "@nbsp": "No-Break Space https://unicode-explorer.com/c/00A0",
  },
   "message": "10@:common.nbspDays"
}

Message is linking to @:common.nbsp, but the link will be interpreted as @:common.nbspDays which is invalid. My PR adds the option to escape linked translations:

{
  "common": {
    "nbsp": "\u00a0",
    "@nbsp": "No-Break Space https://unicode-explorer.com/c/00A0",
  },
   "message": "10@:common.nbsp:@Days"
}
Tienisto commented 30 minutes ago

Hi, thanks for the PR! Do you know how other solutions handle this issue? This feature is inspired by linked translations of easy_localization but they probably followed the structure of Vue i18n.

I think it is better to handle it similar to how Dart handles interpolation ($ for greedy interpolation, ${} for edge cases). Therefore, the following syntax might be safer to use (e.g. when somebody wants to have an actual @ in the string right after the linked translation):

{
  "message": "10@:{common.nbsp}Days"
}

Here, I surrounded the path with {...} Let me know how you think about this. It might be a little bit more difficult to implement though.