getTranslate function
Retrieves a translated string for the given key
.
This function checks if the global snackBarGlobalKey
has a valid context.
If not, it logs a message and returns a fallback warning message.
If a translation is found for the key
, it returns the translated string.
If not, it logs a warning and returns the key
as a fallback.
Make sure you attach snackBarGlobalKey
to your MaterialApp
:
MaterialApp(
scaffoldMessengerKey: snackBarGlobalKey,
...
)
Example:
String text = getTranslate("title");
Returns the translated string or the key itself if not found.
Implementation
String getTranslate(String key) {
if (snackBarGlobalKey.currentContext == null) {
logFile(
message: 'add snack bar key to material app',
name: "get translate",
);
return 'add snack bar key to material app';
}
String? translatedText = AppLocale.getTranslated(key);
if (translatedText != null) {
return translatedText;
} else {
logFile(
message: 'Translation not found for key: $key',
name: "get translate",
);
return key;
}
}