flutter_strings_localization 1.0.1

  • Readme
  • Changelog
  • Installing
  • 52

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)),
				),
			),
		);
	}
}

1.0.0 #

  • First version with support for normal strings, plural strings and override user language.

1.0.1 #

  • Update readme and close scope for TranslationManager methods.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_strings_localization: ^1.0.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_strings_localization/localizate_files.dart';
import 'package:flutter_strings_localization/translate_manager.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
8
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
52
Learn more about scoring.

We analyzed this package on Jul 8, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

because of import path [flutter_strings_localization] that is in a package requiring null.

Maintenance suggestions

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and flutter_strings_localization.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.1
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8
Dev dependencies
flutter_test