Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Supporting multiple languages for constant strings in Flutter

Tags:

flutter

I would like to start putting all my constant strings (like labels etc.) into a place that can be translated at a later stage.

How is this handled in Flutter?

like image 725
juliusspencer Avatar asked May 20 '17 04:05

juliusspencer


People also ask

How do you support multiple languages in flutter?

By default, Flutter only provides US English localizations. To add support for other languages, an application must specify additional MaterialApp (or CupertinoApp ) properties, and include a package called flutter_localizations .

How do you add multi language support to your flutter app via dynamic localization?

Just click on Tools > Flutter Intl > Add Locale. This will open a popup where you can write the name of the . arb file for the particular language. Let's add support for the Hindi language, so enter.

How does localization work in flutter?

Flutter localization uses ARB (Application Resource Bundle) files to house its translations by default. These are simple files written in JSON syntax. At the very least, we need a template file that corresponds to our default locale (English in our case). We specified that our template file will be lib/l10n/app_en.


2 Answers

  1. Create a Localizations.dart file

  2. Add the following code to that file:


import 'dart:async';
import 'package:flutter/material.dart';

import 'package:flutter/foundation.dart' show SynchronousFuture;
class DemoLocalizations {
  DemoLocalizations(this.locale);
  final Locale locale;
  static DemoLocalizations of(BuildContext context) {
    return Localizations.of<DemoLocalizations>(context, DemoLocalizations);
  }
  static Map<String, Map<String, String>> _localizedValues = {
    'en': {
      'title': 'App title',
      'googleLogin': 'Login with Google'
    },
    'es': {
      'title': 'Título de App',
      'googleLogin': 'Conectar con Google'
    },
  };
  String get title {
    return _localizedValues[locale.languageCode]['title'];
  }
  String get googleLogin {
    return _localizedValues[locale.languageCode]['googleLogin'];
  }
}
class DemoLocalizationsDelegate extends LocalizationsDelegate<DemoLocalizations> {
  const DemoLocalizationsDelegate();
  @override
  bool isSupported(Locale locale) => ['en', 'es'].contains(locale.languageCode);

  @override
  Future<DemoLocalizations> load(Locale locale) {
    // Returning a SynchronousFuture here because an async "load" operation
    // isn't needed to produce an instance of DemoLocalizations.
    return new SynchronousFuture<DemoLocalizations>(new DemoLocalizations(locale));
  }
  @override
  bool shouldReload(DemoLocalizationsDelegate old) => false;
}

  1. Import Localizations.dart into the file where you use the strings.

  2. Add the delegate DemoLocalizationsDelegate in the MaterialApp

MaterialApp(
  localizationsDelegates: [
   MyLocalizationsDelegate(),
  ],
...
)
  1. Substitute new Text("App Title"), with new Text(DemoLocalizations.of(context).title),

For each new string you want to localize, you need to add the translated text to each language's map and then add the String get... line. It's a bit cumbersome but it does what you need it to.

This is a quick overview of one way of doing it. You can read more about it in the Flutter docs: https://flutter.io/tutorials/internationalization/

like image 85
AlexL Avatar answered Sep 20 '22 23:09

AlexL


I asked on gitter and I got the following:

Translation/Internationalization isn't a feature we consider "done" yet. https://pub.dartlang.org/packages/intl works in Flutter. We have a bug tracking this more generally: flutter/flutter#393

More complete internationalization (i18n) and accessibility support are two of the big arcs of work ahead of Flutter in the coming months. Another example of i18n work we have planned, is completing Right-to-left (RTL) layouts for our provided Widgets (e.g. teaching the Material library's Scaffold to place the Drawer on the left when the locale is an RTL language). RTL Text support works today, but there are no widgets which are out-of-the-box RTL-layout aware at this moment.

like image 27
juliusspencer Avatar answered Sep 18 '22 23:09

juliusspencer