Skip to content

Commit

Permalink
Refactor code formatting and minor improvements in l10n mapper generator
Browse files Browse the repository at this point in the history
  • Loading branch information
kwado-tech committed Feb 1, 2025
1 parent cae854c commit 19f9556
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
3 changes: 1 addition & 2 deletions l10n_mapper_generator/core/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ class LogType {
int get hashCode => _value.hashCode;
}

void logger<T>(String log, T Function()? callback,
{LogType type = LogType.none}) {
void logger<T>(String log, T Function()? callback, {LogType type = LogType.none}) {
if (type != LogType.none) {
// ignore: avoid_print
print('${type.toString()}: $log');
Expand Down
20 changes: 6 additions & 14 deletions l10n_mapper_generator/core/models/l10n_mapper_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ class TranslationOptions {
final Option<String> input;
final Option<String> output;

const TranslationOptions(
{required this.locale, required this.input, required this.output});
const TranslationOptions({required this.locale, required this.input, required this.output});

factory TranslationOptions.none() =>
TranslationOptions(locale: none(), input: none(), output: none());
factory TranslationOptions.none() => TranslationOptions(locale: none(), input: none(), output: none());

factory TranslationOptions.fromJson(Map<String, dynamic> json) {
return TranslationOptions(
Expand All @@ -44,18 +42,14 @@ class TranslationOptions {
bool operator ==(Object other) {
if (identical(this, other)) return true;

return other is TranslationOptions &&
other.locale == locale &&
other.input == input &&
other.output == output;
return other is TranslationOptions && other.locale == locale && other.input == input && other.output == output;
}

@override
int get hashCode => locale.hashCode ^ input.hashCode ^ output.hashCode;

@override
String toString() =>
'TranslationOptions(locale: $locale, input: $input, output: $output)';
String toString() => 'TranslationOptions(locale: $locale, input: $input, output: $output)';
}

class FormatterOptions {
Expand Down Expand Up @@ -89,12 +83,10 @@ class FormatterOptions {
inputPath: optionOf(json['inputPath'] as String?),
outputPath: optionOf(json['outputPath'] as String?),
translations: (json['translations'] as List?)
?.map(
(e) => TranslationOptions.fromJson(e as Map<String, dynamic>))
?.map((e) => TranslationOptions.fromJson(e as Map<String, dynamic>))
.toList() ??
[],
keyPredicateMatch:
optionOf(json['keyPredicateMatch'] as Map<String, dynamic>?),
keyPredicateMatch: optionOf(json['keyPredicateMatch'] as Map<String, dynamic>?),
);
}

Expand Down
13 changes: 8 additions & 5 deletions l10n_mapper_generator/lib/l10n_mapper_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class L10nMapperGenerator extends Generator {
final bufferL10nHelper = StringBuffer();

bufferBuildContextExtension.writeln('extension $buildContextExtensionName on BuildContext {');
bufferAppLocalizationsExtension.writeln('extension $appLocalizationsExtensionName on ${classElement.displayName} {');
bufferAppLocalizationsExtension
.writeln('extension $appLocalizationsExtensionName on ${classElement.displayName} {');

bufferBuildContextExtension.writeln(' $className get _localizations => $className.of(this)!;');

Expand All @@ -69,14 +70,17 @@ class L10nMapperGenerator extends Generator {
"${nullable ? 'String?' : 'String'} parseL10n(String translationKey, {List<Object>? arguments}) {");

bufferBuildContextExtension.writeln('final localizations = $className.of(this)!;');
bufferBuildContextExtension.writeln('return L10nHelper.parseL10n(localizations, translationKey, arguments: arguments);');
bufferAppLocalizationsExtension.writeln('return L10nHelper.parseL10n(this, translationKey, arguments: arguments);');
bufferBuildContextExtension
.writeln('return L10nHelper.parseL10n(localizations, translationKey, arguments: arguments);');
bufferAppLocalizationsExtension
.writeln('return L10nHelper.parseL10n(this, translationKey, arguments: arguments);');

bufferBuildContextExtension.writeln('}');
bufferAppLocalizationsExtension.writeln('}');

bufferL10nHelper.writeln('class L10nHelper {');
bufferL10nHelper.writeln('static String parseL10n(AppLocalizations localizations, String translationKey, {List<Object>? arguments}) {');
bufferL10nHelper.writeln(
'static String parseL10n(AppLocalizations localizations, String translationKey, {List<Object>? arguments}) {');

bufferL10nHelper.writeln('const mapper = $mapperName();');
bufferL10nHelper.writeln('final object = mapper.toLocalizationMap(localizations)[translationKey];');
Expand Down Expand Up @@ -108,7 +112,6 @@ class L10nMapperGenerator extends Generator {
..write(bufferL10nHelper.toString());
}


// end of extension

// generate class
Expand Down

0 comments on commit 19f9556

Please sign in to comment.