Skip to content

Commit 801a1c2

Browse files
committed
add add_context_prefix flag
1 parent 56dd68f commit 801a1c2

7 files changed

+72
-55
lines changed

bin/import.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void main(List<String> args) async {
5454
_checkAuthConfig(config.gsheet);
5555

5656
final gsheetToArb = GSheetToArb(config: config);
57-
gsheetToArb.build(generateDartCode: config.generateCode != false);
57+
gsheetToArb.build();
5858
}
5959

6060
void _checkAuthConfig(GoogleSheetConfig config) {

example/pubspec.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ dev_dependencies:
1717
gsheet_to_arb:
1818
arb_file_prefix: 'intl'
1919
localization_file_name: 'l10n'
20-
output_directory: 'lib/l10n'
20+
output_directory: 'lib/l10n'
21+
add_context_prefix: false
2122
gsheet:
2223
auth_file: './gsheet_to_arb.yaml'
2324
category_prefix: "# "

lib/src/config/plugin_config.dart

+5-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ class GsheetToArbConfig {
4242
@JsonKey(name: 'generate_code')
4343
bool generateCode;
4444

45+
@JsonKey(name: 'add_context_prefix')
46+
bool addContextPrefix;
47+
4548
@JsonKey(name: 'gsheet')
4649
GoogleSheetConfig gsheet;
4750

@@ -50,7 +53,8 @@ class GsheetToArbConfig {
5053
this.arbFilePrefix,
5154
this.gsheet,
5255
this.localizationFileName,
53-
this.generateCode});
56+
this.generateCode,
57+
this.addContextPrefix});
5458

5559
factory GsheetToArbConfig.fromJson(Map<String, dynamic> json) =>
5660
_$GsheetToArbConfigFromJson(json);

lib/src/config/plugin_config.g.dart

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/config/plugin_config_manager.dart

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ class PluginConfigManager {
2828
config.gsheet.auth = AuthConfig.fromJson(authConfig);
2929
}
3030

31+
if (config != null) {
32+
config.generateCode = config.generateCode ?? true;
33+
config.addContextPrefix = config.addContextPrefix ?? false;
34+
}
35+
3136
return config;
3237
}
3338

lib/src/dart/arb_to_dart_generator.dart

+49-45
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@ import 'package:intl_translation/src/intl_message.dart';
1616
import 'package:petitparser/petitparser.dart';
1717

