Skip to content

Commit

Permalink
feat: ignore empty plural / context nodes when `fallback_strategy: ba…
Browse files Browse the repository at this point in the history
…se_locale` is used
  • Loading branch information
Tienisto committed Aug 8, 2022
1 parent fb6eb03 commit 8abdec9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions slang/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.7.0

- feat: ignore empty plural / context nodes when `fallback_strategy: base_locale` is used
- feat: add `coverage:ignore-file` to generated file and ignore every lint

## 2.6.2

- feat: add Russian plural resolver (thanks to @LuckyWins)
Expand Down
13 changes: 13 additions & 0 deletions slang/lib/builder/builder/translation_model_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class TranslationModelBuilder {
// Assumption: They are basic linked translations without parameters
// Reason: Not all TextNodes are built, so final parameters are unknown
final resultNodeTree = _parseMapNode(
locale: locale,
parentPath: '',
curr: map,
config: buildConfig,
Expand Down Expand Up @@ -158,6 +159,7 @@ class TranslationModelBuilder {
/// Takes the [curr] map which is (a part of) the raw tree from json / yaml
/// and returns the node model.
static Map<String, Node> _parseMapNode({
required I18nLocale locale,
required String parentPath,
required Map<String, dynamic> curr,
required BuildConfig config,
Expand Down Expand Up @@ -229,6 +231,7 @@ class TranslationModelBuilder {
for (int i = 0; i < value.length; i++) i.toString(): value[i],
};
children = _parseMapNode(
locale: locale,
parentPath: currPath,
curr: listAsMap,
config: config,
Expand All @@ -249,6 +252,7 @@ class TranslationModelBuilder {
} else {
// key: { ...value }
final children = _parseMapNode(
locale: locale,
parentPath: currPath,
curr: value,
config: config,
Expand All @@ -269,6 +273,15 @@ class TranslationModelBuilder {
if (detectedType.nodeType == _DetectionType.context ||
detectedType.nodeType == _DetectionType.pluralCardinal ||
detectedType.nodeType == _DetectionType.pluralOrdinal) {
if (children.isEmpty) {
switch (config.fallbackStrategy) {
case FallbackStrategy.none:
throw '"$currPath" in <${locale.languageTag}> is empty but it is marked for pluralization. Define "fallback_strategy: base_locale" to ignore this node.';
case FallbackStrategy.baseLocale:
return;
}
}

if (detectedType.nodeType == _DetectionType.pluralCardinal) {
cardinalNotifier();
} else if (detectedType.nodeType == _DetectionType.pluralOrdinal) {
Expand Down

0 comments on commit 8abdec9

Please sign in to comment.