Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locale fallback #124

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,25 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
- Holiday Provider for South Korea. [\#156](https://github.com/azuyalabs/yasumi/pull/156) ([blood72](https://github.com/blood72))
- Translation for the Easter holiday for the 'fr_FR' locale [\#146](https://github.com/azuyalabs/yasumi/pull/146) ([pioc92](https://github.com/pioc92))
- Translation for the Pentecoste holiday for the 'fr_FR' locale [\#145](https://github.com/azuyalabs/yasumi/pull/145) ([pioc92](https://github.com/pioc92))
- Added `Yasumi::setDefaultLocale()` and `Yasumi::getDefaultLocale()`. [\#123](https://github.com/azuyalabs/yasumi/pull/123) ([c960657](https://github.com/c960657))
- Added optional $argument for `Holiday::getName($locale)` for overriding the default locale. [\#123](https://github.com/azuyalabs/yasumi/pull/123) ([c960657](https://github.com/c960657))

### Changed
- Replaced the standard 'InvalidArgumentException' when an invalid year or holiday provider are given by a new exception for each of these two situations separately ('InvalidYearException' and 'ProviderNotFoundException'). This allows you to better distinguish which exception may occur when instantiating the Yasumi class. [\#95](https://github.com/azuyalabs/yasumi/pull/95) ([qneyrat](https://github.com/qneyrat))
- Updated the translation for the All Saints holiday for the 'fr_FR' locale [\#152](https://github.com/azuyalabs/yasumi/pull/152) ([pioc92](https://github.com/pioc92))
- Updated the translation for the Armistice holiday for the 'fr_FR' locale [\#154](https://github.com/azuyalabs/yasumi/pull/154) ([pioc92](https://github.com/pioc92))
- Updated the translation for the Victory in Europe holiday for the 'fr_FR' locale [\#153](https://github.com/azuyalabs/yasumi/pull/153) ([pioc92](https://github.com/pioc92))
- Updated the translation for the Assumption of Mary holiday for the 'fr_FR' locale [\#155](https://github.com/azuyalabs/yasumi/pull/155) ([pioc92](https://github.com/pioc92))
- Fallback support added to getName() to allow e.g. fallback from 'de_AT' to 'de'. [\#124](https://github.com/azuyalabs/yasumi/pull/124) ([c960657](https://github.com/c960657))

### Fixed

### Removed

### Deprecated
- Deprecated the `$locale` argument for `Yasumi::create()` and `Yasumi::createByISO3166_2()`. [\#123](https://github.com/azuyalabs/yasumi/pull/123) ([c960657](https://github.com/c960657))
- Deprecated the constant `Yasumi::DEFAULT_LOCALE`. [\#123](https://github.com/azuyalabs/yasumi/pull/123) ([c960657](https://github.com/c960657))


## [2.1.0] - 2019-03-29

Expand Down
28 changes: 24 additions & 4 deletions src/Yasumi/Holiday.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,34 @@ public function jsonSerialize(): self
* Returns the name of this holiday.
*
* The name of this holiday is returned translated in the given locale. If for the given locale no translation is
* defined, the name in the default locale ('en_US') is returned. In case there is no translation at all, the short
* defined, the name in the default locale is returned. In case there is no translation at all, the short
* internal name is returned.
*
* @param string $locale The locale to use
* @param array $fallbackLocales Fallback locales to use when looking for translations
*/
public function getName(): string
public function getName(string $locale = null, array $fallbackLocales = null): string
{
return $this->translations[$this->displayLocale] ?? $this->translations[self::DEFAULT_LOCALE] ?? $this->shortName;
}
if ($locale === null) {
$locale = $this->displayLocale;
}

if (isset($this->translations[$locale])) {
return $this->translations[$locale];
}

if ($fallbackLocales === null) {
$fallbackLocales = Yasumi::getFallbackLocales($locale);
}

foreach ($fallbackLocales as $locale) {
if (isset($this->translations[$locale])) {
return $this->translations[$locale];
}
}

return $this->shortName;
}
/**
* Merges local translations (preferred) with global translations.
*
Expand Down
1 change: 1 addition & 0 deletions src/Yasumi/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ abstract class AbstractProvider implements ProviderInterface, Countable, Iterato

/**
* @var string the object's current locale
* @deprecated will be removed in Yasumi 3.0
*/
protected $locale;

Expand Down
24 changes: 12 additions & 12 deletions src/Yasumi/Provider/Australia.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private function calculateAustraliaDay(): void
{
$date = new DateTime("$this->year-01-26", new DateTimeZone($this->timezone));

$this->calculateHoliday('australiaDay', ['en_AU' => 'Australia Day'], $date);
$this->calculateHoliday('australiaDay', ['en' => 'Australia Day'], $date);
}

/**
Expand Down Expand Up @@ -125,15 +125,15 @@ public function calculateHoliday(
private function calculateNewYearHolidays(): void
{
$newyearsday = new DateTime("$this->year-01-01", new DateTimeZone($this->timezone));
$this->calculateHoliday('newYearsDay', ['en_AU' => 'New Year\'s Day'], $newyearsday, false, false);
$this->calculateHoliday('newYearsDay', [], $newyearsday, false, false);
switch ($newyearsday->format('w')) {
case 0: // sunday
$newyearsday->add(new DateInterval('P1D'));
$this->calculateHoliday('newYearsHoliday', ['en_AU' => 'New Year\'s Holiday'], $newyearsday, false, false);
$this->calculateHoliday('newYearsHoliday', ['en' => 'New Year\'s Holiday'], $newyearsday, false, false);
break;
case 6: // saturday
$newyearsday->add(new DateInterval('P2D'));
$this->calculateHoliday('newYearsHoliday', ['en_AU' => 'New Year\'s Holiday'], $newyearsday, false, false);
$this->calculateHoliday('newYearsHoliday', ['en' => 'New Year\'s Holiday'], $newyearsday, false, false);
break;
}
}
Expand Down Expand Up @@ -161,7 +161,7 @@ private function calculateAnzacDay(): void
}

$date = new DateTime("$this->year-04-25", new DateTimeZone($this->timezone));
$this->calculateHoliday('anzacDay', ['en_AU' => 'ANZAC Day'], $date, false, false);
$this->calculateHoliday('anzacDay', ['en' => 'ANZAC Day'], $date, false, false);
$easter = $this->calculateEaster($this->year, $this->timezone);

$easterMonday = $this->calculateEaster($this->year, $this->timezone);
Expand All @@ -170,7 +170,7 @@ private function calculateAnzacDay(): void
$fDate = $date->format('Y-m-d');
if ($fDate === $easter->format('Y-m-d') || $fDate === $easterMonday->format('Y-m-d')) {
$easterMonday->add(new DateInterval('P1D'));
$this->calculateHoliday('easterTuesday', ['en_AU' => 'Easter Tuesday'], $easterMonday, false, false);
$this->calculateHoliday('easterTuesday', ['en' => 'Easter Tuesday'], $easterMonday, false, false);
}
unset($fDate);
}
Expand All @@ -191,23 +191,23 @@ private function calculateChristmasDay(): void
{
$christmasDay = new DateTime("$this->year-12-25", new DateTimeZone($this->timezone));
$boxingDay = new DateTime("$this->year-12-26", new DateTimeZone($this->timezone));
$this->calculateHoliday('christmasDay', ['en_AU' => 'Christmas Day'], $christmasDay, false, false);
$this->calculateHoliday('secondChristmasDay', ['en_AU' => 'Boxing Day'], $boxingDay, false, false);
$this->calculateHoliday('christmasDay', [], $christmasDay, false, false);
$this->calculateHoliday('secondChristmasDay', [], $boxingDay, false, false);

switch ($christmasDay->format('w')) {
case 0: // sunday
$christmasDay->add(new DateInterval('P2D'));
$this->calculateHoliday('christmasHoliday', ['en_AU' => 'Christmas Holiday'], $christmasDay, false, false);
$this->calculateHoliday('christmasHoliday', ['en' => 'Christmas Holiday'], $christmasDay, false, false);
break;
case 5: // friday
$boxingDay->add(new DateInterval('P2D'));
$this->calculateHoliday('secondChristmasHoliday', ['en_AU' => 'Boxing Day Holiday'], $boxingDay, false, false);
$this->calculateHoliday('secondChristmasHoliday', ['en' => 'Boxing Day Holiday'], $boxingDay, false, false);
break;
case 6: // saturday
$christmasDay->add(new DateInterval('P2D'));
$boxingDay->add(new DateInterval('P2D'));
$this->calculateHoliday('christmasHoliday', ['en_AU' => 'Christmas Holiday'], $christmasDay, false, false);
$this->calculateHoliday('secondChristmasHoliday', ['en_AU' => 'Boxing Day Holiday'], $boxingDay, false, false);
$this->calculateHoliday('christmasHoliday', ['en' => 'Christmas Holiday'], $christmasDay, false, false);
$this->calculateHoliday('secondChristmasHoliday', ['en' => 'Boxing Day Holiday'], $boxingDay, false, false);
break;
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/Yasumi/Provider/Australia/ACT.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private function calculateCanberraDay(): void
} else {
$date = new DateTime("second monday of march $this->year", new DateTimeZone($this->timezone));
}
$this->addHoliday(new Holiday('canberraDay', ['en_AU' => 'Canberra Day'], $date, $this->locale));
$this->addHoliday(new Holiday('canberraDay', ['en' => 'Canberra Day'], $date, $this->locale));
}

/**
Expand All @@ -82,7 +82,7 @@ private function calculateReconciliationDay(): void
if ($day !== 1) {
$date = $date->add($day === 0 ? new DateInterval('P1D') : new DateInterval('P'.(8-$day).'D'));
}
$this->addHoliday(new Holiday('reconciliationDay', ['en_AU' => 'Reconciliation Day'], $date, $this->locale));
$this->addHoliday(new Holiday('reconciliationDay', ['en' => 'Reconciliation Day'], $date, $this->locale));
}

/**
Expand All @@ -94,7 +94,7 @@ private function calculateLabourDay(): void
{
$date = new DateTime("first monday of october $this->year", new DateTimeZone($this->timezone));

$this->addHoliday(new Holiday('labourDay', ['en_AU' => 'Labour Day'], $date, $this->locale));
$this->addHoliday(new Holiday('labourDay', ['en' => 'Labour Day'], $date, $this->locale));
}

/**
Expand Down Expand Up @@ -122,7 +122,7 @@ private function easterSaturday($year, $timezone, $locale, $type = Holiday::TYPE
{
return new Holiday(
'easterSaturday',
['en_AU' => 'Easter Saturday'],
['en' => 'Easter Saturday'],
$this->calculateEaster($year, $timezone)->sub(new DateInterval('P1D')),
$locale,
$type
Expand Down Expand Up @@ -154,7 +154,7 @@ private function easterSunday($year, $timezone, $locale, $type = Holiday::TYPE_O
{
return new Holiday(
'easter',
['en_AU' => 'Easter Sunday'],
['en' => 'Easter Sunday'],
$this->calculateEaster($year, $timezone),
$locale,
$type
Expand All @@ -180,7 +180,7 @@ private function calculateQueensBirthday(): void
{
$this->calculateHoliday(
'queensBirthday',
['en_AU' => "Queen's Birthday"],
['en' => "Queen's Birthday"],
new DateTime('second monday of june ' . $this->year, new DateTimeZone($this->timezone)),
false,
false
Expand Down
10 changes: 5 additions & 5 deletions src/Yasumi/Provider/Australia/NSW.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function initialize(): void
{
parent::initialize();

$this->addHoliday(new Holiday('easter', ['en_AU' => 'Easter Sunday'], $this->calculateEaster($this->year, $this->timezone), $this->locale));
$this->addHoliday(new Holiday('easter', [], $this->calculateEaster($this->year, $this->timezone), $this->locale));
$this->addHoliday($this->easterSaturday($this->year, $this->timezone, $this->locale));
$this->calculateQueensBirthday();
$this->calculateLabourDay();
Expand Down Expand Up @@ -87,7 +87,7 @@ private function easterSaturday($year, $timezone, $locale, $type = Holiday::TYPE
{
return new Holiday(
'easterSaturday',
['en_AU' => 'Easter Saturday'],
['en' => 'Easter Saturday'],
$this->calculateEaster($year, $timezone)->sub(new DateInterval('P1D')),
$locale,
$type
Expand All @@ -113,13 +113,13 @@ private function calculateQueensBirthday(): void
{
$this->calculateHoliday(
'queensBirthday',
['en_AU' => "Queen's Birthday"],
['en' => "Queen's Birthday"],
new DateTime('second monday of june ' . $this->year, new DateTimeZone($this->timezone)),
false,
false
);
}

/**
* Bank Holiday.
*
Expand All @@ -130,7 +130,7 @@ private function calculateBankHoliday(): void
{
$this->calculateHoliday(
'bankHoliday',
['en_AU' => 'Bank Holiday'],
['en' => 'Bank Holiday'],
new DateTime('first monday of august '. $this->year, new DateTimeZone($this->timezone)),
false,
false,
Expand Down
8 changes: 4 additions & 4 deletions src/Yasumi/Provider/Australia/NT.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private function calculateMayDay(): void
{
$date = new DateTime("first monday of may $this->year", new DateTimeZone($this->timezone));

$this->addHoliday(new Holiday('mayDay', ['en_AU' => 'May Day'], $date, $this->locale));
$this->addHoliday(new Holiday('mayDay', ['en' => 'May Day'], $date, $this->locale));
}

/**
Expand All @@ -73,7 +73,7 @@ private function calculatePicnicDay(): void
{
$this->calculateHoliday(
'picnicDay',
['en_AU' => 'Picnic Day'],
['en' => 'Picnic Day'],
new DateTime('first monday of august '. $this->year, new DateTimeZone($this->timezone)),
false,
false
Expand Down Expand Up @@ -105,7 +105,7 @@ private function easterSaturday($year, $timezone, $locale, $type = Holiday::TYPE
{
return new Holiday(
'easterSaturday',
['en_AU' => 'Easter Saturday'],
['en' => 'Easter Saturday'],
$this->calculateEaster($year, $timezone)->sub(new DateInterval('P1D')),
$locale,
$type
Expand All @@ -131,7 +131,7 @@ private function calculateQueensBirthday(): void
{
$this->calculateHoliday(
'queensBirthday',
['en_AU' => "Queen's Birthday"],
['en' => "Queen's Birthday"],
new DateTime('second monday of june ' . $this->year, new DateTimeZone($this->timezone)),
false,
false
Expand Down
6 changes: 3 additions & 3 deletions src/Yasumi/Provider/Australia/Queensland.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private function calculateLabourDay(): void
} else {
$date = new DateTime("first monday of may $this->year", new DateTimeZone($this->timezone));
}

$this->addHoliday(new Holiday('labourDay', [], $date, $this->locale));
}

Expand All @@ -82,15 +82,15 @@ private function calculateQueensBirthday(): void
if ($this->year < 2012 || $this->year === 2013 || $this->year === 2014 || $this->year === 2015) {
$this->calculateHoliday(
'queensBirthday',
['en_AU' => "Queen's Birthday"],
['en' => "Queen's Birthday"],
new DateTime('second monday of june ' . $this->year, new DateTimeZone($this->timezone)),
false,
false
);
} else {
$this->calculateHoliday(
'queensBirthday',
['en_AU' => "Queen's Birthday"],
['en' => "Queen's Birthday"],
new DateTime('first monday of october ' . $this->year, new DateTimeZone($this->timezone)),
false,
false
Expand Down
2 changes: 1 addition & 1 deletion src/Yasumi/Provider/Australia/Queensland/Brisbane.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ private function calculatePeoplesDay(): void
$date = $date->add(new DateInterval('P7D'));
}
$date = $date->add(new DateInterval('P5D'));
$this->addHoliday(new Holiday('peoplesDay', ['en_AU' => 'Ekka People\'s Day'], $date, $this->locale));
$this->addHoliday(new Holiday('peoplesDay', ['en' => 'Ekka People\'s Day'], $date, $this->locale));
}
}
Loading