Repository: jmrozanec/cron-utils
Pull request #303 (2017-12-07 09:01:17)

#3 Added a small example (for english and german) how we could try to get better translations.


Patch:
diff --git a/src/main/java/com/cronutils/descriptor/refactor/TimeDescriptor.java b/src/main/java/com/cronutils/descriptor/refactor/TimeDescriptor.java
new file mode 100644
index 0000000..53b58c1
--- /dev/null
+++ b/src/main/java/com/cronutils/descriptor/refactor/TimeDescriptor.java
@@ -0,0 +1,88 @@
+package com.cronutils.descriptor.refactor;
+
+import java.text.ChoiceFormat;
+import java.text.Format;
+import java.text.MessageFormat;
+import java.text.NumberFormat;
+import java.util.Locale;
+import java.util.Map;
+import java.util.ResourceBundle;
+
+import com.cronutils.model.Cron;
+import com.cronutils.model.field.CronField;
+import com.cronutils.model.field.CronFieldName;
+import com.cronutils.model.field.expression.Always;
+import com.cronutils.model.field.expression.Every;
+import com.cronutils.model.field.expression.On;
+import com.cronutils.utils.Preconditions;
+import com.cronutils.utils.StringUtils;
+
+public class TimeDescriptor {
+
+    private final ResourceBundle resourceBundle;
+
+    public TimeDescriptor(final ResourceBundle resourceBundle) {
+        this.resourceBundle = Preconditions.checkNotNull(resourceBundle, "The resource bundle must not be null");
+    }
+
+
+    public String describe(final Cron cron) {
+        return describe(cron.retrieveFieldsAsMap());
+    }
+
+    private String describe(final Map<CronFieldName, CronField> expressions) {
+
+        if (expressions.containsKey(CronFieldName.SECOND)) {
+            final CronField cronField = expressions.get(CronFieldName.SECOND);
+            if (cronField.getExpression() instanceof Always) {
+                return describeEverySecond(1);
+            } else if (cronField.getExpression() instanceof On) {
+                return describeAtSecond(((On)cronField.getExpression()).getTime().getValue());
+            } else if (cronField.getExpression() instanceof Every) {
+                return describeEverySecond(((Every)cronField.getExpression()).getPeriod().getValue());
+            }
+        }
+
+
+        return StringUtils.EMPTY;
+    }
+
+    private String describeEverySecond(final int second) {
+        final double[] secondsLimit = {1, 2};
+        final String[] secondsStrings = {
+                resourceBundle.getString("oneSecond"),
+                resourceBundle.getString("multipleSeconds")
+        };
+        final double[] everyLimit = {1,2};
+        final String[] everyStrings = {
+                resourceBundle.getString("every_one"),
+                resourceBundle.getString("every_multi")
+        };
+
+        final ChoiceFormat secondsChoiceFormat = new ChoiceFormat(secondsLimit, secondsStrings);
+        final ChoiceFormat everyChoiceFormat = new ChoiceFormat(everyLimit, everyStrings);
+        final String pattern = resourceBundle.getString("pattern_every_seconds");
+
+        final MessageFormat messageFormat = new MessageFormat(pattern, Locale.UK);
+
+        final Format[] formats = { everyChoiceFormat, secondsChoiceFormat, NumberFormat.getInstance() };
+        messageFormat.setFormats(formats);
+        final Object[] messageArguments = {second, second, second };
+        final String result = messageFormat.format(messageArguments);
+        return result;
+    }
+
+    private String describeAtSecond(final int second) {
+
+        final String pattern = resourceBundle.getString("pattern_at_second");
+
+        final MessageFormat messageFormat = new MessageFormat(pattern, Locale.UK);
+        final Format[] formats = {NumberFormat.getInstance()};
+        messageFormat.setFormats(formats);
+
+        final Object[] messageArguments = { second };
+        final String result = messageFormat.format(messageArguments);
+        return result;
+    }
+
+}
diff --git a/src/main/resources/CronUtilsI18N.properties b/src/main/resources/CronUtilsI18N.properties
index eb7629b..fe341d9 100644
--- a/src/main/resources/CronUtilsI18N.properties
+++ b/src/main/resources/CronUtilsI18N.properties
@@ -21,3 +21,11 @@ last=last
 the_nearest_weekday_to_the=the nearest weekday to the
 last_day_of_month=last day of month
 last_weekday_of_month=last weekday of month
