flutter_gettext

Last updated:

0 purchases

flutter_gettext Image
flutter_gettext Images
Add to Cart

Description:

flutter gettext

Flutter gettext #
This package provides a simple way to translate your flutter application using gettext.
It is inspired/based on gettext and gettext_parser and gettext_i18n but doesn't depend on these packages.
Features #

✅ Positional arguments
✅ Named arguments
✅ Plurals
✅ Contexts
✅ Comments

Usage #
Add this package, and flutter_localizations, to pubspec.yaml:
flutter pub add flutter_gettext
flutter pub add flutter_localizations --sdk=flutter
copied to clipboard
In pubspec.yaml, add assets/i18n/ as an asset folder:
flutter:
assets:
- assets/i18n/
copied to clipboard
Place your translation files in that folder:
$ ls assets/i18n/
en.po
de.po
de_AT.po
copied to clipboard
In your application file, declare supported locales, and initialize translations:
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
locale: const Locale('de'),
supportedLocales: const [
Locale('en'),
Locale('de'),
Locale('de_AT'),
],
localizationsDelegates: [
GettextLocalizationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
home: const HomePage(),
);
}
}
copied to clipboard
In files where you want to translate a string :
import 'package:gettext_i18n/gettext_i18n.dart';

Text(context.translate('There is {0} apple', keyPlural: 'There are {0} apples', pArgs: [1]));
// output: There is 1 apple
// output(de): Es gibt 1 Apfel
Text(context.translate('There is {0} apple', keyPlural: 'There are {0} apples', pArgs: [2]));
// output: There are 2 apples
// output(de): Es gibt 2 Äpfel

Text(
context.translate(
'You have {message_count} message',
keyPlural: 'You have {message_count} messages',
nArgs: {'message_count': 1},
),
);
// output: You have 1 message
// output(de): Du hast 1 Nachricht

Text(
context.translate(
'You have {message_count} message',
keyPlural: 'You have {message_count} messages',
nArgs: {'message_count': 3},
),
);
// output: You have 3 messages
// output(de): Du hast 3 Nachrichten
copied to clipboard
po files structure #
msgid ""
msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"language: en\n"

msgid "Hello"
msgstr "Hello"

# Comments are ignored
msgid "Welcome to my app"
msgstr "Welcome to my app"

# Named argument implementation
msgid "You have {message_count} message"
msgid_plural "You have {message_count} messages"
msgstr[0] "You have {message_count} message"
msgstr[1] "You have {message_count} messages"

# Positional argument implementation
msgid "There is {0} apple"
msgid_plural "There are {0} apples"
msgstr[0] "There is {0} apple"
msgstr[1] "There are {0} apples"
copied to clipboard
Additional information #
.po files can be edited by hand, but it's preferable to use an editor or an online service to manage them.

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.