Skip to content

If device locale is not on the supported locales, it does not use fallback it gives an eror. #761

@arrrrny

Description

@arrrrny

Lets say user has device locale fr-FR which is not in the supported locales.

    runApp(
      EasyLocalization(
        supportedLocales: const [
          Locale('de'),
          Locale('en'),
          Locale('es'),
          Locale('pl'),
          Locale('ru'),
          Locale('tr'),
        ],
        path: 'assets/translations',
        fallbackLocale: const Locale('en', 'US'),
        startLocale: const Locale('fr', 'FR'),
        useFallbackTranslations: true,
        useOnlyLangCode: true,
        useFallbackTranslationsForEmptyResources: true,
        child: const App(),
      ),
    );

expected behaviour is to use en, US

however it throws an error instead.

Image Image

and here is the code:

abstract class AssetLoader {
  const AssetLoader();
  Future<Map<String, dynamic>?> load(String path, Locale locale);
}

///
/// default used is RootBundleAssetLoader which uses flutter's assetloader
///
class RootBundleAssetLoader extends AssetLoader {
  const RootBundleAssetLoader();

  String getLocalePath(String basePath, Locale locale) {
    return '$basePath/${locale.toStringWithSeparator(separator: "-")}.json';
  }

  @override
  Future<Map<String, dynamic>?> load(String path, Locale locale) async {
    var localePath = getLocalePath(path, locale);
    EasyLocalization.logger.debug('Load asset from $path');
    return json.decode(await rootBundle.loadString(localePath));
  }
}

There is no mechanism in place to check the supported locales first before checking assets folder

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions