From 62377688c3687225bf5530c084b194afff8c9f90 Mon Sep 17 00:00:00 2001 From: James Leahy Date: Sun, 3 May 2020 13:45:00 +0200 Subject: [PATCH] Add basic plurals support. --- lib/flappy_translator.dart | 47 ++++++++++++++++++++++++++++++++++---- lib/template.dart | 3 +++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/lib/flappy_translator.dart b/lib/flappy_translator.dart index c4fa2ea..6d3ac5e 100644 --- a/lib/flappy_translator.dart +++ b/lib/flappy_translator.dart @@ -15,6 +15,7 @@ const String FIELDS_AREA_TEMPLATE_KEY = "/// Fields area"; const String SUPPORTED_LANGUAGES_AREA_TEMPLATE_KEY = "/// SupportedLanguages area"; const String PARAMETERS_REGEX = r"(\%[[0-9a-zA-Z]+]*\$(d|s))"; +const String PLURALS_REGEX = r'\|(.*?)\|'; const List RESERVED_WORDS = [ "assert", "default", @@ -237,11 +238,14 @@ class FlappyTranslator { String _addField(String key, String defaultWord, {bool dependsOnContext = false, String quoteString = '"'}) { - final RegExp regex = new RegExp(PARAMETERS_REGEX); - final bool hasParameters = regex.hasMatch(defaultWord); + final RegExp paramtersRegExp = RegExp(PARAMETERS_REGEX); + final RegExp pluralsRegExp = RegExp(PLURALS_REGEX); + final bool hasParameters = paramtersRegExp.hasMatch(defaultWord); + final bool hasPlural = pluralsRegExp.hasMatch(defaultWord); if (hasParameters) { String parameters = ""; - final List matches = regex.allMatches(defaultWord).toList(); + final List matches = + paramtersRegExp.allMatches(defaultWord).toList(); for (RegExpMatch match in matches) { final String parameterType = match.group(2) == "d" ? "int" : "String"; parameters += @@ -263,9 +267,44 @@ class FlappyTranslator { """; } + if (hasPlural) { + final plurals = pluralsRegExp + .firstMatch(defaultWord) + .group(0) + .replaceAll('|', '') + .split(':'); + if (plurals.length != 6) { + FlappyLogger.logError( + "Incorrect number of plurals given in $defaultWord. Must give 6."); + return null; + } + + String varName = + getParameterNameFromPlaceholder(matches.first.group(0)); + result += """ + final plurals = _pluralsRegExp.firstMatch(text).group(0).replaceAll('|', '').split(':'); + if(plurals.length != 6) { + return 'Issue with key $key. Incorrect number of plurals.'; + } + return Intl.plural( + $varName, + zero: text.replaceAll(_pluralsRegExp, plurals[0]), + one: text.replaceAll(_pluralsRegExp, plurals[1]), + two: text.replaceAll(_pluralsRegExp, plurals[2]), + few: text.replaceAll(_pluralsRegExp, plurals[3]), + many: text.replaceAll(_pluralsRegExp, plurals[4]), + other: text.replaceAll(_pluralsRegExp, plurals[5]), + ); + + """; + } else { + result += """ + return text; + """; + } + return result + """ - return text; } """; diff --git a/lib/template.dart b/lib/template.dart index cd63e58..6c64c2d 100644 --- a/lib/template.dart +++ b/lib/template.dart @@ -2,10 +2,13 @@ const String templateBegining = """ import 'dart:async'; import 'package:flutter/material.dart'; +import 'package:intl/intl.dart'; /// This class is generated by the flappy_translator package /// Please do not change anything manually in this file, instead re-generate it when changes are available class #CLASS_NAME# { + static final _pluralsRegExp = RegExp(r"\\\|(.*?)\\\|"); + /// Fields area static Map _localizedValues;