csv_localizations 0.3.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 59

csv_localizations #

A minimal CSV localization package for Flutter.

Store translations for multiple languages in a single CSV file.

Consider toml_localizations or yaml_localizations for separate files per language.

Usage #

See example.

Install #

Add to your pubspec.yaml

dependencies:
  csv_localizations:

Add CSV asset file #

Add a CSV file as an asset and describe it in your pubspec.yaml

Tip: Create a Spreadsheet via Google docs, then export as CSV

flutter:
  assets:
    - assets/lang.csv

Example CSV file #

key,en,nb
Hi,Hi,Hei
my_img,assets/en.png,assets/nb.png
Multiline,"This
  is a multiline 
string","Denne
  teksten går over flere 
linjer"

Tip: keys can point to local assets like images etc.

Format #

First row lists supported language codes.

First column are keys for localized values.

Wrap multiline strings in quotation marks.

We use the csv package for parsing, but we use \n as the default eol and replace \r\n with \n before parsing.

MaterialApp #

Add CsvLocalizationsDelegate to MaterialApp and set supportedLocales using language codes.

MaterialApp(
  localizationsDelegates: [
    ... // global delegates
    CsvLocalizationsDelegate(
      CsvLocalizations(
        assetPath: 'assets/lang.csv',
        supportedLanguageCodes: [ 'en', 'nb', ],
      ),
    ),
  ],
  supportedLocales: [ Locale('en'), Locale('nb'), ],
}

API #

Translate strings using

CsvLocalizations.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) => CsvLocalizations.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>

CHANGELOG #

[0.3.0] #

  • use \n as default eol but replace \r\n with \n before parse
  • improve README
  • add multiline example

[0.2.0+3] #

  • update README

[0.2.0+2] #

  • add rationale to README

[0.2.0+1] #

  • tweak example and remove danish

[0.2.0] #

  • simplify API to use a single string method. We don't want to pollute String with extensions methods. Users can add (and name) these themselves as they see fit.

[0.1.5] #

  • trim CSV file for leading and trailing spaces

[0.1.4] #

  • update README

[0.1.3] #

  • update README with localized image example

[0.1.2] #

  • update README

[0.1.1] #

  • update README

[0.1.0] #

  • add String extension translate method 'Hi'.tr(context)
  • update README

[0.0.1] #

  • initial release
  • localize app using a single csv file

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:csv_localizations/csv_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,
        CsvLocalizationsDelegate(
          CsvLocalizations(
            assetPath: 'assets/lang.csv',
            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('csv_localizations'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text(
            CsvLocalizations.of(context).string('Hi'),
            style: TextStyle(
              fontSize: 24,
            ),
          ),
          Image.asset(
            CsvLocalizations.of(context).string('my_img'),
            width: 64,
            height: 64,
            fit: BoxFit.contain,
            filterQuality: FilterQuality.none,
          ),
          Text(
            CsvLocalizations.of(context).string('Multiline'),
            style: TextStyle(
              fontSize: 24,
            ),
          ),
        ],
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  csv_localizations: ^0.3.0

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:csv_localizations/csv_localizations.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
18
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]
59
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.14
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

Because:

  • csv_localizations that is a package requiring null.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
csv ^4.0.3 4.0.3
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 2.1.0-nullsafety
Dev dependencies
flutter_test