Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ArbAssetLoader #43

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/pr_check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: PR Check

on:
workflow_dispatch:
pull_request:

jobs:
test:
name: Test
uses: ./.github/workflows/test.yml
secrets: inherit
39 changes: 39 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Release to pub.dev

on:
workflow_dispatch:
push:
tags:
- 'v[0-9]+.[0-9]+.[0-9]+*'

jobs:
test:
name: Test
uses: ./.github/workflows/test.yml
secrets: inherit

publish:
needs: [test]
name: Publish
permissions:
id-token: write # This is required for authentication using OIDC
runs-on: ubuntu-latest
timeout-minutes: 5

steps:
- uses: actions/checkout@v3

- uses: dart-lang/setup-dart@v1

- uses: subosito/flutter-action@v2
with:
channel: "stable"

- name: Install dependencies
run: dart pub get

- name: code format
run: dart format lib/*/*.dart lib/*.dart

- name: Publish
run: dart pub publish --force
29 changes: 29 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Test

on:
workflow_call:

jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v1
with:
java-version: '12.x'
- uses: subosito/flutter-action@v1
with:
channel: 'stable'

- name: Install packages dependencies
run: flutter pub get

- name: Analyze the project's Dart code
run: flutter analyze

- name: Run tests
run: flutter test

- name: Run tests coverage
run: flutter test --coverage
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*.ipr
*.iws
.idea/
.vscode/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
Expand Down
23 changes: 21 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
## [0.0.1]
## 2.0.1

- initial release
- Extend delimiters for csv loader

## 2.0.0

- **BREAKING**: The local `AssetLoader` class deleted, now using the one from
[easy_localization](https://pub.dev/documentation/easy_localization/latest/easy_localization/AssetLoader-class.html) itself.
- **BREAKING**: Depends on [connectivity_plus](https://pub.dev/packages/connectivity_plus) ^4.0.0
and [http](https://pub.dev/packages/http) ^1.0.0.
- Const constructors in:
- `FileAssetLoader`
- `HttpAssetLoader`
- `JsonAssetLoader`
- `TestsAssetLoader`
- `XmlAssetLoader`
- `YamlAssetLoader`
- Fixed deprecations

## 0.0.1

- Initial release.
11 changes: 11 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Contributing

## Release process

1. Make sure that the changelog is updated

2. Make sure that the version in pubspec.yaml is correct

3. Create a release in the github UI. Name the release like the version, but with a v (3.7.5 -> v3.7.5). Name the tag like the release

4. A pipeline will run and deploy the new version to pub.dev
35 changes: 26 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Custom assets loaders for [Easy Localization](https://github.com/aissat/easy_localization) package
## Custom assets loaders for [Easy Localization](https://github.com/aissat/easy_localization) package

![Pub Version](https://img.shields.io/pub/v/easy_localization_loader?style=flat-square)
![Code Climate issues](https://img.shields.io/github/issues/aissat/easy_localization_loader?style=flat-square)
Expand All @@ -15,6 +15,7 @@
### Supported formats

- [x] JSON (JsonAssetLoader)
- [x] ARB (ArbAssetLoader)
- [x] CSV (CsvAssetLoader)
- [x] HTTP (HttpAssetLoader)
- [x] XML (XmlAssetLoader, XmlSingleAssetLoader)
Expand All @@ -39,19 +40,35 @@ dependencies:

```

2. Change assetLoader and patch
2. Change assetLoader and path

```dart
...
void main(){
runApp(EasyLocalization(
child: MyApp(),
supportedLocales: [Locale('en', 'US'), Locale('ar', 'DZ')],
path: 'resources/langs/langs.csv',
assetLoader: CsvAssetLoader()
));
runApp(EasyLocalization(
child: MyApp(),
supportedLocales: [Locale('en', 'US'), Locale('ar', 'DZ')],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert formating

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted

path: 'resources/langs/langs.csv',
assetLoader: CsvAssetLoader()
));
}
...
```

3. All done!.
3. All done!.


### Loaders Specification

#### HttpAssetLoader

In order to use HttpAssetLoader you must provide a path to a folder (i.e. base path) where all your translations are placed like `https://example.com/translations`

Your translations should be created as separate files with `.json` extension. Placing translations as individual files reduces the size of the file to load on application init.
Example:

```
translations/
├── en-US.json
└── uk-UA.json
```
13 changes: 6 additions & 7 deletions lib/easy_localization_loader.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
library easy_localization_loader;

export 'package:easy_localization_loader/src/csv_asset_loader.dart';
export 'package:easy_localization_loader/src/file_asset_loader.dart';
export 'package:easy_localization_loader/src/json_asset_loader.dart';
export 'package:easy_localization_loader/src/http_asset_loader.dart';
export 'package:easy_localization_loader/src/csv_asset_loader.dart';
export 'package:easy_localization_loader/src/yaml_asset_loader.dart';
export 'package:easy_localization_loader/src/xml_asset_loader.dart';
export 'package:easy_localization_loader/src/tests_asset_loader.dart';
export 'package:easy_localization_loader/src/json_asset_loader.dart';
export 'package:easy_localization_loader/src/smart_network_asset_loader.dart';
export 'package:easy_localization_loader/src/tests_asset_loader.dart';
export 'package:easy_localization_loader/src/xml_asset_loader.dart';
export 'package:easy_localization_loader/src/yaml_asset_loader.dart';
export 'package:easy_localization_loader/src/arb_asset_loader.dart';
38 changes: 38 additions & 0 deletions lib/src/arb_asset_loader.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'dart:convert';
import 'dart:developer';
import 'dart:ui';

import 'package:flutter/services.dart';

import 'package:easy_localization/easy_localization.dart';

class ArbAssetLoader extends AssetLoader {
const ArbAssetLoader();

String getLocalePath(String basePath, Locale locale) {
return '$basePath/${locale.toStringWithSeparator(separator: "-")}.arb';
}

@override
Future<Map<String, dynamic>> load(String path, Locale locale) async {
var localePath = getLocalePath(path, locale);
log('easy localization loader: load arb file $localePath');
return jsonDecode(await rootBundle.loadString(localePath));
}
}

// Loader for single json file
class ArbSingleAssetLoader extends AssetLoader {
Map? jsonData;

@override
Future<Map<String, dynamic>> load(String path, Locale locale) async {
if (jsonData == null) {
log('easy localization loader: load arb file $path');
jsonData = jsonDecode(await rootBundle.loadString(path));
} else {
log('easy localization loader: arb already loaded, read cache');
}
return jsonData![locale.toString()];
}
}
27 changes: 0 additions & 27 deletions lib/src/asset_loader.dart

This file was deleted.

17 changes: 13 additions & 4 deletions lib/src/csv_asset_loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,28 @@ import 'dart:ui';

import 'package:csv/csv.dart';
import 'package:csv/csv_settings_autodetection.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/services.dart';

import 'asset_loader.dart';

//
// load example/resources/langs/langs.csv
//
class CsvAssetLoader extends AssetLoader {
CSVParser? csvParser;
final bool useAutodetect;

CsvAssetLoader({
this.useAutodetect = true,
});

@override
Future<Map<String, dynamic>> load(String path, Locale locale) async {
if (csvParser == null) {
log('easy localization loader: load csv file $path');
csvParser = CSVParser(await rootBundle.loadString(path));
csvParser = CSVParser(
await rootBundle.loadString(path),
useAutodetect: useAutodetect,
);
} else {
log('easy localization loader: CSV parser already loaded, read cache');
}
Expand Down Expand Up @@ -60,8 +67,10 @@ class CSVParser {
csvSettingsDetector:
useAutodetect && fieldDelimiter == null && eol == null
? FirstOccurrenceSettingsDetector(
fieldDelimiters: [',', ';', '\t'],
textDelimiters: ['"', "'", '”'],
textEndDelimiters: ['"', "'", '”'],
eols: ['\r\n', '\n'],
fieldDelimiters: [',', '\t'],
)
: null,
);
Expand Down
8 changes: 3 additions & 5 deletions lib/src/file_asset_loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import 'dart:developer';
import 'dart:io';
import 'dart:ui';

import 'asset_loader.dart';
import 'package:easy_localization/easy_localization.dart';

//
//
//
//
class FileAssetLoader extends AssetLoader {
const FileAssetLoader();

@override
Future<Map<String, dynamic>> load(String path, Locale locale) async {
final file = File(path);
Expand Down
15 changes: 6 additions & 9 deletions lib/src/http_asset_loader.dart
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
//
//
//
//
import 'dart:convert';
import 'dart:developer';
import 'dart:ui';

import 'package:easy_localization/easy_localization.dart';
import 'package:http/http.dart' as http;

import 'asset_loader.dart';

class HttpAssetLoader extends AssetLoader {
const HttpAssetLoader();

@override
Future<Map<String, dynamic>> load(String path, Locale locale) async {
log('easy localization loader: load http $path');
try {
var url = Uri.parse(path);
var url = Uri.parse('$path/${locale.toLanguageTag()}.json');
return http
.get(url)
.then((response) => json.decode(response.body.toString()));
.then((response) => json.decode(utf8.decode(response.bodyBytes)));
} catch (e) {
//Catch network exceptions
return Future.value();
return {};
}
}
}
7 changes: 4 additions & 3 deletions lib/src/json_asset_loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import 'dart:convert';
import 'dart:developer';
import 'dart:ui';

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/services.dart';

import 'asset_loader.dart';

class JsonAssetLoader extends AssetLoader {
const JsonAssetLoader();

String getLocalePath(String basePath, Locale locale) {
return '$basePath/${localeToString(locale, separator: "-")}.json';
return '$basePath/${locale.toStringWithSeparator(separator: "-")}.json';
}

@override
Expand Down
3 changes: 1 addition & 2 deletions lib/src/smart_network_asset_loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import 'dart:convert';
import 'dart:io';
import 'dart:ui';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:http/http.dart' as http;
import 'package:path_provider/path_provider.dart' as paths;

import 'package:flutter/services.dart';

import 'asset_loader.dart';

/// ```dart
/// SmartNetworkAssetLoader(
/// assetsPath: 'assets/translations',
Expand Down
Loading