Skip to content

Commit

Permalink
Automatically detect diffs import for the translation from Google Pla…
Browse files Browse the repository at this point in the history
…y with suffix _diffs.
  • Loading branch information
greymag committed Mar 5, 2024
1 parent 6efa903 commit efb5f07
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

* [L10n] `import_xml`: Support for .zip from Google Play as a source (argument `--path`).
* [L10n] `import_xml`: Automatically detect diffs import for the translation from Google Play with suffix `_diffs`.
* [L10n] `import_xml`: Translations are imported in alphabetical order.

## 1.6.7
Expand Down
34 changes: 26 additions & 8 deletions lib/commands/l10n/import_xml_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import 'package:path/path.dart' as path;
import 'package:xml/xml.dart';
import 'package:list_ext/list_ext.dart';

typedef _CompositeNameBuilder = String Function(String uidWithName);

/// Command to import translations from Google Play
/// to the project's xml files.
class ImportXmlCommand extends L10nCommandBase {
Expand Down Expand Up @@ -211,6 +213,12 @@ class ImportXmlCommand extends L10nCommandBase {
// but we don't know for sure
bool? useBaseName;
String? baseFileName;
_CompositeNameBuilder? compositeFilenameBuilder;

String defaultCompositeBuilder(String uidWithName) =>
'${uidWithName}_$projectUid.xml';
String diffsCompositeBuilder(String uidWithName) =>
defaultCompositeBuilder(uidWithName).asDiffsName();

// if multiple files - than it's in subdirectory,
// if single file - it's directly in root
Expand All @@ -225,8 +233,6 @@ class ImportXmlCommand extends L10nCommandBase {
final googlePlayLocale = name.replaceFirst('${translationUid}_', '');
final uidWithName = '${translationUid}_$googlePlayLocale';
if (projectUid != null) {
final compositeFilename = '${uidWithName}_$projectUid.xml';

bool checkBaseName(String? filename) {
if (filename == null) return false;
if (!fileExist(item, filename)) return false;
Expand All @@ -235,14 +241,22 @@ class ImportXmlCommand extends L10nCommandBase {
return true;
}

useBaseName ??= !fileExist(item, compositeFilename) &&
bool checkCompositeName(_CompositeNameBuilder builder) {
final filename = builder(uidWithName);
if (!fileExist(item, filename)) return false;
compositeFilenameBuilder = builder;
printVerbose('Set composite name: $filename');
return true;
}

useBaseName ??= !checkCompositeName(defaultCompositeBuilder) &&
!checkCompositeName(diffsCompositeBuilder) &&
(checkBaseName(srcFilename) ||
srcFilename != null &&
checkBaseName(
L10nUtils.getDiffsXmlFileName(srcFilename)));
checkBaseName(srcFilename?.asDiffsName()));

final sourceFilename =
useBaseName ? baseFileName! : compositeFilename;
final sourceFilename = useBaseName
? baseFileName!
: compositeFilenameBuilder!.call(uidWithName);

await import(item, sourceFilename, googlePlayLocale);
} else {
Expand Down Expand Up @@ -483,3 +497,7 @@ class ImportXmlCommand extends L10nCommandBase {
return unpackedDir;
}
}

extension _StrExt on String {
String asDiffsName() => L10nUtils.getDiffsXmlFileName(this);
}

0 comments on commit efb5f07

Please sign in to comment.