Skip to content

Commit

Permalink
Merge pull request #91 from Impaktfull/feat/formatting
Browse files Browse the repository at this point in the history
feat: Added formatting function to format your locale files
  • Loading branch information
vanlooverenkoen committed Feb 11, 2024
2 parents c739d61 + 5eb8811 commit b8f955c
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 74 deletions.
19 changes: 0 additions & 19 deletions .github/workflows/analyzer.yml

This file was deleted.

25 changes: 25 additions & 0 deletions .github/workflows/create_tag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Automated version bump

on:
push:
branches:
- main

jobs:
bump_version:
name: Version bump on main
runs-on: ubuntu-latest
steps:
- name: Checkout source code
uses: actions/checkout@v2
with:
token: ${{ secrets.IMPAKTFULL_GITHUB_PAT }}
ref: ${{ github.ref }}
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: 18
- name: Automated version bump
uses: impaktfull/gh_action_dart_conventional_release@main
with:
tag-prefix: 'v'
19 changes: 0 additions & 19 deletions .github/workflows/formatting.yml

This file was deleted.

14 changes: 14 additions & 0 deletions .github/workflows/publish_to_pubdev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Publish to pub.dev

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

jobs:
publish:
permissions:
id-token: write # Required for authentication using OIDC
uses: Impaktfull/gh_action_dart_conventional_release/.github/workflows/flutter_release.yml@main
with:
environment: 'pub.dev'
25 changes: 0 additions & 25 deletions .github/workflows/tests.yml

This file was deleted.

7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Changelog
# 12.1.0

## Feat

- Formatting function added. (`dart run locale_gen:format`)

## [12.0.0] - 2023-06-05
Updated to intl 0.18.x

Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,19 @@ flutter packages pub run locale_gen
### Run package with Dart

```shell
pub run locale_gen
dart pub run locale_gen
```

### Format your locale file in Flutter

```shell
flutter packages pub run locale_gen:format
```

### Format your locale file in Dart

```shell
dart pub run locale_gen:format
```

### Migration steps <9.0.0 to >=9.0.0
Expand Down
1 change: 0 additions & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ analyzer:
missing_required_param: error
missing_return: error
todo: ignore
sdk_version_async_exported_from_core: ignore
exclude:
- '**.g.dart'
- '**.mocks.dart'
Expand Down
6 changes: 6 additions & 0 deletions bin/format.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import 'package:locale_gen/locale_gen.dart';

void main() {
final params = LocaleGenParams('locale_gen');
LocaleGenFormatter.format(params);
}
1 change: 0 additions & 1 deletion example/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ analyzer:
missing_required_param: error
missing_return: error
todo: ignore
sdk_version_async_exported_from_core: ignore
exclude:
- '**.g.dart'
- '**.mocks.dart'
Expand Down
4 changes: 2 additions & 2 deletions example/assets/locale/fi-FI.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"test_arg2": "Lisää napauttamalla %1$d",
"test_arg3": "Lisää napauttamalla %1$s %2$d",
"test_arg4": "Lisää napauttamalla %1$s %2$f %1$s",
"welcome_message": "Lisää napauttamalla",
"test_new_line": "Lisää\nLisää napauttamalla\n\n%1$s %2$d %1$s",
"test_new_line_carriage_return": "Carriage\r\nReturn",
"test_non_positional": "Testataan ei-positiaalista argumenttia %s ja %f",
"test_plural": {
"one": "%d tunti",
"other": "%d tuntia"
}
},
"welcome_message": "Lisää napauttamalla"
}
4 changes: 2 additions & 2 deletions example/assets/locale/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"test_arg2": "Test argument %1$d",
"test_arg3": "Test argument %1$s %2$d",
"test_arg4": "Test argument %1$s %2$f %1$s",
"welcome_message": "Hallo daar",
"test_new_line": "Test\nargument\n\n%1$s %2$d %1$s",
"test_new_line_carriage_return": "Carriage\r\nReturn",
"test_non_positional": "Test niet positioneel argument %s en %f",
"test_plural": {
"one": "%d uur",
"other": "%d uren"
}
},
"welcome_message": "Hallo daar"
}
1 change: 1 addition & 0 deletions lib/locale_gen.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export 'src/locale_gen_params.dart';
export 'src/locale_gen_writer.dart';
export 'src/locale_gen_formatter.dart';
13 changes: 10 additions & 3 deletions lib/src/case_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ class CaseUtil {
return words.join('');
}

static String getSnakeCase(String string) {
final wordsGroup = _groupIntoWords(string);
final words = wordsGroup.map(_allLowerCase).toList();
return words.join('_');
}

static List<String> _groupIntoWords(String text) {
final sb = StringBuffer();
final words = <String>[];
Expand Down Expand Up @@ -42,7 +48,8 @@ class CaseUtil {
return words;
}

static String _upperCaseFirstLetter(String word) {
return '${word.substring(0, 1).toUpperCase()}${word.substring(1).toLowerCase()}';
}
static String _upperCaseFirstLetter(String word) =>
'${word.substring(0, 1).toUpperCase()}${word.substring(1).toLowerCase()}';

static String _allLowerCase(String word) => word.toLowerCase();
}
30 changes: 30 additions & 0 deletions lib/src/locale_gen_formatter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'dart:convert';
import 'dart:io';

import 'package:locale_gen/locale_gen.dart';
import 'package:locale_gen/src/case_util.dart';

class LocaleGenFormatter {
const LocaleGenFormatter._();

static void format(LocaleGenParams params) {
final files = params.languages
.map((language) => File('${params.assetsDir}$language.json'));
const jsonEncoder = JsonEncoder.withIndent(' ');
for (final file in files) {
print('Formatting ${file.path}');
final content = file.readAsStringSync();
final json = jsonDecode(content) as Map<String, dynamic>;
final newJson = {};
final sortedKeys = json.keys.toList()..sort();
for (final key in sortedKeys) {
final value = json[key];
newJson[CaseUtil.getSnakeCase(key)] = value;
}

final data = jsonEncoder.convert(newJson);
file.writeAsStringSync(data);
}
print('Formatting done!');
}
}

0 comments on commit b8f955c

Please sign in to comment.