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

update LocaleApi and remove old metatdata access #3366

Merged
merged 9 commits into from
Jan 5, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion CHANGELOG-1.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CHANGELOG - ZIKULA 1.4.x
* 1.4.6 (?)

- BC Breaks:
- ?
- LocaleApi (introduced in 1.4.4) has been refactored and eliminates access to locale metadata.

- Deprecated:
- \StreamReader_*
Expand Down
12 changes: 3 additions & 9 deletions src/docs/Core-2.0/Api/LocaleApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,9 @@ classname: \Zikula\SettingsModule\Api\LocaleApi

service id="zikula_settings_module.locale_api"

This class loads the definitions associated with a locale and allows the developer to fetch the properties of that
locale with e.g. `$localeApi->language_direction` or `$localeApi->currency_symbol`. The data is read from the locale's
`.ini` file which is located in `/app/Resources/locale/<locale>/locale.ini`.

The class is available as a Twig global variable as `localeApi` e.g. `{{ localeApi.language_direction }}`.

The class is loaded via an onRequest listener: `\Zikula\SettingsModule\Listener\LocaleListener`
This class defines the locales that are supported based on the translations available in `app/Resources/translations`.

The class makes the following methods available:

- load($locale, $rootDir)
- __call($key, $args)
- getSupportedLocales()
- getSupportedLocaleNames()
14 changes: 0 additions & 14 deletions src/docs/Core-2.0/TranslationAndLanguage/Language.md

This file was deleted.

31 changes: 31 additions & 0 deletions src/docs/Core-2.0/TranslationAndLanguage/MigrationFromZLanguage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Replacing ZLanguage
===================

In the examples below, the provided variable (e.g. $container, $kernel, etc) must be adjusted to match the environment
from which it may be obtained. These variables are not defined explicitly in most cases.


ZLanguage::getLanguageCode() becomes $container->getParameter('locale')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about $request->getLocale()?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just saw getLocale() below.

What is the difference between ZLanguage::getLanguageCode() and ZLanguage::getLocale()?
What is the difference between $container->getParameter('locale') and $request->getLocale()?

The first is the default and the second one the current request's value only?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose so. not sure.


ZLanguage::getModuleDomain('AcmeFooModule') becomes $kernel->getModule('AcmeFooModule')->getTranslationDomain()

ZLanguage::getDirection() becomes use `dir=auto` in the template instead
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{{ localeApi.language_direction }} is deprecated then?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not deprecated - removed. broken. see also #3365

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose we could provide some BC


ZLanguage::getLanguageName($code) becomes \Intl::getLanguageBundle()->getLanguageName($locale)

ZLanguage::getInstalledLanguages() becomes $localeApi->getSupportedLocales()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great 😄


ZLanguage::getInstalledLanguageNames() becomes $localeApi->getSupportedLocaleNames()

ZLanguage::setLocale('en') becomes depends on the need. probably set in the Request $request->setLocale('en')
maybe need to set the parameter in the container or in config.yml (or both)

ZLanguage::getLocale() becomes $request->getLocale()

ZLanguage::bind*Domain('AcmeFooModule') becomes no longer needed with symfony translator

ZLanguage::getEncoding() becomes $kernel->getCharset()

ZLanguage::isRequiredLangParam() is handled automatically by the router

ZLanguage::countryMap() becomes \Intl::getRegionBundle()->getCountryNames()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also \Intl::getRegionBundle()->getCountryName($countryCode) available.

3 changes: 3 additions & 0 deletions src/lib/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
umask($kernelConfig['umask']);
}

// set default locale for Intl classes
\Locale::setDefault($kernelConfig['locale']);

// on install or upgrade, check if system requirements are met.
requirementCheck($kernelConfig);

Expand Down
2 changes: 1 addition & 1 deletion src/system/RoutesModule/Twig/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function displayPathAsString($path, $route)
$path = preg_replace_callback('#{(.*?)}#', function ($matches) use ($container, $defaults, $requirements) {
$title = '';
if (isset($defaults[$matches[1]])) {
$title .= $this->translator->__f('Default: %s', ['%s' => DataUtil::formatForDisplay($defaults[$matches[1]])]);
$title .= $this->__f('Default: %s', ['%s' => DataUtil::formatForDisplay($defaults[$matches[1]])]);
}
if (isset($requirements[$matches[1]])) {
if ($title != '') {
Expand Down
212 changes: 34 additions & 178 deletions src/system/SettingsModule/Api/LocaleApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,201 +11,57 @@

namespace Zikula\SettingsModule\Api;

use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Intl\Intl;

class LocaleApi
{
/**
* @var bool has locale been loaded?
* Locales with translations present
* @var array
*/
private $loaded = false;
private $supportedLocales = [];

/**
* @var string local name
*/
private $locale = 'en';

/**
* @var string ltr or rtl for left or right
*/
private $language_direction = 'ltr';

/**
* @var string decimal point character
*/
private $decimal_point = '.';

/**
* @var string thousands separator
*/
private $thousands_sep = ',';

/**
* @var string international currency symbol (i.e. EUR)
*/
private $int_curr_symbol = 'EUR';

/**
* @var string local currency symbol (i.e. €)
*/
private $currency_symbol = '€';

/**
* @var string monetary decimal point character
*/
private $mon_decimal_point = '.';

/**
* @var string monetary thousands separator
*/
private $mon_thousands_sep = ',';

/**
* @var string sign for positive values
*/
private $positive_sign = '';

/**
* @var string sign for negative values
*/
private $negative_sign = '-';

/**
* @var int international fractional digits
*/
private $int_frac_digits = 2;

/**
* @var int local fractional digits
*/
private $frac_digits = 2;

/**
* @var bool TRUE if currency_symbol precedes a positive value, FALSE if it succeeds one
*/
private $p_cs_precedes = true;

/**
* @var bool TRUE if a space separates currency_symbol from a positive value, FALSE otherwise
*/
private $p_sep_by_space = true;

/**
* @var bool TRUE if currency_symbol precedes a negative value, FALSE if it succeeds one
*/
private $n_cs_precedes = true;

/**
* @var bool TRUE if a space separates currency_symbol from a negative value, FALSE otherwise
*/
private $n_sep_by_space = true;

/**
* @var int
* 0 - parentheses surround the quantity and currency_symbol
* 1 - The sign string precedes the quantity and currency_symbol
* 2 - The sign string succeeds the quantity and currency_symbol
* 3 - The sign string immediately precedes the currency_symbol
* 4 - The sign string immediately succeeds the currency_symbol
*/
private $p_sign_posn = 1;

/**
* @var int
* 0 - parentheses surround the quantity and currency_symbol
* 1 - The sign string precedes the quantity and currency_symbol
* 2 - The sign string succeeds the quantity and currency_symbol
* 3 - The sign string immediately precedes the currency_symbol
* 4 - The sign string immediately succeeds the currency_symbol
*/
private $n_sign_posn = 2;

/**
* @var int 0 = Sunday, 1 Monday etc
*/
private $firstweekday = 0;

/**
* @var string Use 12/24 depending on country
*/
private $timeformat = '24';

/**
* @var array An array containing numeric groupings
*/
private $grouping = [];

/**
* @var array An array containing monetary groupings
*/
private $mon_grouping = [];

/**
* @see \Zikula\SettingsModule\Listener\LocaleListener
* @param $locale
* @param $rootDir
*/
public function load($locale, $rootDir)
{
if ($this->loaded) {
return;
}
$this->locale = $locale;
$fs = new Filesystem();
$path = $rootDir . '/Resources/locale/' . $this->locale . '/locale.ini';
if ($fs->exists($path)) {
$this->parseIniFile($path);
} else {
throw new InvalidConfigurationException('Could not load the locale configuration.');
}
$this->loaded = true;
}

/**
* Allows Twig to fetch properties without use of ArrayAccess
*
* ArrayAccess is problematic because Twig uses isset() to
* check if property field exists, so it's not possible
* to get using default values, ie, empty.
* Get array of supported locales
*
* @param $key
* @param $args
*
* @return string
* @return array
*/
public function __call($key, $args)
public function getSupportedLocales()
{
if (!$this->loaded) {
throw new InvalidConfigurationException('Did not load the locale configuration.');
if (empty($this->supportedLocales)) {
$this->supportedLocales[] = 'en';
$finder = new Finder();
$files = $finder->files()
->in(['app/Resources/translations'])
->depth(0)
->name('*.po')
->notName('*.template.*');
foreach ($files as $file) {
$fileName = $file->getBasename('.po');
list($domain, $locale) = explode('.', $fileName);
if (!in_array($locale, $this->supportedLocales)) {
$this->supportedLocales[] = $locale;
}
}
}

return $this->$key;
return $this->supportedLocales;
}

/**
* Parse an .ini file and set the object properties based on their current type.
* @param $path
* Get array of supported locales with their translated name
*
* @return array
*/
private function parseIniFile($path)
public function getSupportedLocaleNames()
{
$iniFile = parse_ini_file($path, false);
foreach ($iniFile as $key => $data) {
if (property_exists($this, $key)) {
switch (true) {
case is_bool($this->$key):
$this->$key = (bool) $data;
break;
case is_array($this->$key):
$this->$key = explode(',', $data);
break;
case is_int($this->$key):
$this->$key = (int) $data;
break;
default:
$this->$key = $data;
}
}
$locales = $this->getSupportedLocales();
$namedLocales = [];
foreach ($locales as $locale) {
$namedLocales[Intl::getLanguageBundle()->getLanguageName($locale)] = $locale;
}

return $namedLocales;
}
}
63 changes: 0 additions & 63 deletions src/system/SettingsModule/Listener/LocaleListener.php

This file was deleted.

Loading