Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flutter How to translate text snippet that comes from backend (api) (i.e. translation as key) #104249

Open
gtu-myowin opened this issue May 20, 2022 · 9 comments
Labels
a: internationalization Supporting other languages or locales. (aka i18n) c: new feature Nothing broken; request for a new capability d: api docs Issues with https://api.flutter.dev/ framework flutter/packages/flutter repository. See also f: labels. P3 Issues that are less important to the Flutter project team-framework Owned by Framework team triaged-framework Triaged by Framework team

Comments

@gtu-myowin
Copy link

I guess, I just wanted to confirm that flutter doesn't support translation string as key. I have followed "Introduction to localizations in Flutter" tutorial and checked out packages like "Easy Localization".

Defining a key for a string in every language is a pain, time consuming and impossible to translate strings that comes from backend api. What I want to achieved is something that is possible in Laravel. Is that possible in Flutter yet?

Thanks in advance!

This question was asked on Stackoverflow a month ago but without any relevant answers.

@darshankawar darshankawar added the in triage Presently being triaged by the triage team label May 20, 2022
@darshankawar
Copy link
Member

Can you check if your case is same as #67092 ?

@darshankawar darshankawar added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 20, 2022
@gtu-myowin
Copy link
Author

I don't think the feature request you mentioned is related with mine. Let's say I have the translation json file with

 { "Hello": "မင်္ဂလာပါ" }

I will only display "Hello" in my app in proper way and flutter must handle to show "Hello" text on English language devices and "မင်္ဂလာပါ" on Myanmar (Burmese) language devices and the text "Hello" can also be a static hardcoded text or comes from API.

@github-actions github-actions bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 20, 2022
@leoshusar
Copy link

OT: Just curious, how is defining keys more painful than using whole sentences as keys?
I always use keys with fast_i18n package (now rebranded to slang), it generates static keys for me and I don't have to worry I messed up a character or something, and mainly I don't have to hardcode english texts anywhere (I really hate unnecessary strings in code). Also I have all texts in one file and when I want to change anything, I exactly know where it is.
What if you want to change the english hardcoded key? You have to change it in all translations, right?

And for translating backend messages.
First I am also sending keys, but this would work even for non keys, if you cannot modify API.
Then I define my translations like this:

...
"some_text": "Some text.",
"api_codes": {
  "some_error": "Some error.",
  "other_error": "Other error.",
  "Your non key sentence.": "Your non key sentence."
}
...

and tell slang to generate api_codes as a dictionary.
Then in code I can use it like:

final translatedText = i18n.someText; //returns already translated string
final translatedApiResponse = i18n.apiCodes[backendResponseCode];

I've once got an old PHP eshop to maintain for a few weeks (never done PHP before) and they wanted to change some strings. Fun times, searching through that ugly code, modifying those hardcoded strings and then also modifying them in translation mappings :)

@gtu-myowin
Copy link
Author

What if you want to change the english hardcoded key? You have to change it in all translations, right?

Not unless you want more than 2 languages in your app. That's the main reason I want this feature. According to my example, I would only need one .arb file, let's say app_my.arb. In this file, I would define

 { "Hello": "မင်္ဂလာပါ" }

As you might guess, it shouldn't need to translate from English to English. So, displaying the text Hello for English user isn't really localizing and the text Hello being as a key, would never need to change because saying hello in English is always be Hello.

But let's say you want to add Spanish to your app and your app would now have 3 languages, English (primary), Burmese and Spanish. Only then, you need another .arb file (e.g, app_es.arb), in which you would define

 { "Hello": "Hola" }

Then in the place of Hello text in your app, English users will see Hello, Burmese users will see မင်္ဂလာပါ and Spanish users will see Hola. How cool it would be? It would also enable flutter developers to use dynamic string as a key.

@leoshusar
Copy link

I would still hate seeing this anywhere in my codebase:

Text('This is your profile. Here you can manage your subscriptions, get in touch with your friends and etc.');
// de.json
"This is your profile. Here you can manage your subscriptions, get in touch with your friends and etc.": "Dies ist Ihr Profil. Hier können Sie Ihre Abonnements verwalten und mit Ihren Freunden und usw. in Verbindung setzen."
// es.json
"This is your profile. Here you can manage your subscriptions, get in touch with your friends and etc.": "Este es tu perfil. Aquí puede administrar sus suscripciones, ponerse en contacto con sus amigos y etc."

when I can have it clean like this:

Text(i18n.profileDescription);
// en.json
"profile_description": "This is your profile. Here you can manage your subscriptions, get in touch with your friends and etc."
// de.json
"profile_description": "Dies ist Ihr Profil. Hier können Sie Ihre Abonnements verwalten und mit Ihren Freunden und usw. in Verbindung setzen."
// es.json
"profile_description": "Este es tu perfil. Aquí puede administrar sus suscripciones, ponerse en contacto con sus amigos y etc."

But hey, your code, just my two cents!

@darshankawar
Copy link
Member

Maybe check this issue which looks a bit similar ? #84326

@darshankawar darshankawar added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 23, 2022
@gtu-myowin
Copy link
Author

@leoshusar glad you get my point and thank you for your two cents.

I would still hate seeing this anywhere in my codebase:

  1. Most of the time, the translation text are not long like your example
  2. My proposal is not a replacement for any of existing localization methods. I am requesting for additional feature. So, your favorite method of localization is safe.
  3. My proposal is something that is proven because it has been using in Laravel framework since I know Laravel which is almost a decade.
  4. It only needs one translation file if your app has only 2 languages (most of the time, we just need 2 languages) and can update both translations in one line (won't need to jump around multiple files to update a translation)

@github-actions github-actions bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 23, 2022
@gtu-myowin
Copy link
Author

@darshankawar Thank you for your effort to merge similar issues/feature requests but #84326 isn't even similar to mine. If you need more information, kindly go through all the links I provided in first thread.

@darshankawar
Copy link
Member

Thanks for the update. I am going ahead and labeling it for further insights from the team.

@darshankawar darshankawar added framework flutter/packages/flutter repository. See also f: labels. a: internationalization Supporting other languages or locales. (aka i18n) documentation and removed in triage Presently being triaged by the triage team labels May 23, 2022
@goderbauer goderbauer added c: new feature Nothing broken; request for a new capability P6 labels May 25, 2022
@flutter-triage-bot flutter-triage-bot bot added P3 Issues that are less important to the Flutter project and removed P6 labels Jun 28, 2023
@flutter-triage-bot flutter-triage-bot bot added the d: api docs Issues with https://api.flutter.dev/ label Jul 5, 2023
@flutter-triage-bot flutter-triage-bot bot added team-framework Owned by Framework team triaged-framework Triaged by Framework team labels Jul 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a: internationalization Supporting other languages or locales. (aka i18n) c: new feature Nothing broken; request for a new capability d: api docs Issues with https://api.flutter.dev/ framework flutter/packages/flutter repository. See also f: labels. P3 Issues that are less important to the Flutter project team-framework Owned by Framework team triaged-framework Triaged by Framework team
Projects
None yet
Development

No branches or pull requests

5 participants