Skip to content

Commit

Permalink
fix: Localization gets stripped when it contains a plural form (#34)
Browse files Browse the repository at this point in the history
* dependencies update
  • Loading branch information
FlutterOd committed Dec 22, 2023
1 parent 637cc73 commit c30421d
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 9 deletions.
21 changes: 15 additions & 6 deletions lib/src/crowdin_extractor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,20 @@ class Extractor {

final extractedPlurals = List.generate(pluralIds.length, (i) {
final extracted = findPlural(messageValue, pluralIds[i]);
var formattedMessage = message.placeholders.values.fold<String?>(
var formattedPlural = message.placeholders.values.fold<String?>(
extracted,
(extracted, placeholder) => extracted?.replaceAll(
'#${placeholder.name}#', '{${placeholder.name}}'),
);
String? formattedMessage = formattedPlural != null
? messageValue.replaceRange(messageValue.indexOf('{'),
messageValue.lastIndexOf('}') + 1, formattedPlural)
: formattedPlural;

for (final placeholder in message.placeholders.values) {
formattedMessage = formattedMessage?.replaceAll(
'#${placeholder.name}#', '{${placeholder.name}}');
}
return findPlaceholders(locale, message, formattedMessage, args);
});

Expand All @@ -163,20 +172,20 @@ class Extractor {
}

@visibleForTesting
String? findPlural(String formattedMessage, String pluralKey) {
final startIndex = formattedMessage.indexOf(pluralKey);
String? findPlural(String messageValue, String pluralKey) {
final startIndex = messageValue.indexOf(pluralKey);

/// Returns -1 if no match is found
if (startIndex == -1) {
return null;
}
final openingBraceIndex = formattedMessage.indexOf('{', startIndex);
final openingBraceIndex = messageValue.indexOf('{', startIndex);
if (openingBraceIndex == -1) {
return null;
}
final closingBraceIndex = formattedMessage.indexOf('}', openingBraceIndex);
final closingBraceIndex = messageValue.indexOf('}', openingBraceIndex);
if (closingBraceIndex == -1) {
return null;
}
return formattedMessage.substring(openingBraceIndex + 1, closingBraceIndex);
return messageValue.substring(openingBraceIndex + 1, closingBraceIndex);
}
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ dependencies:
flutter:
sdk: flutter
http: '>=0.13.3 <2.0.0'
intl: '>=0.17.0 <0.19.0'
intl: '>=0.17.0 <0.20.0'
shared_preferences: ^2.0.4
connectivity_plus: '>=2.1.0'
connectivity_plus: '>=2.1.0 <6.0.0'
yaml: ^3.1.1
meta: ^1.7.0
path: ^1.8.1
Expand Down
36 changes: 36 additions & 0 deletions test/crowdin_extractor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,42 @@ void main() {
expect(result, "{count,plural, =0{no nulls} other{null nulls}}");
});

test('get correct text for message with plural', () {
var thing = "wombat";
var result1 = extractor.getText('en', AppResourceBundle(testArb),
'nThings', {'count': 0, 'thing': thing});
expect(result1, "no wombats");
var result2 = extractor.getText('en', AppResourceBundle(testArb),
'nThings', {'count': 1, 'thing': thing});
expect(result2, "1 wombats");
var result3 = extractor.getText('en', AppResourceBundle(testArb),
'nThings', {'count': 5, 'thing': thing});
expect(result3, "5 wombats");
});

test('get correct text for message with variable and plural', () {
var variable = "Some text";
var thing = "wombat";
var result1 = extractor.getText(
'en',
AppResourceBundle(testArb),
'variable_nThings',
{'count': 0, 'variable': variable, 'thing': thing});
expect(result1, "Some text no wombats");
var result2 = extractor.getText(
'en',
AppResourceBundle(testArb),
'variable_nThings',
{'count': 1, 'variable': variable, 'thing': thing});
expect(result2, "Some text 1 wombats");
var result3 = extractor.getText(
'en',
AppResourceBundle(testArb),
'variable_nThings',
{'count': 4, 'variable': variable, 'thing': thing});
expect(result3, "Some text 4 wombats");
});

test('should return message.value for message without placeholders', () {
Message message = Message(AppResourceBundle(testArb), 'example', false);
var result = extractor.findPlaceholders('en', message, message.value);
Expand Down
3 changes: 2 additions & 1 deletion test/crowdin_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ void main() {
group('findPlaceholders test', () {
test('should return list of translation keys', () {
var keys = CrowdinGenerator.getKeys(testArb);
expect(keys, ['example', 'hello', 'nThings', 'counter']);
expect(
keys, ['example', 'hello', 'nThings', 'variable_nThings', 'counter']);
});

test('should return list of method parameters', () {
Expand Down
10 changes: 10 additions & 0 deletions test/test_arb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ var testArb = {
"thing": {"example": "wombat"}
}
},
"variable_nThings":
"{variable} {count,plural, =0{no {thing}s} other{{count} {thing}s}}",
"@variable_nThings": {
"description": "A plural message with an additional parameter",
"placeholders": {
"count": {"type": "int"},
"thing": {"type": "String"},
"variable": {"type": "String"}
}
},
"counter": "Counter: {value}",
"@counter": {
"description": "A message with a formatted int parameter",
Expand Down

0 comments on commit c30421d

Please sign in to comment.