easy_localization icon indicating copy to clipboard operation
easy_localization copied to clipboard

[fix] Small fix on Translations.get

Open jonasbadstuebner opened this issue 3 years ago • 3 comments

I got an error like this:

Flutter Error:  type '_Map<String, dynamic>' is not a subtype of type 'String?'

from Translations.get. I had a JSON for translation like

{
  "error": {
    "unknown": "An unknown error occurred",
    "unauthenticated": "You need to log in first"
  }
}

etc.

My code was trying to translate like tr(errorString) where errorString was "error" and this led to the lookup in lib/src/translations.dart#L16 to try and put the resulting Map (

{
  "unknown": "An unknown error occurred",
  "unauthenticated": "You need to log in first"
}

) into a String?.

This PR fixes the problem and just gives [🌎 Easy Localization] [WARNING] Localization key [error] not found as expected.

I did not check in any other places, if this could happen. If an error occurs somewhere else, I will open a new PR.

By the way, when will you release the next time? The last release on pub.dev was 10 months ago...?

Summary by CodeRabbit

  • Bug Fixes
    • Hardened translation retrieval to return only text values from top-level entries, avoiding non-string data appearing in the UI. This results in more reliable localization output and reduces edge-case rendering issues when translation maps contain mixed types. Nested key lookups and caching behavior remain unchanged, preserving existing behavior while improving correctness of returned strings. No user-facing settings or workflows were altered.

jonasbadstuebner avatar Mar 16 '23 10:03 jonasbadstuebner

@DrBu7cher Thanks for this PR, a very good idea! Maybe we should add a dedicated warning-log in this case, so the user knows what's wrong? Anyways, it would be important to have a unittest covering this, so it's not destroyed by a another change.

bw-flagship avatar May 16 '23 07:05 bw-flagship

Good ideas, I will come back to that.

jonasbadstuebner avatar Mar 12 '24 18:03 jonasbadstuebner