+
+pattern_every_seconds = {0} {1}
+every_one=every
+every_multi=every
+oneSecond=second
+multipleSeconds={2} seconds
+
+pattern_at_second=at second {0}
\ No newline at end of file
diff --git a/src/main/resources/CronUtilsI18N_de.properties b/src/main/resources/CronUtilsI18N_de.properties
index b9c4289..af5241e 100644
--- a/src/main/resources/CronUtilsI18N_de.properties
+++ b/src/main/resources/CronUtilsI18N_de.properties
@@ -21,3 +21,12 @@ last=letzten
 the_nearest_weekday_to_the=am n\ufffdchsten Werktag nach dem 
 last_day_of_month=letzten Tag des Monats
 last_weekday_of_month=letzten Werktag des Monats
+
+
+pattern_every_seconds = {0} {1}
+every_one=jede
+every_multi=alle
+oneSecond=sekunde
+multipleSeconds={2} sekunden
+
+pattern_at_second=at second {0}
\ No newline at end of file


Test patch:
diff --git a/src/test/java/com/cronutils/utils/descriptor/TestDescriptor.java b/src/test/java/com/cronutils/utils/descriptor/TestDescriptor.java
index 0d3f982..8b494c2 100644
--- a/src/test/java/com/cronutils/utils/descriptor/TestDescriptor.java
+++ b/src/test/java/com/cronutils/utils/descriptor/TestDescriptor.java
@@ -1,9 +1,13 @@
 package com.cronutils.utils.descriptor;
 
+import java.util.Locale;
+import java.util.ResourceBundle;
+
 import org.junit.Ignore;
 import org.junit.Test;
 
 import com.cronutils.descriptor.CronDescriptor;
+import com.cronutils.descriptor.refactor.TimeDescriptor;
 import com.cronutils.model.Cron;
 import com.cronutils.model.CronType;
 import com.cronutils.model.definition.CronDefinition;
@@ -13,7 +17,7 @@ import com.cronutils.parser.CronParser;
 import static org.junit.Assert.assertEquals;
 
 //FIXME https://github.com/jmrozanec/cron-utils/issues/3
