Yasumi\Translations::loadTranslations PHP Method

loadTranslations() public method

Loads translations from directory.
public loadTranslations ( string $directoryPath )
$directoryPath string directory path for translation files
    public function loadTranslations($directoryPath)
    {
        if (!file_exists($directoryPath)) {
            throw new InvalidArgumentException('Directory with translations not found');
        }
        $directoryPath = rtrim($directoryPath, '/\\') . DIRECTORY_SEPARATOR;
        $extension = 'php';
        foreach (new DirectoryIterator($directoryPath) as $file) {
            if ($file->isDot() || $file->isDir()) {
                continue;
            }
            if ($file->getExtension() !== $extension) {
                continue;
            }
            $filename = $file->getFilename();
            $shortName = $file->getBasename('.' . $extension);
            $translations = (require $directoryPath . $filename);
            if (is_array($translations)) {
                foreach (array_keys($translations) as $locale) {
                    $this->isValidLocale($locale);
                    // Validate the given locale
                }
                $this->translations[$shortName] = $translations;
            }
        }
    }

Usage Example

示例#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);
 }
All Usage Examples Of Yasumi\Translations::loadTranslations