Getting BuildContext in Flutter for localization

后端 未结 4 1905
名媛妹妹
名媛妹妹 2021-02-13 17:00

I try to localize a String in Flutter with the localization package. The problem is the location where my translation is needed. It is not related to the UI, rather it is somewh

相关标签:
4条回答
  • 2021-02-13 17:31

    I am not sure if i did it right (from performance point of view) and maybe someone can comment on this but i have rx BehaviorSubject in my AppLocalization and fire event once new locales are loaded. I am listening to it in my main.dart and doing setState on receiving an event. I checked performance tab but did not noticed any big changes in it once comparing my method vs accessing translations through context (inherited widget).

    0 讨论(0)
  • 2021-02-13 17:33

    No, there is no other way because it is stored using an InheritedWidget, which is a part of the build tree and thus can only be accessed with a reference to it (the BuildContext).
    You will need to pass your context to somewhere deep in your model.

    0 讨论(0)
  • 2021-02-13 17:33

    I know this question is dated way back. But I came across this issue when implementing my application, and I dont see any "nice" way to handle it. So here is my approach

    class LanguageService {
      static String defaultLanguage = 'en';
    
      static Map<String, Map<String, String>> _localizedValues = {
        'en': {
          'title': 'Storefront',
          'language': 'Language',
          'googleLogin': 'Login with Google'
        },
        'vn': {
          'title': 'Cửa hàng',
          'language': 'Ngôn ngữ',
          'googleLogin': 'Đăng Nhập với Google'
        }
      };
    
      static set language(String lang) {
        defaultLanguage = lang;
      }
    
      static String get title {
        return _localizedValues[defaultLanguage]['title'];
      }
    
      static String get language {
        return _localizedValues[defaultLanguage]['language'];
      }
    
      static String get googleLogin {
        return _localizedValues[defaultLanguage]['googleLogin'];
      }
    
    }
    

    Now you can reference a string as follows:

    String title = LanguageService.title;
    

    You can find the detailed tutorial here

    0 讨论(0)
  • 2021-02-13 17:47

    Yes there is. You don't need BuildContext to access strings. Here is my solution:

    class Strings {
      Strings._(Locale locale) : _localeName = locale.toString() {
        current = this;
      }
    
      final String _localeName;
    
      static Strings current;
    
      static Future<Strings> load(Locale locale) async {
        await initializeMessages(locale.toString());
        final result = Strings._(locale);
        return result;
      }
    
      static Strings of(BuildContext context) {
        return Localizations.of<Strings>(context, Strings);
      }
    
      String get title {
        return Intl.message(
          'Hello World',
          name: 'title',
          desc: 'Title for the Demo application',
        );
      }
    }
    
    Future<Null> main() async {
      final Locale myLocale = Locale(window.locale);
      await Strings.load(myLocale);
      runApp(MyApplication());
    }
    

    Now you can reference a string as follows:

    final title = Strings.current.title;
    
    0 讨论(0)
提交回复
热议问题