-@Ignore
+
 public class TestDescriptor {
 
     private final CronDescriptor descriptor = CronDescriptor.instance();
@@ -27,6 +31,7 @@ public class TestDescriptor {
     //every 2 days starting on the 9th,
     //every 2 months starting in October,
     //every 2 years starting in 2017
+    @Ignore
     @Test
     public void testFull() {
         final Cron cron = getCron("3/4 5/6 7/8 9/2 10/2 ? 2017");
@@ -43,7 +48,43 @@ public class TestDescriptor {
     @Test
     public void testEverySecond() {
         final Cron cron = getCron("* * * * * ? *");
-        assertEquals("every second", descriptor.describe(cron));
+        final TimeDescriptor t = new TimeDescriptor(ResourceBundle.getBundle("CronUtilsI18N", Locale.UK));
+        assertEquals("every second", t.describe(cron));
+    }
+
+    @Test
+    public void testEvery1Second() {
+        final Cron cron = getCron("*/1 * * * * ? *");
+        final TimeDescriptor t = new TimeDescriptor(ResourceBundle.getBundle("CronUtilsI18N", Locale.UK));
+        assertEquals("every second", t.describe(cron));
+    }
+
+    @Test
+    public void testEvery2Seconds() {
+        final Cron cron = getCron("*/2 * * * * ? *");
+        final TimeDescriptor t = new TimeDescriptor(ResourceBundle.getBundle("CronUtilsI18N", Locale.UK));
+        assertEquals("every 2 seconds", t.describe(cron));
+    }
+
+    @Test
+    public void testEverySecondGerman() {
+        final Cron cron = getCron("* * * * * ? *");
+        final TimeDescriptor t = new TimeDescriptor(ResourceBundle.getBundle("CronUtilsI18N", Locale.GERMAN));
+        assertEquals("jede sekunde", t.describe(cron));
+    }
+
+    @Test
+    public void testEvery1SecondGerman() {
+        final Cron cron = getCron("*/1 * * * * ? *");
+        final TimeDescriptor t = new TimeDescriptor(ResourceBundle.getBundle("CronUtilsI18N", Locale.GERMAN));
+        assertEquals("jede sekunde", t.describe(cron));
+    }
+
+    @Test
+    public void testEvery2SecondsGerman() {
+        final Cron cron = getCron("*/2 * * * * ? *");
+        final TimeDescriptor t = new TimeDescriptor(ResourceBundle.getBundle("CronUtilsI18N", Locale.GERMAN));
+        assertEquals("alle 2 sekunden", t.describe(cron));
     }
 
     @Test
@@ -68,6 +109,7 @@ public class TestDescriptor {
         assertEquals(descriptor.describe(cron), descriptor.describe(otherCron));
     }
 
+    @Ignore
     @Test
     public void testAtSecondEveryMinute() {
         final Cron cron = getCron("1 * * * * ? *");
@@ -76,6 +118,7 @@ public class TestDescriptor {
         assertEquals("at second 15 of every minute", descriptor.describe(otherCron));
     }
 
+    @Ignore
     @Test
     public void testAtSecondEveryMinuteWithoutYear() {
         final Cron cron = getCron("1 * * * * ?");
@@ -84,6 +127,7 @@ public class TestDescriptor {
         assertEquals("at second 15 of every minute", descriptor.describe(otherCron));
     }
 
+    @Ignore
     @Test
     public void testEverySecondDuringMinuteOfEveryHour() {
         final Cron cron = getCron("* 1 * * * ? *");
@@ -92,6 +136,7 @@ public class TestDescriptor {
         assertEquals("every second during minute 15 of every hour", descriptor.describe(otherCron));
     }
 
+    @Ignore
     @Test
     public void testEverySecondDuringMinuteOfEveryHourWithoutYear() {
         final Cron cron = getCron("* 1 * * * ?");
@@ -100,6 +145,7 @@ public class TestDescriptor {
         assertEquals("every second during minute 15 of every hour", descriptor.describe(otherCron));
     }
 
+    @Ignore
     @Test
     public void testEverySecondOnTheDayEveryMonth() {
         final Cron first = getCron("* * * 1 * ? *");
@@ -112,6 +158,7 @@ public class TestDescriptor {
         assertEquals("every second, on the 15th day, every month", descriptor.describe(otherCron));
     }
 
+    @Ignore
     @Test
     public void testEverySecondOnTheDayEveryMonthWithoutYear() {
         final Cron first = getCron("* * * 1 * ?");
@@ -124,6 +171,7 @@ public class TestDescriptor {
         assertEquals("every second, on the 15th day, every month", descriptor.describe(otherCron));
     }
 
+    @Ignore
     @Test
     public void testEverySecondEveryDayInMonth() {
         final Cron cron = getCron("* * * * 1 ? *");
@@ -132,6 +180,7 @@ public class TestDescriptor {
         assertEquals("every second, every day, in December", descriptor.describe(otherCron));
     }
 
+    @Ignore
     @Test
     public void testEverySecondEveryDayInMonthWithoutYear() {
         final Cron cron = getCron("* * * * 1 ?");
@@ -140,6 +189,7 @@ public class TestDescriptor {
         assertEquals("every second, every day, in December", descriptor.describe(otherCron));
     }
 
+    @Ignore
     @Test
     public void testEverySecondEveryDayInYear() {
         final Cron cron = getCron("* * * * * ? 2017");
@@ -149,90 +199,105 @@ public class TestDescriptor {
         assertEquals("every second, every day, in 2018", descriptor.describe(otherCron));
     }
 
+    @Ignore
     @Test
     public void testAtTimeEveryDay() {
         final Cron cron = getCron("0 0 0 * * ? *");
         assertEquals("at 00:00:00am every day", descriptor.describe(cron));
     }
 
+    @Ignore
     @Test
     public void testAtTimeEveryDay2() {
         final Cron cron = getCron("00 00 16 * * ? *");
         assertEquals("at 16:00:00pm every day", descriptor.describe(cron));
     }
 
+    @Ignore
     @Test
     public void testAtMultipleTimesEveryDay() {
         final Cron cron = getCron("00 00 8,16 * * ? *");
         assertEquals("at second 00, at minute 00, at 08am and 16pm, of every day", descriptor.describe(cron));
     }
 
+    @Ignore
     @Test
     public void testAtTimeOfDayEveryMonth() {
         final Cron cron = getCron("0 0 16 1 * ? *");
         assertEquals("at 16:00:00pm, on the first day, every month", descriptor.describe(cron));
     }
 
+    @Ignore
     @Test
     public void testAtTimeOfMultipleDaysEveryMonth() {
         final Cron cron = getCron("0 0 16 1,5,26 * ? *");
         assertEquals("at 16:00:00pm on the 1st, 5th and 26th day, every month", descriptor.describe(cron));
     }
 
+    @Ignore
     @Test
     public void testAtTimeOfEveryDayInMonth() {
         final Cron cron = getCron("0 0 16 * 3 ? *");
         assertEquals("at 16:00:00pm, every day, in March", descriptor.describe(cron));
     }
 
+    @Ignore
     @Test
     public void testAtTimeOfEveryDayInMultipleMonth() {
         final Cron cron = getCron("0 0 16 * 1,5,12 ? *");
         assertEquals("at 16:00:00pm, every day in January, May and December", descriptor.describe(cron));
     }
 
+    @Ignore
     @Test
     public void testAtTimeOfEveryDayBetweenMonths() {
         final Cron cron = getCron("0 0 16 * 3-7 ? *");
         assertEquals("at 16:00:00pm, every day between March and July", descriptor.describe(cron));
     }
 
+    @Ignore
     @Test
     public void testAtTimeOfEveryDayBetweenMultipleMonths() {
         final Cron cron = getCron("0 0 16 * 3-7,10-12 ? *");
         assertEquals("at 16:00:00pm, every day between march and July and every month between October and December", descriptor.describe(cron));
     }
 
+    @Ignore
     @Test
     public void testAtTimeOfFirstInMultipleMonths() {
         final Cron cron = getCron("0 0 16 1 3,5,12 ? *");
         assertEquals("at 16:00:00pm, on the 1st day in March, May and December", descriptor.describe(cron));
     }
 
+    @Ignore
     @Test
     public void testAtTimeOfSecondInMultipleMonths() {
         final Cron cron = getCron("0 0 16 2 3,5,12 ? *");
         assertEquals("at 16:00:00pm, on the 2nd day in March, May and December", descriptor.describe(cron));
     }
 
+    @Ignore
     @Test
     public void testAtTimeOfThirdInMultipleMonths() {
         final Cron cron = getCron("0 0 16 3 3,5,12 ? *");
         assertEquals("at 16:00:00pm, on the 3rd day in March, May and December", descriptor.describe(cron));
     }
 
+    @Ignore
     @Test
     public void testAtTimeOfSpecificDayInMultipleMonths() {
         final Cron cron = getCron("0 0 16 15 3,5,12 ? *");
         assertEquals("at 16:00:00pm, on the 15th day in March, May and December", descriptor.describe(cron));
     }
 
+    @Ignore
     @Test
     public void testAtTimeOfSpecificDayBetweenMonths() {
         final Cron cron = getCron("0 0 16 15 3-7 ? *");
         assertEquals("at 16:00:00pm, on the 15th day every month between March and July", descriptor.describe(cron));
     }
 
+    @Ignore
     @Test
     public void testAtTimeOfSpecificDayBetweenMultipleMonths() {
         final Cron cron = getCron("0 0 16 15 3-7,10-12 ? *");
@@ -240,48 +305,56 @@ public class TestDescriptor {
                      descriptor.describe(cron));
     }
 
+    @Ignore
     @Test
     public void testAtTimeOnLastDayOfMonth() {
         final Cron cron = getCron("0 0 16 L * ? *");
         assertEquals("at 16:00:00pm, on the last day of the month, every month", descriptor.describe(cron));
     }
 
+    @Ignore
     @Test
     public void testAtTimeOnLastDayOfSpecificMonth() {
         final Cron cron = getCron("0 0 16 L 3 ? *");
         assertEquals("at 16:00:00pm, on the last day of the month, in March", descriptor.describe(cron));
     }
 
+    @Ignore
     @Test
     public void testAtTimeOnThirdLastDayOfMonth() {
         final Cron cron = getCron("0 0 16 L-3 * ? *");
         assertEquals("at 16:00:00pm, 3 days before the end of the month, every month", descriptor.describe(cron));
     }
 
+    @Ignore
     @Test
     public void testAtTimeOnNthSpecificWeekdayOfMonth() {
         final Cron cron = getCron("0 0 16 ? * 5#2 *");
         assertEquals("at 16:00:00pm, on the 2nd Thursday of the month, every month", descriptor.describe(cron));
     }
 
+    @Ignore
     @Test
     public void testAtTimeOnLastSpecificWeekdayOfMonth() {
         final Cron cron = getCron("0 0 16 ? * 2L *");
         assertEquals("at 16:00:00pm, on the last Monday of the month, every month", descriptor.describe(cron));
     }
 
+    @Ignore
     @Test
     public void testAtTimeEverySpecificWeekdayOfMonth() {
         final Cron cron = getCron("0 0 16 ? * 2 *");
         assertEquals("at 16:00:00pm, every Monday of the month, every month", descriptor.describe(cron));
     }
 
+    @Ignore
     @Test
     public void testAtTimeEverySpecificWeekdayOfSpecificMonth() {
         final Cron cron = getCron("0 0 16 ? 5 3 *");
         assertEquals("at 16:00:00pm, every Tuesday of the month, in May", descriptor.describe(cron));
     }
 
+    @Ignore
     @Test
     public void testAtTimeMultipleWeekdaysOfMultipleSpecificMonths() {
         final Cron cron = getCron("0 0 16 ? 1,5,12 2,5,7 *");