lokalise_flutter_sdk 0.0.7 lokalise_flutter_sdk: ^0.0.7 copied to clipboard
Lokalise Flutter SDK over-the-air translations update.
Lokalise Flutter SDK #
This package provides over-the-air translation updates from lokalise.com
Getting started #
Update pubspec.yaml file #
- Add the intl and lokalise_flutter_sdk package to the pubspec.yaml file:
dependencies: flutter: sdk: flutter flutter_localizations: # Add this line sdk: flutter # Add this line intl: ^0.17.0 # Add this line lokalise_flutter_sdk: ^0.1.0 # Add this line
- Also, in the pubspec.yaml file, enable the generate flag. This is added to the section of the pubspec that is specific to Flutter, and usually comes later in the pubspec file.
# The following section is specific to Flutter. flutter: generate: true # Add this line
- In ${FLUTTER_PROJECT}/lib/l10n, add the intl_en.arb template file. For example:
{ "@@locale": "en", "pageHomeWelcomeMessage": "Greetings!!!", "title": "Lokalise SDK", "@title": { "type": "text", "placeholders": {} }, "welcome_header": "Welcome", "insentive": "Іnsentive", "sugnup_button": "Signup Button" }
- Next, aAdd one ARB file for each locale you need to support in your Flutter app. Add them to lib/l10n folder inside your project, and name them in a following way: intl_
{ "pageHomeWelcomeMessage": "Вітаю вас!", "welcome_header": "Ласкаво просимо", }
-
Now, run terminal command: dart pub global activate lokalise_flutter_sdk.
-
Next, run terminal command: tr. You should see generated folder /lib/generated/..
Initialize Lokalise SDK (main.dart
file) #
import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:lokalise_flutter_sdk/lokalise_flutter_sdk.dart'; import 'generated/l10n.dart'; void main() { Lokalise.init('Lokalise SDK Token', 'Project ID'); Lokalise.preRelease(true); // Add this only if you want to use prereleases Lokalise.setVersion(0); // Add this only if you want to explicitly set the application version, or in cases when automatic detection is not possible (e.g. Flutter web apps) runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( title: 'Lokalise SDK', theme: ThemeData( primarySwatch: Colors.blue, ), home: const MyHomePage(), onGenerateTitle: (context) => Tr.of(context).welcome_header, localizationsDelegates: const [ Tr.delegate, GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, GlobalCupertinoLocalizations.delegate, ], supportedLocales: Tr.delegate.supportedLocales, ); } } class MyHomePage extends StatefulWidget { const MyHomePage({Key? key}) : super(key: key); @override State
Generate localization files for over-the-air translation updates #
To generate dart class for OTA service, after the translation template have been changed (lib/l10n/intl_
Change locale #
setState(() { Tr.load(const Locale('ar_BH')); })