Skip to content

Commit 01c5055

Browse files
committed
working on document importer
1 parent 90856e1 commit 01c5055

6 files changed

+112
-62
lines changed

lib/gsheet_to_arb.dart

-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ library gsheet_to_arb;
99
export 'src/gsheet_to_arb.dart';
1010
export 'src/config/plugin_config.dart';
1111
export 'src/config/plugin_config_manager.dart';
12-
export 'src/output/arb_to_class_generator.dart';
1312
export 'src/utils/log.dart';

lib/src/output/arb_to_class_generator.dart lib/src/dart/arb_to_dart_generator.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import 'package:dart_style/dart_style.dart';
1111
import 'package:gsheet_to_arb/src/arb/arb.dart';
1212
import 'package:recase/recase.dart';
1313

14-
class TranslationsGenerator {
14+
class ArbToDartGenerator {
1515
void buildTranslations(
1616
ArbDocument document, String directory, String className) {
1717
var translationClass = Class((ClassBuilder builder) {

lib/src/gsheet/ghseet_importer.dart

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import 'package:gsheet_to_arb/gsheet_to_arb.dart';
2+
3+
import 'package:gsheet_to_arb/src/translation_document.dart';
4+
5+
import 'package:googleapis/sheets/v4.dart';
6+
import 'package:googleapis_auth/auth_io.dart';
7+
8+
class _SheetColumns {
9+
static int key = 0;
10+
static int description = 1;
11+
static int first_language_key = 2;
12+
}
13+
14+
class _SheetRows {
15+
static int header_row = 0;
16+
static int first_translation_row = 1;
17+
}
18+
19+
class GSheetImporter {
20+
final AuthConfig auth;
21+
final String categoryPrefix;
22+
23+
GSheetImporter({this.auth, this.categoryPrefix});
24+
25+
Future<TranslationsDocument> import(String documentId) async {
26+
var authClient = await _getAuthClient(auth);
27+
var sheetsApi = SheetsApi(authClient);
28+
var spreadsheet =
29+
await sheetsApi.spreadsheets.get(documentId, includeGridData: true);
30+
final document = _importFrom(spreadsheet);
31+
authClient.close();
32+
return document;
33+
}
34+
35+
Future<AuthClient> _getAuthClient(AuthConfig auth) async {
36+
final scopes = [SheetsApi.SpreadsheetsReadonlyScope];
37+
var authClient;
38+
if (auth.oauthClientId != null) {
39+
void clientAuthPrompt(String url) {
40+
Log.i(
41+
'Please go to the following URL and grant Google Spreadsheet access:\n\t=> $url\n');
42+
}
43+
44+
final client = auth.oauthClientId;
45+
var id = ClientId(client.clientId, client.clientSecret);
46+
authClient = await clientViaUserConsent(id, scopes, clientAuthPrompt);
47+
} else if (auth.serviceAccountKey != null) {
48+
final service = auth.serviceAccountKey;
49+
var credentials = ServiceAccountCredentials(service.clientEmail,
50+
ClientId(service.clientId, null), service.privateKey);
51+
authClient = await clientViaServiceAccount(credentials, scopes);
52+
}
53+
return authClient;
54+
}
55+
56+
Future<TranslationsDocument> _importFrom(Spreadsheet spreadsheet) async {
57+
Log.i('Opening ${spreadsheet.spreadsheetUrl}');
58+
59+
var sheet = spreadsheet.sheets[0];
60+
var rows = sheet.data[0].rowData;
61+
var header = rows[0];
62+
var headerValues = header.values;
63+
64+
final languages = List<String>();
65+
66+
for (var column = _SheetRows.first_translation_row;
67+
column < headerValues.length;
68+
column++) {
69+
final language = headerValues[column].formattedValue;
70+
languages.add(language);
71+
}
72+
73+
return TranslationsDocument(
74+
languages: languages, //
75+
items: null // TODO
76+
);
77+
}
78+
}

lib/src/gsheet_to_arb.dart

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import '../gsheet_to_arb.dart';
1+
import 'package:gsheet_to_arb/src/parser/translation_parser.dart';
2+
import 'package:gsheet_to_arb/src/utils/log.dart';
3+
24
import 'arb/arb_serializer.dart';
3-
import 'arb/intl_translation_helper.dart';
4-
import 'gsheet/sheet_parser.dart';
5+
import 'config/plugin_config.dart';
6+
import 'gsheet/ghseet_importer.dart';
57

68
class GSheetToArb {
79
final GsheetToArbConfig config;
@@ -17,11 +19,14 @@ class GSheetToArb {
1719
final auth = gsheet.auth;
1820
final documentId = gsheet.documentId;
1921

20-
final sheetParser =
21-
SheetParser(auth: auth, categoryPrefix: gsheet.categoryPrefix);
22+
//
23+
final importer =
24+
GSheetImporter(auth: auth, categoryPrefix: gsheet.categoryPrefix);
25+
final document = await importer.import(documentId);
2226

2327
// Parse ARB
24-
final arbBundle = await sheetParser.parseSheet(documentId);
28+
final sheetParser = SheetParser();
29+
final arbBundle = await sheetParser.parseDocument(document);
2530

2631
// Save ARB
2732
_arbSerializer.saveArbBundle(arbBundle, config.outputDirectoryPath);

lib/src/gsheet/sheet_parser.dart lib/src/parser/translation_parser.dart

+7-54
Original file line numberDiff line numberDiff line change
@@ -6,67 +6,18 @@
66

77
import 'dart:async';
88

9-
import 'package:googleapis/sheets/v4.dart';
10-
import 'package:googleapis_auth/auth_io.dart';
119
import 'package:gsheet_to_arb/gsheet_to_arb.dart';
1210
import 'package:gsheet_to_arb/src/arb/arb.dart';
1311
import 'package:gsheet_to_arb/src/arb/arb_generator.dart';
14-
import 'package:gsheet_to_arb/src/utils/log.dart';
15-
16-
class SheetColumns {
17-
static int key = 0;
18-
static int description = 1;
19-
static int first_language_key = 2;
20-
}
12+
import 'package:gsheet_to_arb/src/translation_document.dart';
2113

22-
class SheetRows {
23-
static int first_translation_row = 1;
24-
}
14+
import 'package:gsheet_to_arb/src/utils/log.dart';
2515

2616
class SheetParser {
27-
final AuthConfig auth;
28-
final String categoryPrefix;
29-
30-
SheetParser({this.auth, this.categoryPrefix});
31-
32-
Future<ArbBundle> parseSheet(String documentId) async {
33-
var authClient = await _getAuthClient(auth);
34-
var arbBundle = await _parseSheetWithAuth(authClient, documentId);
35-
return arbBundle;
36-
}
37-
38-
Future<AuthClient> _getAuthClient(AuthConfig auth) async {
39-
final scopes = [SheetsApi.SpreadsheetsReadonlyScope];
40-
var authClient;
41-
if (auth.oauthClientId != null) {
42-
void clientAuthPrompt(String url) {
43-
Log.i(
44-
'Please go to the following URL and grant Google Spreadsheet access:\n\t=> $url\n');
45-
}
46-
47-
final client = auth.oauthClientId;
48-
var id = ClientId(client.clientId, client.clientSecret);
49-
authClient = await clientViaUserConsent(id, scopes, clientAuthPrompt);
50-
} else if (auth.serviceAccountKey != null) {
51-
final service = auth.serviceAccountKey;
52-
var credentials = ServiceAccountCredentials(service.clientEmail,
53-
ClientId(service.clientId, null), service.privateKey);
54-
authClient = await clientViaServiceAccount(credentials, scopes);
55-
}
56-
return authClient;
57-
}
58-
59-
Future<ArbBundle> _parseSheetWithAuth(
60-
AuthClient client, String documentId) async {
61-
var sheetsApi = SheetsApi(client);
62-
var spreadsheet =
63-
await sheetsApi.spreadsheets.get(documentId, includeGridData: true);
64-
var bundle = _parseSpreadsheet(spreadsheet);
65-
client.close();
66-
return bundle;
67-
}
17+
Future<ArbBundle> parseDocument(TranslationsDocument document) async {
18+
return null;
6819

69-
ArbBundle _parseSpreadsheet(Spreadsheet spreadsheet) {
20+
/*
7021
Log.i('Opening ${spreadsheet.spreadsheetUrl}');
7122
7223
var sheet = spreadsheet.sheets[0];
@@ -149,6 +100,7 @@ class SheetParser {
149100
150101
_addEntry(builder, key: key, attributes: attributes, value: value);
151102
}
103+
152104
}
153105
154106
// complete plural parser
@@ -166,6 +118,7 @@ class SheetParser {
166118
var documents = <ArbDocument>[];
167119
builders.forEach((_, builder) => documents.add(builder.build()));
168120
return ArbBundle(documents);
121+
*/
169122
}
170123

171124
void _addEntry(ArbDocumentBuilder builder,

lib/src/translation_document.dart

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class TranslationsDocument {
2+
final List<String> languages;
3+
final List<TranslationRow> items;
4+
5+
TranslationsDocument({this.languages, this.items});
6+
}
7+
8+
class TranslationRow {
9+
final String key;
10+
final String description;
11+
final String category;
12+
final List<String> values;
13+
14+
TranslationRow({this.key, this.description, this.category, this.values});
15+
}

0 commit comments

Comments
 (0)