diff options
-rw-r--r-- | core/Period/Week.php | 62 | ||||
-rw-r--r-- | plugins/Intl/Commands/GenerateIntl.php | 13 | ||||
-rw-r--r-- | tests/PHPUnit/Unit/Period/WeekTest.php | 53 |
3 files changed, 113 insertions, 15 deletions
diff --git a/core/Period/Week.php b/core/Period/Week.php index 60b1af2ce6..88072b4ba5 100644 --- a/core/Period/Week.php +++ b/core/Period/Week.php @@ -28,8 +28,8 @@ class Week extends Period $dateStart = $this->getDateStart(); $dateEnd = $this->getDateEnd(); - $string = $this->translator->translate('CoreHome_ShortWeekFormat'); - $string = self::getTranslatedRange($string, $dateStart, $dateEnd); + $format = $this->translator->translate('Intl_Format_Interval_Week_Short_'.$this->getMinDifference($dateStart, $dateEnd)); + $string = $this->getTranslatedRange($format, $dateStart, $dateEnd); return $string; } @@ -40,12 +40,27 @@ class Week extends Period */ public function getLocalizedLongString() { - $format = $this->translator->translate('CoreHome_LongWeekFormat'); - $string = self::getTranslatedRange($format, $this->getDateStart(), $this->getDateEnd()); + //"30 Dec - 6 Jan 09" + $dateStart = $this->getDateStart(); + $dateEnd = $this->getDateEnd(); + + $format = $this->translator->translate('Intl_Format_Interval_Week_Long_'.$this->getMinDifference($dateStart, $dateEnd)); + $string = $this->getTranslatedRange($format, $dateStart, $dateEnd); return $this->translator->translate('Intl_PeriodWeek') . " " . $string; } + protected function getMinDifference($dateFrom, $dateEnd) + { + if ($dateFrom->toString('y') != $dateEnd->toString('y')) { + return 'Y'; + } elseif ($dateFrom->toString('m') != $dateEnd->toString('m')) { + return 'M'; + } + + return 'D'; + } + /** * @param string $format * @param \Piwik\Date $dateStart @@ -53,17 +68,46 @@ class Week extends Period * * @return mixed */ - protected static function getTranslatedRange($format, $dateStart, $dateEnd) + protected function getTranslatedRange($format, $dateStart, $dateEnd) { - $string = str_replace('From%', '%', $format); - $string = $dateStart->getLocalized($string); - $string = str_replace('To%', '%', $string); - $string = $dateEnd->getLocalized($string); + list($formatStart, $formatEnd) = $this->explodeFormat($format); + + $string = $dateStart->getLocalized($formatStart); + $string .= $dateEnd->getLocalized($formatEnd); return $string; } /** + * Explodes the given format into two pieces. One that can be user for start date and the other for end date + * + * @param $format + * @return array + */ + protected function explodeFormat($format) + { + $intervalTokens = array( + array('d', 'E', 'C'), + array('M', 'L'), + array('y') + ); + + $offset = strlen($format); + + // search for first duplicate date field + foreach ($intervalTokens AS $tokens) { + if (preg_match_all('/['.implode('|', $tokens).']+/', $format, $matches, PREG_OFFSET_CAPTURE)) { + if (count($matches[0]) > 1 && $offset > $matches[0][1][1]) { + $offset = $matches[0][1][1]; + } + } + + } + + return array(substr($format, 0, $offset), substr($format, $offset)); + } + + /** * Returns the current period as a string * * @return string diff --git a/plugins/Intl/Commands/GenerateIntl.php b/plugins/Intl/Commands/GenerateIntl.php index 0c027000fa..b997f62e18 100644 --- a/plugins/Intl/Commands/GenerateIntl.php +++ b/plugins/Intl/Commands/GenerateIntl.php @@ -275,6 +275,19 @@ class GenerateIntl extends ConsoleCommand $translations['Intl']['Format_DateTime_Long'] = $calendarData['dateFormats']['full'] . ' ' . $calendarData['timeFormats']['medium']; $translations['Intl']['Format_DateTime_Short'] = $calendarData['dateFormats']['medium'] . ' ' . $calendarData['timeFormats']['medium']; + $translations['Intl']['Format_Interval_Week_Long_D'] = $this->transformDateFormat($calendarData['dateTimeFormats']['intervalFormats']['yMMMd']['d'], array('MMMM' => 'MMM', 'LLLL' => 'LLL', 'MMM' => 'MMMM', 'LLL' => 'LLLL')); + $translations['Intl']['Format_Interval_Week_Long_M'] = $this->transformDateFormat($calendarData['dateTimeFormats']['intervalFormats']['yMMMd']['M'], array('MMMM' => 'MMM', 'LLLL' => 'LLL', 'MMM' => 'MMMM', 'LLL' => 'LLLL')); + $translations['Intl']['Format_Interval_Week_Long_Y'] = $this->transformDateFormat($calendarData['dateTimeFormats']['intervalFormats']['yMMMd']['y'], array('MMMM' => 'MMM', 'LLLL' => 'LLL', 'MMM' => 'MMMM', 'LLL' => 'LLLL')); + + if(isset($calendarData['dateTimeFormats']['intervalFormats']['yMMMMd'])) { + $translations['Intl']['Format_Interval_Week_Long_D'] = $calendarData['dateTimeFormats']['intervalFormats']['yMMMMd']['d']; + $translations['Intl']['Format_Interval_Week_Long_M'] = $calendarData['dateTimeFormats']['intervalFormats']['yMMMMd']['M']; + $translations['Intl']['Format_Interval_Week_Long_Y'] = $calendarData['dateTimeFormats']['intervalFormats']['yMMMMd']['y']; + } + + $translations['Intl']['Format_Interval_Week_Short_D'] = $calendarData['dateTimeFormats']['intervalFormats']['yMMMd']['d']; + $translations['Intl']['Format_Interval_Week_Short_M'] = $calendarData['dateTimeFormats']['intervalFormats']['yMMMd']['M']; + $translations['Intl']['Format_Interval_Week_Short_Y'] = $calendarData['dateTimeFormats']['intervalFormats']['yMMMd']['y']; $output->writeln('Saved calendar data for ' . $langCode); } catch (\Exception $e) { diff --git a/tests/PHPUnit/Unit/Period/WeekTest.php b/tests/PHPUnit/Unit/Period/WeekTest.php index 05f54b9942..bbf0a8935e 100644 --- a/tests/PHPUnit/Unit/Period/WeekTest.php +++ b/tests/PHPUnit/Unit/Period/WeekTest.php @@ -8,6 +8,7 @@ namespace Piwik\Tests\Unit\Period; +use Piwik\Container\StaticContainer; use Piwik\Date; use Piwik\Period\Week; @@ -117,24 +118,64 @@ class WeekTest extends BasePeriodTest $this->assertEquals(7, $week->getNumberOfSubperiods()); } + public function getLocalizedShortStrings() + { + return array( + array('en', array('Oct 7 – 13, 2024', 'Nov 25 – Dec 1, 2024', 'Dec 30, 2024 – Jan 5, 2025')), + array('lt', array('2024-10-07 – 2024-10-13', '2024-11-25 – 2024-12-01', '2024-12-30 – 2025-1-05')), + array('zh-cn', array('2024年10月7日至13日', '2024年11月25日至12月1日', '2024年12月30日至2025年01月5日')), + ); + } + /** * @group Core + * @group Week + * @dataProvider getLocalizedShortStrings */ - public function testGetLocalizedShortString() + public function testGetLocalizedShortString($language, $shouldBe) { + StaticContainer::get('Piwik\Translation\Translator')->setCurrentLanguage($language); + // a week within a month $week = new Week(Date::factory('2024-10-09')); - $shouldBe = '7 Oct - 13 Oct 24'; - $this->assertEquals($shouldBe, $week->getLocalizedShortString()); + $this->assertEquals($shouldBe[0], $week->getLocalizedShortString()); + + // a week ending in another month + $week = new Week(Date::factory('2024-12-01')); + $this->assertEquals($shouldBe[1], $week->getLocalizedShortString()); + + // a week ending in another year + $week = new Week(Date::factory('2024-12-31')); + $this->assertEquals($shouldBe[2], $week->getLocalizedShortString()); + } + + public function getLocalizedLongStrings() + { + return array( + array('en', array('Week October 7 – 13, 2024', 'Week November 25 – December 1, 2024', 'Week December 30, 2024 – January 5, 2025')), + array('lt', array('Savaitė 2024 Spalio 7–13', 'Savaitė 2024 Lapkričio 25 – Gruodžio 1', 'Savaitė 2024 Gruodžio 30 – 2025 Sausio 5')), + array('zh-cn', array('周 2024年10月7日至13日', '周 2024年11月25日至12月1日', '周 2024年12月30日至2025年01月5日')), + ); } /** * @group Core + * @group Week + * @dataProvider getLocalizedLongStrings */ - public function testGetLocalizedLongString() + public function testGetLocalizedLongString($language, $shouldBe) { + StaticContainer::get('Piwik\Translation\Translator')->setCurrentLanguage($language); + // a week within a month $week = new Week(Date::factory('2024-10-09')); - $shouldBe = 'Week 7 October - 13 October 2024'; - $this->assertEquals($shouldBe, $week->getLocalizedLongString()); + $this->assertEquals($shouldBe[0], $week->getLocalizedLongString()); + + // a week ending in another month + $week = new Week(Date::factory('2024-12-01')); + $this->assertEquals($shouldBe[1], $week->getLocalizedLongString()); + + // a week ending in another year + $week = new Week(Date::factory('2024-12-31')); + $this->assertEquals($shouldBe[2], $week->getLocalizedLongString()); } /** |