-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for formatting from 0.1.0
- Loading branch information
1 parent
52e44de
commit f9d5bd4
Showing
12 changed files
with
334 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import 'case_util.dart'; | ||
|
||
class FileWriter { | ||
static final formatRegex = RegExp(r'\%(\d*)\$([a-z])'); | ||
static const REGEX_INDEX_GROUP_INDEX = 1; | ||
static const REGEX_TYPE_GROUP_INDEX = 2; | ||
|
||
static void buildTranslationFunction( | ||
StringBuffer sb, String key, String value) { | ||
if (value == null || value.isEmpty) { | ||
_buildDefaultFunction(sb, key, value); | ||
return; | ||
} | ||
final allMatched = formatRegex.allMatches(value); | ||
if (allMatched == null || allMatched.isEmpty) { | ||
_buildDefaultFunction(sb, key, value); | ||
return; | ||
} | ||
try { | ||
final tmpSb = StringBuffer(' String ${CaseUtil.getCamelcase(key)}('); | ||
|
||
final validMatcher = List<RegExpMatch>(); | ||
allMatched.forEach((match) { | ||
final sameTypeMatch = validMatcher.where((validMatch) => | ||
validMatch.group(REGEX_INDEX_GROUP_INDEX) == | ||
match.group(REGEX_INDEX_GROUP_INDEX)); | ||
if (sameTypeMatch.isNotEmpty && | ||
sameTypeMatch.first.group(REGEX_TYPE_GROUP_INDEX) != | ||
match.group(REGEX_TYPE_GROUP_INDEX)) { | ||
throw Exception( | ||
'$key contains a value with more than 1 argument with the same index but different type'); | ||
} | ||
if (validMatcher | ||
.where((validMatch) => validMatch.group(0) == match.group(0)) | ||
.isEmpty) { | ||
validMatcher.add(match); | ||
} | ||
}); | ||
|
||
validMatcher.asMap().forEach((index, match) { | ||
final argument = _getArgument(key, match); | ||
tmpSb.write(argument); | ||
if (index != validMatcher.length - 1) { | ||
tmpSb.write(', '); | ||
} | ||
}); | ||
tmpSb.write(") => _t('$key', args: ["); | ||
validMatcher.asMap().forEach((index, match) { | ||
if (index != 0) { | ||
tmpSb.write(', '); | ||
} | ||
tmpSb.write('arg${match.group(REGEX_INDEX_GROUP_INDEX)}'); | ||
}); | ||
tmpSb..writeln(']);')..writeln(); | ||
sb.write(tmpSb.toString()); | ||
} on Exception catch (e) { | ||
print(e); | ||
_buildDefaultFunction(sb, key, value); | ||
} | ||
} | ||
|
||
static String _getArgument(String key, RegExpMatch match) { | ||
final index = match.group(REGEX_INDEX_GROUP_INDEX); | ||
final type = match.group(REGEX_TYPE_GROUP_INDEX); | ||
if (type == 's') { | ||
return 'String arg$index'; | ||
} else if (type == 'd') { | ||
return 'num arg$index'; | ||
} | ||
throw Exception( | ||
'Unsupported argument type for $key. Supported types are -> s,d. Create a github ticket for support -> https://github.com/vanlooverenkoen/locale_gen/issues'); | ||
} | ||
|
||
static void _buildDefaultFunction(StringBuffer sb, String key, String value) { | ||
sb | ||
..writeln(" String get ${CaseUtil.getCamelcase(key)} => _t('$key');") | ||
..writeln(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
{ | ||
"app_title": "Locale Gen EN", | ||
"test": "Testing in english" | ||
"test": "Testing in english", | ||
"test_arg1": "Testing argument %1$s", | ||
"test_arg2": "Testing argument %1$d", | ||
"test_arg3": "Testing argument %1$s %2$d" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
{ | ||
"app_title": "Locale Gen NL", | ||
"test": "Test in het Nederlands" | ||
"test": "Test in het Nederlands", | ||
"test_arg1": "Test argument %1$s", | ||
"test_arg2": "Test argument %1$d", | ||
"test_arg3": "Test argument %1$s %2$d" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.