How to create a Offline Internationalization App:Support multiple languages

nasawz

nasa.wang

Posted on October 11, 2021

How to create a Offline Internationalization App:Support multiple languages

There is a column of locale in the Sqlite database that supports multiple languages, so you only need to change the Sql query conditions to find out the data in different languages.

image-20211010174334-jvp95ah

Modify VegetableDao to add language query conditions

[project_root]/lib/app/data/dao/vegetalbe_dao.dart

import 'package:floor/floor.dart';
import 'package:strapi_flutter_internation_poc/app/data/entity/vegetable.dart';

@dao
abstract class VegetableDao {
  @Query('SELECT * FROM vegetables_v WHERE  locale = :locale')
  Future<List<VegetableV>> findAll(String locale);
}

Enter fullscreen mode Exit fullscreen mode

Re-run the Floor code generator after modification

flutter packages pub run build_runner build
Enter fullscreen mode Exit fullscreen mode

HomeController

[project_root]/lib/app/modules/home/controllers/home_controller.dart

  Future<void> getAllVegetables() async {
    final result = await DbService.to.db.vegetableDao.findAll('en');
    vegetables.value = result;
  }
Enter fullscreen mode Exit fullscreen mode

The data displayed in this way is all in English

image-20211010175216-r3ixc2x

Next, integrate the internationalization features of GetX

Create a new language_service

[project_root]/lib/app/common/services/language_service.dart

Here, get_storage is used as a cache for the default language. Remember to increase the dependency of this library in the Flutter project. Use the command get install get_storage to quickly complete the installation.

import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';

class LanguageService extends GetxService {
  static LanguageService get to => Get.find();

  var box = GetStorage();
  var locale = Locale('en', 'US');
  var localeKey = 'en';

  Future<LanguageService> init() async {
    if (box.read('language') != null) {
      if (box.read('language') == 'zh-CN') {
        locale = Locale('zh', 'CN');
        localeKey = 'zh-CN';
      } else {
        locale = Locale('en', 'US');
        localeKey = 'en';
      }
    } else {
      if (ui.window.locale.languageCode == 'zh') {
        locale = Locale('zh', 'CN');
        localeKey = 'zh-CN';
      } else {
        locale = Locale('en', 'US');
        localeKey = 'en';
      }
    }

    return this;
  }

  void changeLocale(l) {
    if (l == Locale('zh', 'CN')) {
      localeKey = 'zh-CN';
      updateLocale(Locale('zh', 'CN'));
    } else if (l == Locale('en', 'US')) {
      localeKey = 'en';
      updateLocale(Locale('en', 'US'));
    }
    box.write('language', localeKey);
  }

  void updateLocale(_l) {
    locale = _l;
    Get.updateLocale(_l);
  }
}

Enter fullscreen mode Exit fullscreen mode

GetX Cli can quickly generate the multi-language configuration required by the GetX framework from a JSON file.

Create two new JSON files under [project_root]/assets/locales

en_US.json

{
  "app": {
    "name": "VAF"
  },
  "locale": {
    "title": "Language",
    "zh": "中文",
    "en": "English"
  }
}
Enter fullscreen mode Exit fullscreen mode

zh_CN.json

{
  "app": {
    "name": "蔬果"
  },
  "locale": {
    "title": "语言",
    "zh": "中文",
    "en": "English"
  }
}
Enter fullscreen mode Exit fullscreen mode

run

get generate locales assets/locales
Enter fullscreen mode Exit fullscreen mode

out[project_root]/lib/generated/locales.g.dart

class AppTranslation {
  static Map<String, Map<String, String>> translations = {
    'zh_CN': Locales.zh_CN,
    'en_US': Locales.en_US,
  };
}

class LocaleKeys {
  LocaleKeys._();
  static const app_name = 'app_name';
  static const locale_title = 'locale_title';
  static const locale_zh = 'locale_zh';
  static const locale_en = 'locale_en';
}

class Locales {
  static const zh_CN = {
    'app_name': '蔬果',
    'locale_title': '语言',
    'locale_zh': '中文',
    'locale_en': 'English',
  };
  static const en_US = {
    'app_name': 'VAF',
    'locale_title': 'Language',
    'locale_zh': '中文',
    'locale_en': 'English',
  };
}

Enter fullscreen mode Exit fullscreen mode

Add the initialization of LanguageService in main.dart

Future<void> initServices() async {
  print('starting services ...');
  await Get.putAsync(() => DbService().init());
  await Get.putAsync(() => LanguageService().init());
  print('All services started...');
}
Enter fullscreen mode Exit fullscreen mode

Modify runApp to add multi-language configuration

  runApp(
    GetMaterialApp(
      title: "Application",
      initialRoute: AppPages.INITIAL,
      getPages: AppPages.routes,
      translationsKeys: AppTranslation.translations,
      locale: LanguageService.to.locale,
      fallbackLocale: Locale('zh', 'CN'),
    ),
  );
Enter fullscreen mode Exit fullscreen mode

Adjust the query conditions in the Controller

final result = await DbService.to.db.vegetableDao.findAll('en');
Enter fullscreen mode Exit fullscreen mode

To

    final result = await DbService.to.db.vegetableDao
        .findAll(LanguageService.to.localeKey);
Enter fullscreen mode Exit fullscreen mode

Modify the text in the interface to reference multilingual resources

      appBar: AppBar(
        title: Text('Vegetables'),
        centerTitle: true,
      ),
Enter fullscreen mode Exit fullscreen mode

To

      appBar: AppBar(
        title: Text(LocaleKeys.app_name.tr),
        centerTitle: true,
      ),
Enter fullscreen mode Exit fullscreen mode

Run it again to see the interface that defaults to Chinese

image-20211010182410-m6ayfcq

Make a little improvement and add a button to switch languages

appBar: AppBar(
        title: Text(LocaleKeys.app_name.tr),
        centerTitle: true,
        actions: [
          IconButton(
              onPressed: () {
                Get.dialog(SimpleDialog(
                  title: Text(LocaleKeys.locale_title.tr),
                  children: <Widget>[
                    SimpleDialogOption(
                      onPressed: () {
                        LanguageService.to.changeLocale(Locale('en', 'US'));
                      },
                      child: Padding(
                        padding: const EdgeInsets.symmetric(vertical: 6),
                        child: Text(LocaleKeys.locale_en.tr),
                      ),
                    ),
                    SimpleDialogOption(
                      onPressed: () {
                        LanguageService.to.changeLocale(Locale('zh', 'CN'));
                      },
                      child: Padding(
                        padding: const EdgeInsets.symmetric(vertical: 6),
                        child: Text(LocaleKeys.locale_zh.tr),
                      ),
                    ),
                  ],
                ));
              },
              icon: Icon(Icons.language))
        ],
      ),
Enter fullscreen mode Exit fullscreen mode

Very convenient and fast to complete the language switch

image-20211010183432-nqf0vo3


Image description

Play Store

Apple Store

💖 💪 🙅 🚩
nasawz
nasa.wang

Posted on October 11, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related