detectLanguage static method

String detectLanguage()

Detect the current device language

Priority order:

  1. GetX locale (if set by user in app settings)
  2. Platform/Device locale (system language)
  3. English (fallback)

Implementation

static String detectLanguage() {
  String? languageCode;

  // First, try to get the locale from GetX (user's app preference)
  try {
    final getxLocale = Get.locale;
    if (getxLocale != null) {
      languageCode = getxLocale.languageCode.toLowerCase();

      // If GetX locale is supported, use it
      if (app_translations.Translations.isLanguageSupported(languageCode)) {
        return languageCode;
      }
    }
  } catch (e) {
    // GetX might not be initialized, continue to platform locale
  }

  // Second, fall back to platform/device locale
  final platformLocale = PlatformDispatcher.instance.locale;
  languageCode = platformLocale.languageCode.toLowerCase();

  // Check if the detected language is supported
  if (app_translations.Translations.isLanguageSupported(languageCode)) {
    return languageCode;
  }

  // Default to English if language not supported
  return 'en';
}