getStringForLocale function

String getStringForLocale(
  1. String tag,
  2. String locale, {
  3. String? plural,
  4. int length = 0,
  5. Map<String, String>? params,
})

Implementation

String getStringForLocale(
  String tag,
  String locale, {
  String? plural,
  int length = 0,
  Map<String, String>? params,
}) {
  // Store current locale
  final currentLocale = Get.locale;

  // Temporarily switch to target locale
  Get.updateLocale(Locale(locale));

  // Get translation in target locale
  String translation;
  if (plural != null) {
    if (params != null) {
      translation = tag.trPluralParams(plural, length, params);
    } else {
      translation = tag.trPlural(plural, length);
    }
  } else if (params != null) {
    translation = tag.trParams(params);
  } else {
    translation = tag.tr;
  }

  // Restore original locale
  Get.updateLocale(currentLocale!);

  return translation;
}