Inheritance: implements yasumi\TranslationsInterface
示例#1
0
 /**
  * Create a new holiday provider instance.
  *
  * A new holiday provider instance can be created using this function. You can use one of the providers included
  * already with Yasumi, or your own provider by giving the name of your class in the first parameter. Your provider
  * class needs to implement the 'ProviderInterface' class.
  *
  * @param string $class  holiday provider name
  * @param int    $year   year for which the country provider needs to be created. Year needs to be a valid integer
  *                       between 1000 and 9999.
  * @param string $locale The locale to use. If empty we'll use the default locale (en_US)
  *
  * @throws RuntimeException         If no such holiday provider is found
  * @throws InvalidArgumentException if the year parameter is not between 1000 and 9999
  * @throws UnknownLocaleException   if the locale parameter is invalid
  * @throws InvalidArgumentException if the holiday provider for the given country does not exist
  *
  * @return AbstractProvider An instance of class $class is created and returned
  */
 public static function create($class, $year = null, $locale = self::DEFAULT_LOCALE)
 {
     // Find and return holiday provider instance
     $providerClass = sprintf('Yasumi\\Provider\\%s', str_replace('/', '\\', $class));
     if (class_exists($class) && (new ReflectionClass($class))->implementsInterface(ProviderInterface::class)) {
         $providerClass = $class;
     }
     if (!class_exists($providerClass) || $class === 'AbstractProvider') {
         throw new InvalidArgumentException(sprintf('Unable to find holiday provider "%s".', $class));
     }
     // Assert year input
     if ($year < 1000 || $year > 9999) {
         throw new InvalidArgumentException(sprintf('Year needs to be between 1000 and 9999 (%s given).', $year));
     }
     // Load internal locales variable
     if (!isset(static::$locales)) {
         static::$locales = self::getAvailableLocales();
     }
     // Load internal translations variable
     if (!isset(static::$globalTranslations)) {
         static::$globalTranslations = new Translations(static::$locales);
         static::$globalTranslations->loadTranslations(__DIR__ . '/data/translations');
     }
     // Assert locale input
     if (!in_array($locale, static::$locales)) {
         throw new UnknownLocaleException(sprintf('Locale "%s" is not a valid locale.', $locale));
     }
     return new $providerClass($year, $locale, self::$globalTranslations);
 }
示例#2
0
    /**
     * Tests loading more than one translation file from directory.
     */
    public function testLoadingMultipleTranslationsFromDirectory()
    {
        $firstShortName = 'newYearsDay';
        $firstFileContents = <<<'FILE'
<?php
return [
    'en_US' => 'New Year\'s Day',
    'nl_NL' => 'Nieuwjaar',
    'pl_PL' => 'Nowy Rok',
];
FILE;
        $secondShortName = 'easter';
        $secondFileContents = <<<'FILE'
<?php
return [
    'en_US' => 'Easter Sunday',
    'nl_NL' => 'Eerste Paasdag',
];
FILE;
        vfsStream::setup('root', null, ['lang' => [$firstShortName . '.php' => $firstFileContents, $secondShortName . '.php' => $secondFileContents]]);
        $translations = new Translations($this->locales);
        $translations->loadTranslations(vfsStream::url('root/lang'));
        $locale = 'en_US';
        $translation = 'New Year\'s Day';
        $this->assertNotNull($translations->getTranslations($firstShortName));
        $this->assertNotEmpty($translations->getTranslations($firstShortName));
        $this->assertInternalType('string', $translations->getTranslation($firstShortName, $locale));
        $this->assertEquals($translation, $translations->getTranslation($firstShortName, $locale));
        $locale = 'nl_NL';
        $translation = 'Eerste Paasdag';
        $this->assertNotNull($translations->getTranslations($secondShortName));
        $this->assertNotEmpty($translations->getTranslations($secondShortName));
        $this->assertInternalType('string', $translations->getTranslation($secondShortName, $locale));
        $this->assertEquals($translation, $translations->getTranslation($secondShortName, $locale));
    }