本文整理汇总了PHP中Locale::parseLocale方法的典型用法代码示例。如果您正苦于以下问题:PHP Locale::parseLocale方法的具体用法?PHP Locale::parseLocale怎么用?PHP Locale::parseLocale使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Locale
的用法示例。
在下文中一共展示了Locale::parseLocale方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getNameFormat
/**
* Get name format based on locale, if locale is not passed locale from system configuration will be used.
*
* @param string|null $locale
* @throws \RuntimeException
*/
public function getNameFormat($locale = null)
{
if (!$locale) {
$locale = $this->localeSettings->getLocale();
}
$nameFormats = $this->localeSettings->getNameFormats();
// match by locale (for example - "fr_CA")
if (isset($nameFormats[$locale])) {
return $nameFormats[$locale];
}
// match by locale language (for example - "fr")
$localeParts = \Locale::parseLocale($locale);
if (isset($localeParts[\Locale::LANG_TAG])) {
$match = $localeParts[\Locale::LANG_TAG];
if (isset($match, $nameFormats[$match])) {
return $nameFormats[$match];
}
}
// match by default locale in system configuration settings
$match = $this->localeSettings->getLocale();
if ($match !== $locale && isset($nameFormats[$match])) {
return $nameFormats[$match];
}
// fallback to default constant locale
$match = LocaleConfiguration::DEFAULT_LOCALE;
if (isset($nameFormats[$match])) {
return $nameFormats[$match];
}
throw new \RuntimeException(sprintf('Cannot get name format for "%s"', $locale));
}
示例2: parse
/**
* Build locale object from a string.
*
* @param string $locale The locale as string
*
* @return Locale A Locale object
*/
public static function parse($locale)
{
if (empty($locale)) {
throw new \InvalidArgumentException('Locale string can not be empty');
}
$result = \Locale::parseLocale($locale);
if (!is_array($result)) {
throw new \RuntimeException(sprintf('Unable to parse locale "%s"', $locale));
}
$language = null;
$region = null;
$variants = array();
foreach ($result as $key => $value) {
if ('language' === $key) {
$language = new Language($value);
} elseif ('region' === $key) {
$region = new Region($value);
} elseif (0 === strpos($key, 'variant')) {
$variants[] = new Variant($value);
}
}
if (null === $language) {
throw new \RuntimeException(sprintf('Unable to retrieve a valid language for locale "%s"', $locale));
}
return new self($language, $region, $variants);
}
示例3: parseHeader
/**
* Parse header
*
* @param null|\Zend\Http\Header\AcceptLanguage $header
* @return array
*/
public function parseHeader($header)
{
$locales = array();
$controller = $this->getController();
if ($controller instanceof ServiceLocatorAwareInterface) {
$availables = $controller->getServiceLocator()->get('Locale')->getAvailableLocales();
} else {
$availables = array(IntlLocale::getDefault());
}
if ($header instanceof AcceptLanguage) {
foreach ($header->getPrioritized() as $part) {
if ($part instanceof LanguageFieldValuePart) {
$locale = IntlLocale::parseLocale($part->getLanguage());
$key = $locale['language'];
if (isset($locale['region'])) {
$key .= '_' . $locale['region'];
}
if ($availables) {
$key = IntlLocale::lookup($availables, $key, false, '');
}
if ($key) {
$locales[$key] = max($part->getPriority(), empty($locales[$key]) ? 0 : $locales[$key]);
}
}
}
}
return $locales;
}
示例4: setSessionLanguage
/**
* set Language to Session
*
* @param string $lang Requested language
*/
protected function setSessionLanguage($lang)
{
session()->set('applocale', $lang);
$locale = \Locale::parseLocale($lang);
session()->set('language', $locale['language']);
$this->log->info("Language Switched:{$locale['language']}");
}
示例5: __construct
public function __construct($locale)
{
$this->localeArray = Locale::parseLocale($locale);
$this->localeString = Locale::composeLocale($this->localeArray);
$this->initMonths();
$this->initDays();
$this->initPattern();
}
示例6: convert
public function convert($locale)
{
$parts = \Locale::parseLocale($locale);
if (!isset($parts['region'])) {
$parts['region'] = $this->country;
}
$locale = \Locale::canonicalize(\Locale::composeLocale($parts));
return $locale;
}
示例7: switchLang
/**
* Switch Language
*
* @param string $lang Language iso code
* @param Session $session Session Facade
* @return Redirect HTTP Redirect
*/
public function switchLang($lang)
{
$this->log->info("Language switch Request to {$lang}");
if (array_key_exists($lang, config()->get('languages'))) {
$this->log->info('Language Switched');
session()->set('applocale', $lang);
$locale = \Locale::parseLocale($lang);
session()->set('language', $locale['language']);
}
return redirect()->back();
}
示例8: normalizeLocale
/**
* Normalize locale
*
* @param string $locale
* @return string
*/
public static function normalizeLocale($locale)
{
$parsed = IntlLocale::parseLocale($locale);
if (empty($parsed) || empty($parsed['language'])) {
return null;
}
$result = $parsed['language'];
if (!empty($parsed['region'])) {
$result .= '_' . strtoupper($parsed['region']);
}
return $result;
}
示例9: cleanLocale
/**
* Clean locale leaving only language and script
*
* @param string $locale
* @return string
*/
protected function cleanLocale($locale)
{
$localeParts = \Locale::parseLocale($locale);
$cleanLocaleParts = [];
if (isset($localeParts['language'])) {
$cleanLocaleParts['language'] = $localeParts['language'];
}
if (isset($localeParts['script'])) {
$cleanLocaleParts['script'] = $localeParts['script'];
}
return \Locale::composeLocale($cleanLocaleParts);
}
示例10: actionScan
public function actionScan($argv)
{
if (count($argv) < 1) {
exit("usage: [1;34mgini i18n scan[0m [<locales>]\n");
}
$info = \Gini\Core::moduleInfo(APP_ID);
if (!isset($info->id)) {
echo "[1;34mgini i18n scan[0m: Invalid app path!\n";
exit;
}
$path = $info->path;
$domain = str_replace('/', '-', $info->id);
$l10n_path = $path . '/' . RAW_DIR . '/l10n';
\Gini\File::ensureDir($l10n_path);
$l10n_template = $l10n_path . '/template.pot';
if (file_exists($l10n_template)) {
unlink($l10n_template);
}
$keywords = '--keyword=T';
//$package = sprintf('--package-name=%s --package-version=%s',
$cmd = sprintf('cd %s && find class view -name "*.php" -o -name "*.phtml" | xargs xgettext -LPHP %s --from-code=UTF-8 -i --copyright-holder=%s --foreign-user --package-name=%s --package-version=%s --msgid-bugs-address=%s -o %s', escapeshellarg($path), $keywords, escapeshellarg($info->author ?: 'Gini Team'), escapeshellarg($info->id), escapeshellarg($info->version), escapeshellarg($info->email ?: 'l10n@geneegroup.com'), escapeshellarg($l10n_template));
passthru($cmd);
// extract msgid ""{context}\004{txt} to msgctxt and msgid
$cmd = sprintf("sed 's/msgid \"\\(.*\\)''/msgctxt \"\\1\"\\nmsgid \"/g' %s", escapeshellarg($l10n_template));
// echo $cmd . "\n"; die;
file_put_contents($l10n_template, `{$cmd}`);
$locales = $argv;
foreach (glob($l10n_path . '/*.po') as $fname) {
$locale = basename($fname, '.po');
$locales[] = $locale;
}
foreach (array_unique($locales) as $locale) {
$locale_arr = \Locale::parseLocale($locale);
if (!isset($locale_arr['language'])) {
continue;
}
$l10n_pofile = $l10n_path . '/' . $locale . '.po';
if (!file_exists($l10n_pofile)) {
$cmd = sprintf('msginit --no-translator -o %1$s -i %2$s -l %3$s', escapeshellarg($l10n_pofile), escapeshellarg($l10n_template), escapeshellarg($locale));
} else {
$cmd = sprintf('msgmerge --update --suffix=none --no-fuzzy-matching -q %1$s %2$s', escapeshellarg($l10n_pofile), escapeshellarg($l10n_template));
}
passthru($cmd);
}
echo "done.\n";
//merge po file to different locale directory
}
示例11: toIso
/**
* Returns the ISO representation of the locale
* ("lang-REGION" as opposed to its canonical "lang_REGION")
*
* @return string
*/
public function toIso()
{
$locale = IntlLocale::parseLocale($this->getCurrent());
$result = $locale['language'];
if (!empty($locale['region'])) {
$result .= '-' . $locale['region'];
}
return $result;
}
示例12: setDefault
/**
* Sets the locale to the given locale
*
* This function will also set the "old" setlocale, mainly timezone support for PHP5.3 - PHP5.5
*
* @param string $locale Locale we want to set in RFC 4646 format
* @return string Returns the setted locale
*/
public function setDefault($locale)
{
$result = '';
if (!empty($locale)) {
$locale = \Locale::parseLocale($locale);
$locale = \Locale::composeLocale($locale);
\Locale::setDefault($locale);
$result = $this->getDefault();
$this->_setOptions();
}
return $result;
}
示例13: decompose
/**
* Parses a locale string and returns an array of key-value locale tags.
*
* @uses Locale
*
* @param string $locale
* @return string
*/
public static function decompose($locale)
{
return \Locale::parseLocale($locale);
}
示例14: normalizeLocale
/**
* Normalize locale
*
* @param string $locale
* @return string
*/
public static function normalizeLocale($locale)
{
if (empty($locale)) {
return '';
}
$parsed = IntlLocale::parseLocale($locale);
// @codeCoverageIgnoreStart
if (empty($parsed) || empty($parsed['language'])) {
return $locale;
}
// @codeCoverageIgnoreEnd
$result = strtolower($parsed['language']);
if (!empty($parsed['region'])) {
$result .= '_' . strtoupper($parsed['region']);
}
return $result;
}
示例15: setLocale
public function setLocale($category, $candidates)
{
$categoryName = self::categoryToName($category);
if (!is_array($candidates)) {
$candidates = array($candidates);
}
if (!count($candidates)) {
throw new \InvalidArgumentException('Invalid locale');
}
$base = $this->getBaseDir($this->component);
$newLocale = null;
foreach ($candidates as $candidate) {
if (!is_string($candidate)) {
throw new \InvalidArgumentException('Invalid locale');
}
$locale = \Locale::parseLocale($candidate);
if (!is_array($locale) || !isset($locale['language'])) {
throw new \InvalidArgumentException('Invalid locale');
}
// For anything else than LC_MESSAGES,
// we take the first candidate as is.
if ($categoryName != 'LC_MESSAGES') {
$newLocale = $candidate;
}
if ($newLocale !== null) {
continue;
}
$catalog = str_replace('\\', '_', ltrim($this->component, '\\'));
if (isset($locale['region'])) {
$normLocale = $locale['language'] . '_' . $locale['region'];
$file = $base . DIRECTORY_SEPARATOR . $normLocale . DIRECTORY_SEPARATOR . $categoryName . DIRECTORY_SEPARATOR . $catalog . '.mo';
if (file_exists($file)) {
$newLocale = $normLocale;
continue;
}
}
$file = $base . DIRECTORY_SEPARATOR . $locale['language'] . DIRECTORY_SEPARATOR . $categoryName . DIRECTORY_SEPARATOR . $catalog . '.mo';
if (file_exists($file)) {
$newLocale = $locale['language'];
continue;
}
}
if ($newLocale === null) {
$newLocale = 'en_US';
}
$this->locales[$category] = $newLocale;
return $newLocale;
}