Skip to content
Merged
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
2 changes: 2 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ app.*.map.json
/android/app/release

*.env

/lib/l10n/*
12 changes: 9 additions & 3 deletions lib/src/gen/crowdin_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,15 @@ class CrowdinLocalization extends ${l10nConfig.outputClass} {
buffer.writeln(
" String get $key => Crowdin.getText(localeName, '$key') ?? _fallbackTexts.$key;");
} else {
var params = generateMethodParameters(message).join(', ');
var values =
placeholders.map((placeholder) => placeholder.name).join(', ');
var paramsList = generateMethodParameters(message);
var params = l10nConfig.useNamedParameters
? '{${paramsList.map((p) => 'required $p').join(', ')}}'
: paramsList.join(', ');
var values = l10nConfig.useNamedParameters
? placeholders
.map((placeholder) => '${placeholder.name}: ${placeholder.name}')
.join(', ')
: placeholders.map((placeholder) => placeholder.name).join(', ');
var args = placeholders
.map((placeholder) => '\'${placeholder.name}\':${placeholder.name}')
.join(', ');
Expand Down
5 changes: 5 additions & 0 deletions lib/src/gen/l10n_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class L10nConfig {
String templateArbFile;
String outputClass;
bool syntheticPackage;
bool useNamedParameters;

L10nConfig({
required this.arbDir,
Expand All @@ -19,6 +20,7 @@ class L10nConfig {
required this.outputDir,
required this.outputClass,
this.syntheticPackage = false,
this.useNamedParameters = false,
});

String get finalOutputDir => syntheticPackage
Expand All @@ -43,13 +45,16 @@ class L10nConfig {

bool syntheticPackage = yamlGenConfig['synthetic-package'] ?? false;

bool useNamedParameters = yamlGenConfig['use-named-parameters'] ?? false;

return L10nConfig(
arbDir: arbDir,
templateArbFile: templateArbFile,
outputClass: outputClass,
outputDir: outputDir,
syntheticPackage: syntheticPackage,
outputLocalizationFile: outputLocalizationFile,
useNamedParameters: useNamedParameters,
);
} else {
throw Exception('No l10n.yaml file');
Expand Down
29 changes: 29 additions & 0 deletions test/crowdin_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:io';

import 'package:crowdin_sdk/src/common/gen_l10n_types.dart';
import 'package:crowdin_sdk/src/gen/crowdin_generator.dart';
import 'package:crowdin_sdk/src/gen/l10n_config.dart';
import 'package:flutter_test/flutter_test.dart';

import 'test_arb.dart';
Expand All @@ -28,5 +29,33 @@ void main() {
String? pluralType = specifyPluralType('int', platformVersion);
expect(result, ['$pluralType count', 'Object thing']);
});

test(
'should generate named parameters when use-named-parameters is enabled',
() {
final config = L10nConfig(
arbDir: 'lib/l10n',
templateArbFile: 'app_en.arb',
outputLocalizationFile: 'app_localizations.dart',
outputDir: null,
outputClass: 'AppLocalizations',
syntheticPackage: true,
useNamedParameters: true,
);

final output = generationContent(
keys: CrowdinGenerator.getKeys(testArb),
arbResource: testArb,
l10nConfig: config,
);

expect(output, contains('String hello({required'));
expect(output, contains('_fallbackTexts.hello(userName: userName)'));

expect(output, contains('String nThings({required'));
expect(output, contains('_fallbackTexts.nThings('));
expect(output, contains('count: count'));
expect(output, contains('thing: thing'));
});
});
}
Loading