getLocalizedValue method

String getLocalizedValue(
  1. String key
)

Retrieves a specific localized string by it's value from the JSON file.

Retrieving values from a JSON file could result in errors. In order to provide feedback if something goes wrong while retrieving, feedback strings are returned. These strings are displayed on the UI instead of the requested localized string.

If one of these cases occur, feedback strings are returned or exceptions are thown:

  • The JSON file is not found or its content is accessed while the parsing is ongoing.

  • The provided key-value pair has not been implemented in the JSON file.

  • The requested language is not implemented in the JSON string, but the language was added to the supportedLanguages list on the delegate instance.

Implementation

String getLocalizedValue(String key) {
  if (_hasError) {
    if (debug) {
      print("LitLocalizationService: ERROR - JSON file not found");
    }
    throw Exception(
        "ERROR - JSON file not found or parsing still ongoing. Have you called the localized string before the LitLocalizationController has completed the parsing?\nPlease check your JSON location and ensure you have awaited the LitLocalizationController's loadFromAsset result on a FutureBuilder before calling localized strings.");
  } else {
    if (_keyValueImplemented(key)) {
      if (_languageImplemented(key)) {
        // The localized value will always be wrapped inside a string object to avoid any
        // exceptions to be displayed on the UI.
        return "${_localizationController.localizedString![key][locale.languageCode]}";
      } else {
        if (debug) {
          print(
              "LitLocalizationService: ERROR - Localization on '$key' for '${locale.languageCode}' not implemented");
        }
        return "ERROR: Localization on '$key' for '${locale.languageCode}' not implemented";
      }
    } else {
      if (debug) {
        print(
            "LitLocalizationService: ERROR - Key-value pair on '$key' not found");
      }
      return "ERROR: Key-value pair on '$key' not found";
    }
  }
}