yaml_localizations 0.1.0+5

  • Readme
  • Changelog
  • Example
  • Installing
  • 68

yaml_localizations #

A minimal YAML localization package for Flutter.

YAML is a human-readable, configuration file format with a minimal syntax, which allows you to represent strings as key/value pairs. String can be be both unquoted, quoted and span multiple lines. Both basic and literal strings are supported.

Consider toml_localizations, which does not depend on string indentation, or csv_localizations if you want to support multiple languages in a single file.

Usage #

See example.

Install #

Add to your pubspec.yaml

dependencies:
  yaml_localizations:

Add a YAML file per language #

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

flutter:
  assets:
    - {path}/{languageCode}.yaml
Example YAML file
Hi: Hi
Text: |
  There once was a tall man from Ealing
  Who got on a bus to Darjeeling
      It said on the door
      "Please don't sit on the floor"
  So he carefully sat on the ceiling
Long: >
  Wrapped text
  will be folded
  into a single
  paragraph

  Blank lines denote
  paragraph breaks

Tip: Yaml supports several ways of expressing strings. Use the vertical bar character to indicate that a string will span several lines. Use the greater-than character to break up long lines.

MaterialApp #

Add YamlLocalizationsDelegate to MaterialApp and set supportedLocales using language codes.

MaterialApp(
  localizationsDelegates: [
    ... // global delegates
    YamlLocalizationsDelegate(
      YamlLocalizations(
        assetPath: 'yaml_translations',
        supportedLanguageCodes: [ 'en', 'nb', ],
      ),
    ),
  ],
  supportedLocales: [ Locale('en'), Locale('nb'), ],
}

API #

Translate strings using

YamlLocalizations.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) => YamlLocalizations.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 #

  • add rationale to README

[0.1.0+2] - 22 may 2020 #

  • simplify and remove danish from example

[0.1.0+1] - 15 may 2020 #

  • Added a yaml example file to README
  • Added a new string example for broken lines

[0.1.0] - 15 may 2020 #

  • initial release

example/lib/main.dart

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: [
        GlobalWidgetsLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        YamlLocalizationsDelegate(
          YamlLocalizations(
            assetPath: 'assets/yaml_translations',
            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('yaml_localizations'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text(YamlLocalizations.of(context).string('Hi')),
          SizedBox(height: 12),
          Text(YamlLocalizations.of(context).string('Text')),
          SizedBox(height: 12),
          Text(YamlLocalizations.of(context).string('Long')),
        ],
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  yaml_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:yaml_localizations/yaml_localizations.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
35
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]
68
Learn more about scoring.

We analyzed this package on Jul 7, 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 [yaml_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
yaml ^2.2.1 2.2.1
Transitive dependencies
charcode 1.1.3
collection 1.14.12 1.14.13
meta 1.1.8 1.2.1
path 1.7.0
sky_engine 0.0.99
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6 1.2.0
vector_math 2.0.8
Dev dependencies
flutter_test