/** *Get all locales that are either selectable or referenced in the reosltuion *of a selectable locale *@returns array of string, the locales */ public static function getAvailableLocales() { $selectable = self::getSelectableLocales(); $locales = array(); foreach ($selectable as $locale) { $locales = array_unique(array_merge($locales, self::getLocaleResolution($locale))); } $locales = I2CE_Locales::validateLocales($locales); return $locales; }
public function setLocales($locales) { $locales = I2CE_Locales::validateLocales($locales); $this->locales = $locales; }
public function processValues($vals) { $desired_locales = array(); if (array_key_exists('def_locale', $vals)) { $desired_locales[] = $vals['def_locale']; } if (array_key_exists('new_locale', $vals)) { $desired_locales[] = $vals['new_locale']; } $success = true; foreach ($desired_locales as $locale) { if (!$locale) { continue; } if (!preg_match('/^(\\w+)_(\\w+)$/', $locale, $matches)) { $msg = "Invalid Locale specified"; $this->userMessage($msg, 'notice', false); I2CE::raiseError($msg); $success = false; continue; } $locale = strtolower($matches[1]) . '_' . strtoupper($matches[2]); $locales = I2CE_Locales::validateLocales($this->getChildNames()); if (!$locale) { $msg = "Invalid Locale specified"; $this->userMessage($msg, 'notice', false); I2CE::raiseError($msg); $success = false; continue; } if (in_array($locale, $locales)) { $msg = "Locale {$locale} is already used"; $this->userMessage($msg, 'notice', false); I2CE::raiseError($msg); continue; } $child = $this->getChild($locale, true); if (!$child instanceof I2CE_Swiss_Locale) { $msg = "Could not add locale {$locale}"; $this->userMessage($msg, 'notice', false); $success = false; continue; } } return $success; }
/** * A path to search for a category of files. * @param string $category the category of files. * @param string $path the parh (or glob pattern for a path) to add. There a a few modifications to the globbing... * A trailing '**' means that the paths should be added recursivley * Paths are automatically checked for localized version by the presence of a 'en_US' subdirectory. * specifically, if a path is added such as /my/path and there is a subdir called /my/path/en_US then * all subdirectories which are in the preffered locales (or $locales below) are added. * * Examples: /usr/share/fonts/ adds in this directory to the search paths * /usr/share/fonts/* adds in all subdirectories * /usr/share/fonts/** recusively adds in all subdirecties * /usr/share/fonts/truetype/ttf* adds in all directories begining with ttf * @param mixed $order. If it is a number, then it indicates the order in which this path is searched. * The lower the number, the higher the priority. * If multiple paths share the same order, the order in which they are searched is not specfied * The are also special string values of $order (all are relative to the category specified): * <ul> * <li>'LAST' -- add at the last added order </li> * <li>'LOWEST' -- add so it has the lowest order thus far </li> * <li>'HIGHEST' -- add so it has the highest order thus far </li> * <li>'EVEN_LOWER' -- add so it has lower order than anything thus far. This is the default behavior </li> * <li>'EVEN_HIGHER' -- add so it has higher order than anything thus far </li> * </ul> * @param boolean $absolut whether or not to try to make a relative path absolut. It will * overide (but not change) the global behaviour of this instance. * @param string $path_prefix. Defaults to null. Any prefix to add to a local class path (if we do make absolute) * @param mixed $locales null(default) , string or array of string. Used to override the default locale settings when not the defaults of null. * It is the list of localized sub-directories to search for, if the given glob is a subdirectory. */ public function addPath($category, $path, $order = 'EVEN_LOWER', $absolut = null, $path_prefix = null, $locales = null) { $path = self::convertPath($path); $path_prefix = self::convertPath($path_prefix); if (!is_string($path) || strlen($path) == 0) { return FALSE; } if ($absolut === null) { $absolut = $this->absolut; } $recurse = false; if (strlen($path) >= 2 && substr($path, strlen($path) - 2) == '**') { $recurse = true; $path = substr($path, 0, -1); } if ($absolut && !$this->isAbsolut($path)) { //relative directorty which we need to change to a absolut directory if ($path_prefix === null || is_string($path_prefix) && strlen($path_prefix) == 0) { $path_prefix = $this->absolut('.' . DIRECTORY_SEPARATOR, 1); } /* see http://us.php.net/manual/en/function.glob.php#86425 for glob and preg_replace */ $path = preg_replace('/(\\*|\\?|\\[)/', '[$1]', $path_prefix) . DIRECTORY_SEPARATOR . $path; } $path = self::convertPath($path, true); if (strlen($path) == 0) { return false; } $t_locales = null; $found_dirs = array(); $globs = array($path => I2CE_Locales::DEFAULT_LOCALE); if (!is_array($locales)) { if (is_string($locales)) { $locales = array($locales); $locales = I2CE_Locales::validateLocales($t_locales); } else { if (array_key_exists($category, $this->preferred_locales) && is_array($this->preferred_locales[$category])) { $locales = $this->preferred_locales[$category]; $locales = I2CE_Locales::validateLocales($locales); } else { $locales = I2CE_Locales::getPreferredLocales(); } } } else { $locales = I2CE_Locales::validateLocales($locales); } while (count($globs) > 0) { end($globs); $glob = key($globs); $locale = array_pop($globs); $dirs = glob($glob, GLOB_ONLYDIR | GLOB_NOSORT); if ($dirs === false) { continue; } foreach ($dirs as $dir) { if ($locale == I2CE_Locales::DEFAULT_LOCALE && is_dir($dir . DIRECTORY_SEPARATOR . I2CE_Locales::DEFAULT_LOCALE . DIRECTORY_SEPARATOR . '.')) { //this directory is localized. foreach ($locales as $t_locale) { $l_dir = $dir . DIRECTORY_SEPARATOR . $t_locale; if (is_dir($l_dir)) { $found_dirs[$l_dir] = $t_locale; if ($recurse) { $globs[preg_replace('/(\\*|\\?|\\[)/', '[$1]', $l_dir) . DIRECTORY_SEPARATOR . '*'] = $t_locale; } } } } else { //this directory is not localized $found_dirs[$dir] = $locale; if ($recurse) { $globs[preg_replace('/(\\*|\\?|\\[)/', '[$1]', $dir) . DIRECTORY_SEPARATOR . '*'] = $locale; } } } } if (count($found_dirs) == 0) { return FALSE; } if (is_string($order)) { if (!array_key_exists($category, $this->ordered_paths) || !is_array($this->ordered_paths[$category]) || count($this->ordered_paths[$category]) == 0) { //nothing added yet. $order = 0; } else { $order = strtoupper($order); switch ($order) { case 'LAST': $order = $this->last_order[$category]; if ($order == null) { //this should not happen but we are being safe $order = 0; } break; case 'LOWEST': $keys = array_keys($this->ordered_paths[$category]); $order = $keys[count($keys) - 1]; break; case 'HIGHEST': $keys = array_keys($this->ordered_paths[$category]); $order = $keys[0]; break; case 'EVEN_HIGHER': $keys = array_keys($this->ordered_paths[$category]); $order = $keys[0] - 1; break; default: // 'EVEN_LOWER' $keys = array_keys($this->ordered_paths[$category]); $order = $keys[count($keys) - 1] + 1; break; } } } else { if (!is_int($order)) { if (class_exists('I2CE', true)) { I2CE::raiseError("Invalid order ({$order}) when setting path ({$path})", E_USER_NOTICE); return FALSE; } else { I2CE::raiseError("Invalid order ({$order}) when setting path ({$path})", E_USER_ERROR); return false; } } } foreach ($found_dirs as $dir => $locale) { $this->ordered_paths[$category][$order][realpath($dir)] = $locale; } ksort($this->ordered_paths[$category]); $this->last_order[$category] = $order; return TRUE; }