Search code examples
flutterflutter-dependenciesflutter-packages

How to link nested linked translations in "easy_translation" package for Flutter?


I have a JSON file which contains these translations:

{
  "copy" : {
    "v1" : "copy"
  },
  "receive" : {
    "v3" : "received"
  },
  "message" : "message",
  "received_message" : "@:receive.v3 @:message",
  "copy_received_message" : "@:copy.v1 @:received_message"
}

When I get copy_received_message translation by doing this:

 "copy_received_message".tr();

I expect to get string of "copy received message"

But, what I get is string of "copy @:receive.v3 @:message"



Footnote
I know I can write this translation on JSON file:

{
  ...
  "copy_received_message" : "@:copy.v1 @:receive.v3 @:message"
}

It seems simple in English, but in other language, received_message might not as simple as "@:receive.v3 @:message"

For the reference, the package website can be accessed on easy_localization


Solution

  • There is a similar open issue since Feb. 8. 2023: https://github.com/aissat/easy_localization/issues/553

    If you cannot wait until the fix, you can use the next snippet, but please don't give me any negative for the solution:

    extension StringExtension on String {
      String trHack() {
        return tr(tr(tr(tr(tr(tr(tr(tr(tr(this)))))))));
      }
    }
    

    And use it like this:

    "copy_received_message".trHack(); // "copy received message"
    

    I repeat this is a hack, and the console will show you warnings during debug!