toml_localizations 0.1.0+5

  • Readme
  • Changelog
  • Example
  • Installing
  • 50

toml_localizations #

A minimal TOML localization package for Flutter.

TOML is a minimal, easy to read, configuration file format, which allows you to represent strings as key/value pairs. That is, as basic, literal or multi-line strings. I believe TOML has the best format to represent a set of strings. Compared to YAML, strings does not have to be indented, which is an advantage when working with text.

Consider csv_localizations if you want to support multiple languages in a single file.

Usage #

See example.

Install #

Add to your pubspec.yaml

dependencies:
  toml_localizations:

Add a TOML file per language #

Add a TOML file per language you support in an asset path and describe it in your pubspec.yaml

flutter:
  assets:
    - {path}/{languageCode}.toml
Example TOML file
str = "The quick brown fox jumps over the lazy dog."

literal_str = 'C:\Users\nodejs\templates'

multiline_str = """\
The quick brown \
fox jumps over \
the lazy dog.\
"""

literal_multiline_str = '''
The first newline is
trimmed in raw strings.
   All other whitespace
   is preserved.
'''

Tip: Toml supports several ways of expressing strings. See Toml documentation for more info.

MaterialApp #

Add TomlLocalizationsDelegate to MaterialApp and set supportedLocales using language codes.

MaterialApp(
  localizationsDelegates: [
    ... // global delegates
    TomlLocalizationsDelegate(
      TomlLocalizations(
        assetPath: 'toml_translations',
        supportedLanguageCodes: [ 'en', 'nb', ],
      ),
    ),
  ],
  supportedLocales: [ Locale('en'), Locale('nb'), ],
}

API #

Translate strings using

TomlLocalizations.of(context).string('Hi')

We keep the API simple, but you can easily add an extension method to String like this:

extension LocalizedString on String {
  String tr(BuildContext context) => TomlLocalizations.of(context).string(this);
}

Note on iOS #

Add supported languages to ios/Runner/Info.plist as described here.

Example:

<key>CFBundleLocalizations</key>
<array>
	<string>en</string>
	<string>nb</string>
</array>

[0.1.0+5] - 28 june 2020 #

  • update README

[0.1.0+4] - 7 june 2020 #

  • update README

[0.1.0+3] - 23 may 2020 #

  • update README

[0.1.0] - 22 may 2020 #

  • initial release

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:toml_localizations/toml_localizations.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

extension LocalizedString on String {
  String tr(BuildContext context) => TomlLocalizations.of(context).string(this);
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: [
        GlobalWidgetsLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        TomlLocalizationsDelegate(
          TomlLocalizations(
            assetPath: 'assets',
            supportedLanguageCodes: [
              'en',
              'nb',
            ],
          ),
        ),
      ],
      supportedLocales: [
        Locale('en'),
        Locale('nb'),
      ],
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('toml_localizations'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text(TomlLocalizations.of(context).string('str')),
          SizedBox(height: 12),
          Text('literal_str'.tr(context)),
          SizedBox(height: 12),
          Text('multiline_str'.tr(context)),
          SizedBox(height: 12),
          Text('literal_multiline_str'.tr(context)),
        ],
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  toml_localizations: ^0.1.0+5

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:toml_localizations/toml_localizations.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
50
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 [toml_localizations] that is in a package requiring null.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
flutter 0.0.0
toml ^0.6.0 0.6.0
Transitive dependencies
charcode 1.1.3
collection 1.14.12 1.14.13
matcher 0.12.8
meta 1.1.8 1.2.1
path 1.7.0
petitparser 2.4.0 3.0.4
quiver 2.1.3
sky_engine 0.0.99
source_span 1.7.0
stack_trace 1.9.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
yaml 2.2.1
Dev dependencies
flutter_test