flutter_strings_localization

A flutter plugin to manage different locations files for your translates with supports for differents country codes and plurals.

Project configuration

To use flutter_localizations, add the package as a dependency to your pubspec.yaml file:

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter

Next, import the flutter_localizations library and specify localizationsDelegates and supportedLocales for MaterialApp:

import 'package:flutter_localizations/flutter_localizations.dart';

MaterialApp(
 localizationsDelegates: [
   // ... app-specific localization delegate[s] here
   GlobalMaterialLocalizations.delegate,
   GlobalWidgetsLocalizations.delegate,
   GlobalCupertinoLocalizations.delegate,
 ],
 supportedLocales: [
    const Locale('en'), // English
    const Locale('he'), // Hebrew
  ],
  // ...
)

More info in https://flutter.dev/docs/development/accessibility-and-localization/internationalization

iOS configuration

Add the next permissions to info.plist with your language codes selected

<key>CFBundleDevelopmentRegion</key>
  <string>en</string>

<key>CFBundleLocalizations</key>
  <array>
	<string>en</string>  <!-- English -->
	<string>he</string>  <!-- Hebrew  -->
  </array>

Android configuration

Nothing to do! Great!

Library Configuration

Add flutter_strings_localization package as a dependency to your pubspec.yaml file:

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter

  # Another Awesome libreries

  flutter_strings_localization: ^1.0.1

How to use

You will need to create a individual file for each location. For example. if you want to have support for (English, Spanish and French) in your project, You will need to add 3 individual files.dart (strings_en.dart, strings_es.dart, strings_french.dart)

Contry code is supported too and you can add different variations of english for example. en_US and _ en_UK_

1. Create your StringsKey file

class  StringsKey {
	static  final title =  "title";
	static  final sampleWithoutParams =  "sampleWithoutParams";
	static  final sampleWithParams =  "sampleWithParams";
	static  final sampleWithPlurals =  "sampleWithPlurals";
}

2. Create your first location file

Sample file for strings_en.dart

Map<String, Map<PluralsKey, String>> stringFileEN = {
	StringsKey.title: {
		PluralsKey.none:  'This is the title',
	},

	StringsKey.sampleWithoutParams: {
		PluralsKey.none:  'This is an description without parameteres'
	},

	StringsKey.sampleWithParams: {
		PluralsKey.none:  'This is the "@0" that I am sending'
	},

	StringsKey.sampleWithPlurals: {
		PluralsKey.zero:  'Tomato Empty',
		PluralsKey.one:  'I have @0 tomato',
		PluralsKey.other:  'I have @0 tomatoes'
	},
};

PluralsKey is an enum used by the library with 4 options (none, zero, one, other) Please use .none for default strings without plurals because is called by default for the getString() method.

@0 is the easy way to replace the different parameters that you will send. there are not limit but always start in @0 and continue with @1, @2, @3, @4. etc... this parameters are sending in a dynamic list.

2. Add your location file

void  main() {
	LocalizationsFiles().save = {
		'en': stringFileES, // English File
		'he': stringFileHE // Hebrew File
	};
	runApp(MyApp());
}

_ Important_ the 'locale_key' must be the same declared in supported locales.

 supportedLocales: [
    const Locale('en'), // English
    const Locale('he'), // Hebrew
  ],

3. Try and enjoy!

_Remember to do a hot restart when all things are installed _

You can call the TranslateManager to access the methods getString() and getPlurals()

class HomePage extends StatelessWidget {
	const HomePage({Key key}) : super(key: key);

	@override
	Widget build(BuildContext context) {
		final translateManager = TranslateManager(context);
		return Scaffold(
			appBar: AppBar(
				title: Text(translateManager.getString(StringsKey.title)),
			),
			body: Container(
				child: Center(
					child:
						Text(translateManager.getString(StringsKey.sampleWithoutParams)),
				),
			),
		);
	}
}

Libraries

localizate_files
translate_manager