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

Add default locale #208

Merged
merged 5 commits into from
Feb 21, 2024
Merged
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
16 changes: 0 additions & 16 deletions lang/bahrain/en/holidays.json

This file was deleted.

10 changes: 0 additions & 10 deletions lang/bangladesh/en/holidays.json

This file was deleted.

33 changes: 0 additions & 33 deletions lang/egypt/en/holidays.json

This file was deleted.

12 changes: 0 additions & 12 deletions lang/iran/fa/holidays.json

This file was deleted.

18 changes: 0 additions & 18 deletions lang/montenegro/sr/holidays.json

This file was deleted.

19 changes: 0 additions & 19 deletions lang/switzerland/de/holidays.json

This file was deleted.

7 changes: 6 additions & 1 deletion src/Concerns/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@

trait Translatable
{
protected function translate(string $country, string $name, ?string $locale = null): string
public function translate(string $country, string $name, ?string $locale = null): string
{
if ($locale === null) {
return $name;

}

if ($locale === $this->defaultLocale()) {
return $name;
}

$countryName = strtolower($country);
Expand Down
10 changes: 10 additions & 0 deletions src/Contracts/HasTranslations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Spatie\Holidays\Contracts;

interface HasTranslations
{
public function defaultLocale(): string;

public function translate(string $country, string $name, ?string $locale = null): string;
}
11 changes: 10 additions & 1 deletion src/Countries/Albania.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@
namespace Spatie\Holidays\Countries;

use Carbon\CarbonImmutable;
use Spatie\Holidays\Concerns\Translatable;
use Spatie\Holidays\Contracts\HasTranslations;
use Spatie\Holidays\Exceptions\InvalidYear;

class Albania extends Country
class Albania extends Country implements HasTranslations
{
use Translatable;

public function countryCode(): string
{
return 'al';
}

public function defaultLocale(): string
{
return 'al';
}

protected function allHolidays(int $year): array
{
return array_merge([
Expand Down
12 changes: 10 additions & 2 deletions src/Countries/Azerbaijan.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@
namespace Spatie\Holidays\Countries;

use Carbon\CarbonImmutable;
use Spatie\Holidays\Concerns\Translatable;
use Spatie\Holidays\Contracts\HasTranslations;

class Azerbaijan extends Country
class Azerbaijan extends Country implements HasTranslations
{
use Translatable;

public function countryCode(): string
{
return 'az';
}

public function defaultLocale(): string
{
return 'az';
}

protected function allHolidays(int $year): array
{
return array_merge([
Expand All @@ -31,7 +40,6 @@ protected function allHolidays(int $year): array
/** @return array<string, CarbonImmutable> */
protected function variableHolidays(int $year): array
{
// does not change according to the standard
return [];
}
}
11 changes: 10 additions & 1 deletion src/Countries/Bahrain.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use Spatie\Holidays\Concerns\Translatable;
use Spatie\Holidays\Contracts\HasTranslations;
use Spatie\Holidays\Exceptions\InvalidYear;

class Bahrain extends Country
class Bahrain extends Country implements HasTranslations
{
use Translatable;

protected const EID_AL_FITR_HOLIDAYS = [
2020 => '05-24',
2021 => '05-13',
Expand Down Expand Up @@ -139,6 +143,11 @@ public function countryCode(): string
return 'bh';
}

public function defaultLocale(): string
{
return 'en';
}

protected function allHolidays(int $year): array
{
$variableHolidays = $this->variableHolidays($year);
Expand Down
11 changes: 10 additions & 1 deletion src/Countries/Bangladesh.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@
namespace Spatie\Holidays\Countries;

use Carbon\CarbonImmutable;
use Spatie\Holidays\Concerns\Translatable;
use Spatie\Holidays\Contracts\HasTranslations;

class Bangladesh extends Country
class Bangladesh extends Country implements HasTranslations
{
use Translatable;

public function countryCode(): string
{
return 'bd';
}

public function defaultLocale(): string
{
return 'en';
}

protected function allHolidays(int $year): array
{
return array_merge([
Expand Down
11 changes: 10 additions & 1 deletion src/Countries/Belarus.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@
namespace Spatie\Holidays\Countries;

use Carbon\CarbonImmutable;
use Spatie\Holidays\Concerns\Translatable;
use Spatie\Holidays\Contracts\HasTranslations;

class Belarus extends Country
class Belarus extends Country implements HasTranslations
{
use Translatable;

public function countryCode(): string
{
return 'by';
}

public function defaultLocale(): string
{
return 'be';
}

protected function allHolidays(int $year): array
{
return array_merge([
Expand Down
11 changes: 10 additions & 1 deletion src/Countries/Belgium.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@
namespace Spatie\Holidays\Countries;

use Carbon\CarbonImmutable;
use Spatie\Holidays\Concerns\Translatable;
use Spatie\Holidays\Contracts\HasTranslations;

class Belgium extends Country
class Belgium extends Country implements HasTranslations
{
use Translatable;

public function countryCode(): string
{
return 'be';
}

public function defaultLocale(): string
{
return 'nl';
}

protected function allHolidays(int $year): array
{
return array_merge([
Expand Down
8 changes: 4 additions & 4 deletions src/Countries/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
namespace Spatie\Holidays\Countries;

use Carbon\CarbonImmutable;
use Spatie\Holidays\Concerns\Translatable;
use Spatie\Holidays\Contracts\HasTranslations;
use Spatie\Holidays\Exceptions\InvalidCountry;
use Spatie\Holidays\Exceptions\InvalidYear;

abstract class Country
{
use Translatable;

abstract public function countryCode(): string;

/** @return array<string, string|CarbonImmutable> */
Expand All @@ -33,7 +31,9 @@ public function get(int $year, ?string $locale = null): array
}
}

$name = $this->translate(basename(str_replace('\\', '/', static::class)), $name, $locale);
if ($this instanceof HasTranslations) {
$name = $this->translate(basename(str_replace('\\', '/', static::class)), $name, $locale);
}

$translatedHolidays[$name] = $date;
}
Expand Down
11 changes: 10 additions & 1 deletion src/Countries/Egypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use Spatie\Holidays\Concerns\Translatable;
use Spatie\Holidays\Contracts\HasTranslations;
use Spatie\Holidays\Exceptions\InvalidYear;

class Egypt extends Country
class Egypt extends Country implements HasTranslations
{
use Translatable;

protected const EID_AL_FITR_HOLIDAYS = [
2005 => '11-04',
2006 => '10-24',
Expand Down Expand Up @@ -229,6 +233,11 @@ public function countryCode(): string
return 'eg';
}

public function defaultLocale(): string
{
return 'en';
}

protected function allHolidays(int $year): array
{
$fixedHolidays = $this->fixedHolidays($year);
Expand Down
11 changes: 10 additions & 1 deletion src/Countries/Finland.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@

use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use Spatie\Holidays\Concerns\Translatable;
use Spatie\Holidays\Contracts\HasTranslations;

class Finland extends Country
class Finland extends Country implements HasTranslations
{
use Translatable;

public function countryCode(): string
{
return 'fi';
}

public function defaultLocale(): string
{
return 'fi';
}

protected function allHolidays(int $year): array
{
return array_merge(
Expand Down
Loading