1818
class ArbToDartGenerator {
19+
final bool addContextPrefix;
20+
1921
final intlTranslation = IntlTranslationGenerator();
2022

23+
ArbToDartGenerator({this.addContextPrefix = false});
24+
2125
void generateDartClasses(
22-
ArbBundle bundle, String outputDirectoryPath, String className) {
26+
ArbBundle bundle, String outputDirectoryPath, String className,
27+
{bool addContextPrefix}) {
2328
_buildIntlListFile(bundle.documents.first, outputDirectoryPath, className);
2429

2530
intlTranslation.generateLookupTables(outputDirectoryPath, className);
@@ -52,65 +57,64 @@ class ArbToDartGenerator {
5257
}
5358

5459
Method _getResourceMethod(ArbResource resource) {
55-
if (resource.placeholders.isNotEmpty) {
56-
return _getResourceFullMethod(resource);
57-
} else {
58-
return _getResourceGetter(resource);
59-
}
60-
}
61-
62-
Method _getResourceFullMethod(ArbResource resource) {
6360
return Method((MethodBuilder builder) {
6461
final key = resource.key;
65-
final value = _fixSpecialCharacters(resource.value);
6662
final description =
6763
_fixSpecialCharacters(resource.attributes['description'] ??= key);
6864

69-
var args = <String>[];
70-
resource.placeholders.forEach((ArbResourcePlaceholder placeholder) {
71-
builder.requiredParameters.add(Parameter((ParameterBuilder builder) {
72-
args.add(placeholder.name);
73-
final argumentType =
74-
placeholder.type == ArbResourcePlaceholder.typeNum
75-
? 'int'
76-
: 'String';
77-
builder
78-
..name = placeholder.name
79-
..type = Reference(argumentType);
80-
}));
81-
});
65+
final methodName =
66+
(addContextPrefix ? '${resource.context.toLowerCase()}_' : '') +
67+
ReCase(key).camelCase;
8268

8369
builder
84-
..name = _getMethodName(key)
70+
..name = methodName
8571
..returns = const Reference('String')
8672
..lambda = true
87-
..docs.add('\t/// ${description}')
88-
..body = Code(
89-
_getCode(value, key: key, args: args, description: description));
73+
..docs.add('/// ${description.replaceAll("\\n", "\n/// ")}');
74+
75+
if (resource.placeholders.isNotEmpty) {
76+
return _getResourceFullMethod(resource, builder);
77+
} else {
78+
return _getResourceGetter(resource, builder);
79+
}
9080
});
9181
}
9282

93-
// """Intl.message('${value}', name: '$key', args: [${args.join(", ")}], desc: '${description}')"""
94-
95-
Method _getResourceGetter(ArbResource resource) {
96-
return Method((MethodBuilder builder) {
97-
final key = resource.key;
98-
final value = _fixSpecialCharacters(resource.value);
99-
final description =
100-
_fixSpecialCharacters(resource.attributes['description'] ??= key);
101-
102-
builder
103-
..name = _getMethodName(key)
104-
..type = MethodType.getter
105-
..returns = const Reference('String')
106-
..lambda = true
107-
..body = Code(
108-
'''Intl.message('${value}', name: '${key}', desc: '${description}')''')
109-
..docs.add('/// ${description.replaceAll("\\n", "\n/// ")}');
83+
void _getResourceFullMethod(ArbResource resource, MethodBuilder builder) {
84+
final key = resource.key;
85+
final value = _fixSpecialCharacters(resource.value);
86+
final description =
87+
_fixSpecialCharacters(resource.attributes['description'] ??= key);
88+
89+
var args = <String>[];
90+
resource.placeholders.forEach((ArbResourcePlaceholder placeholder) {
91+
builder.requiredParameters.add(Parameter((ParameterBuilder builder) {
92+
args.add(placeholder.name);
93+
final argumentType = placeholder.type == ArbResourcePlaceholder.typeNum
94+
? 'int'
95+
: 'String';
96+
builder
97+
..name = placeholder.name
98+
..type = Reference(argumentType);
99+
}));
110100
});
101+
102+
builder
103+
..body =
104+
Code(_getCode(value, key: key, args: args, description: description));
111105
}
112106

113-
String _getMethodName(String key) => ReCase(key).camelCase;
107+
void _getResourceGetter(ArbResource resource, MethodBuilder builder) {
108+
final key = resource.key;
109+
final value = _fixSpecialCharacters(resource.value);
110+
final description =
111+
_fixSpecialCharacters(resource.attributes['description'] ??= key);
112+
113+
builder
114+
..type = MethodType.getter
115+
..body = Code(
116+
'''Intl.message('${value}', name: '${key}', desc: '${description}')''');
117+
}
114118

115119
///
116120
/// intl_translation

lib/src/gsheet_to_arb.dart

+8-7
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,27 @@ class GSheetToArb {
1212

1313
GSheetToArb({this.config});
1414

15-
void build({bool generateDartCode = false}) async {
15+
void build() async {
1616
final gsheet = config.gsheet;
1717
final auth = gsheet.auth;
1818
final documentId = gsheet.documentId;
1919

20-
//
20+
// import TranslationsDocument
2121
final importer =
2222
GSheetImporter(auth: auth, categoryPrefix: gsheet.categoryPrefix);
2323
final document = await importer.import(documentId);
2424

25-
// Parse ARB
25+
// Parse TranslationsDocument to ArbBundle
2626
final sheetParser = TranslationParser();
2727
final arbBundle = await sheetParser.parseDocument(document);
2828

29-
// Save ARB
29+
// Save ArbBundle
3030
_arbSerializer.saveArbBundle(arbBundle, config.outputDirectoryPath);
3131

32-
// Generate Code
33-
if (generateDartCode) {
34-
final generator = ArbToDartGenerator();
32+
// Generate Code from ArbBundle
33+
if (config.generateCode) {
34+
final generator =
35+
ArbToDartGenerator(addContextPrefix: config.addContextPrefix);
3536
generator.generateDartClasses(
3637
arbBundle, config.outputDirectoryPath, config.localizationFileName);
3738
}

0 commit comments

Comments
 (0)