public function testValidateUsingCountrySpecificLocale()
 {
     \Locale::setDefault('en_GB');
     $existingCountry = 'GB';
     $this->context->expects($this->never())->method('addViolation');
     $this->validator->validate($existingCountry, new Country());
 }
Beispiel #2
0
 public function setLocale($locale)
 {
     setlocale(LC_ALL, $locale);
     setlocale(LC_TIME, $locale);
     PhpLocale::setDefault($locale);
     $this->lang->setLangId(explode('_', $locale)[0]);
 }
 public function testValidateUsingCountrySpecificLocale()
 {
     \Locale::setDefault('fr_FR');
     $existingLanguage = 'en';
     $this->context->expects($this->never())->method('addViolation');
     $this->validator->validate($existingLanguage, new Language(array('message' => 'aMessage')));
 }
Beispiel #4
0
 /**
  * Should be called before tests that work fine with the stub implementation.
  */
 public static function requireIntl(\PHPUnit_Framework_TestCase $testCase, $minimumIcuVersion = null)
 {
     if (null === $minimumIcuVersion) {
         $minimumIcuVersion = Intl::getIcuStubVersion();
     }
     // We only run tests if the version is *one specific version*.
     // This condition is satisfied if
     //
     //   * the intl extension is loaded with version Intl::getIcuStubVersion()
     //   * the intl extension is not loaded
     if (($minimumIcuVersion || defined('HHVM_VERSION_ID')) && IcuVersion::compare(Intl::getIcuVersion(), $minimumIcuVersion, '!=', 1)) {
         $testCase->markTestSkipped('ICU version ' . $minimumIcuVersion . ' is required.');
     }
     // Normalize the default locale in case this is not done explicitly
     // in the test
     \Locale::setDefault('en');
     // Consequently, tests will
     //
     //   * run only for one ICU version (see Intl::getIcuStubVersion())
     //     there is no need to add control structures to your tests that
     //     change the test depending on the ICU version.
     //
     // Tests should only rely on functionality that is implemented in the
     // stub classes.
 }
Beispiel #5
0
 /**
  * Append objects to the global dependency injection container.
  *
  * @param Pimple\Container $container
  */
 public static function setupDependencyInjection(Container $container)
 {
     parent::setupDependencyInjection($container);
     $locale = $container['session']->get('_locale', 'en');
     $container['request']->setLocale($locale);
     \Locale::setDefault($locale);
 }
Beispiel #6
0
 public function testConstructor()
 {
     // Testing defaults
     $validator = new CurrencyValidator();
     $this->assertEquals('en_US', $validator->getLocale());
     $this->assertEquals(Uncurrency::DEFAULT_SCALE_CORRECTNESS, $validator->getScaleCorrectness());
     $this->assertEquals(Uncurrency::DEFAULT_CURRENCY_CORRECTNESS, $validator->getCurrencyCorrectness());
     $this->assertEquals(CurrencyValidator::DEFAULT_NEGATIVE_ALLOWED, $validator->isNegativeAllowed());
     // Testing application locale
     \Locale::setDefault('de_DE');
     $validator = new CurrencyValidator();
     $this->assertEquals('de_DE', $validator->getLocale());
     // Testing locale
     $validator = new CurrencyValidator(['locale' => 'it_IT']);
     $this->assertEquals('it_IT', $validator->getLocale());
     // Testing options
     $validator = new CurrencyValidator(['locale' => 'it_IT', 'currency_code' => 'EUR', 'scale_correctness' => false, 'currency_correctness' => false, 'negative_allowed' => false]);
     $this->assertFalse($validator->getScaleCorrectness());
     $this->assertFalse($validator->getCurrencyCorrectness());
     $this->assertFalse($validator->isNegativeAllowed());
     // Testing traversable
     $traversableOpts = new ArrayObject(['locale' => 'it_IT', 'currency_code' => 'EUR', 'scale_correctness' => true, 'currency_correctness' => false, 'negative_allowed' => true]);
     $validator = new CurrencyValidator();
     $validator->setOptions($traversableOpts);
     $this->assertEquals('it_IT', $validator->getLocale());
     $this->assertTrue($validator->getScaleCorrectness());
     $this->assertFalse($validator->getCurrencyCorrectness());
     $this->assertTrue($validator->isNegativeAllowed());
 }
 /**
  * @param MvcEvent $event
  */
 public function onBootstrap(MvcEvent $event)
 {
     $eventManager = $event->getApplication()->getEventManager();
     $moduleRouteListener = new ModuleRouteListener();
     $moduleRouteListener->attach($eventManager);
     $application = $event->getApplication();
     $eventManager = $application->getEventManager();
     /* @var $serviceManager ServiceManager */
     $serviceManager = $application->getServiceManager();
     $config = $serviceManager->get('config');
     if (array_key_exists('locale', $config)) {
         $config = $config['locale'];
     } else {
         $config = null;
     }
     if (!$config) {
         Locale::setDefault('en');
         // hardcoded here
         $translator = $serviceManager->get('translator');
         AbstractValidator::setDefaultTranslator($translator);
         return;
     }
     $settings = new Settings($config);
     // set the locale based on URL from route or cookie & set the default translator to all the app
     $eventManager->attach(MvcEvent::EVENT_ROUTE, function (MvcEvent $e) use($config, $settings, $serviceManager) {
         $settings->setRouteMatch($e->getRouteMatch());
         if (!$e->getRequest() instanceof Request) {
             $settings->setRequest($e->getRequest());
         }
         \Locale::setDefault($settings->getLocale());
         $translator = $serviceManager->get('translator');
         AbstractValidator::setDefaultTranslator($translator);
     }, -9);
 }
 public function testOverrideCurrency()
 {
     \Locale::setDefault("fr_FR");
     $form = $this->factory->create($this->simpleMoneyTypeClass, null, ["currency" => "USD"]);
     $form->submit(array("tbbc_amount" => '1 252,5'));
     $this->assertEquals(Money::USD(125250), $form->getData());
 }
 public function testDefaultLocaleFromHttpHeader()
 {
     $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'sv-se,sv;q=0.8,en-us;q=0.6,en;q=0.4';
     \Locale::setDefault('fi');
     \Library\Application::init('Console', true);
     $this->assertEquals('sv_SE', \Locale::getDefault());
 }
Beispiel #10
0
 protected function tearDown()
 {
     parent::tearDown();
     \Locale::setDefault($this->locale);
     unset($this->locale);
     unset($this->formType);
 }
 /**
  * Should be called before tests that require a feature-complete intl
  * implementation.
  *
  * @param \PhpUnit_Framework_TestCase $testCase
  */
 public static function requireFullIntl(\PhpUnit_Framework_TestCase $testCase)
 {
     // We only run tests if the intl extension is loaded...
     if (!Intl::isExtensionLoaded()) {
         $testCase->markTestSkipped('The intl extension is not available.');
     }
     // ... and only if the version is *one specific version* ...
     if (IcuVersion::compare(Intl::getIcuVersion(), Intl::getIcuStubVersion(), '!=', $precision = 1)) {
         $testCase->markTestSkipped('Please change ICU version to ' . Intl::getIcuStubVersion());
     }
     // ... and only if the data in the Icu component matches that version.
     if (IcuVersion::compare(Intl::getIcuDataVersion(), Intl::getIcuStubVersion(), '!=', $precision = 1)) {
         $testCase->markTestSkipped('Please change the Icu component to version 1.0.x or 1.' . IcuVersion::normalize(Intl::getIcuStubVersion(), 1) . '.x');
     }
     // Normalize the default locale in case this is not done explicitly
     // in the test
     \Locale::setDefault('en');
     // Consequently, tests will
     //
     //   * run only for one ICU version (see Intl::getIcuStubVersion())
     //     there is no need to add control structures to your tests that
     //     change the test depending on the ICU version.
     //   * always use the C intl classes
     //   * always use the binary resource bundles (any locale is allowed)
 }
 protected function setUp()
 {
     \Locale::setDefault('en');
     $dispatcher = new EventDispatcher();
     $this->csrfProvider = $this->getMock('Symfony\\Component\\Form\\Extension\\Csrf\\CsrfProvider\\CsrfProviderInterface');
     $this->factory = new FormFactory(array(new CoreExtension(), new CsrfExtension($this->csrfProvider)));
 }
Beispiel #13
0
 /**
  * @dataProvider getValidCurrencies
  **/
 public function testValidCurrenciesWithCountrySpecificLocale($currency)
 {
     IntlTestHelper::requireFullIntl($this);
     \Locale::setDefault('en_GB');
     $this->validator->validate($currency, new Currency());
     $this->assertNoViolation();
 }
Beispiel #14
0
 public function testToString($value, $currencyCode, $expectedOutput)
 {
     if (Intl::isExtensionLoaded()) {
         \Locale::setDefault('en');
     }
     $this->if($price = new TestedPrice($value, $currencyCode))->phpString((string) $price)->isIdenticalTo($expectedOutput);
 }
Beispiel #15
0
 public function testMoneyPatternWorksForYen()
 {
     \Locale::setDefault('en_US');
     $form = $this->factory->create('money', null, array('currency' => 'JPY'));
     $view = $form->createView();
     $this->assertTrue((bool) strstr($view->get('money_pattern'), '¥'));
 }
Beispiel #16
0
 /**
  * @dataProvider missingTranslationProvider
  */
 public function testMissingTranslationDoesNotTriggerNoticeWhenDisabled($locale)
 {
     \Locale::setDefault($locale);
     $application = \Library\Application::init('Library', true, array('Library\\UserConfig' => array('debug' => array('report missing translations' => false))));
     $translator = $application->getServiceManager()->get('MvcTranslator');
     $this->assertEquals('this_string_is_not_translated', $translator->translate('this_string_is_not_translated'));
 }
Beispiel #17
0
 public function format($datetime, $dateFormat = null, $timeFormat = null, $timezone = null, $locale = null)
 {
     $dt = null;
     $tz = null;
     if (null !== $timezone) {
         $tz = is_string($timezone) ? new \DateTimeZone($timezone) : $timezone;
     }
     if ($datetime instanceof \DateTimeImmutable) {
         $dt = new \DateTime($datetime->format('Y-m-d H:i:s'), $tz);
     } elseif (!$datetime instanceof \DateTime) {
         $dt = new \DateTime($datetime, $tz);
     } else {
         $dt = clone $datetime;
     }
     if (null === $tz) {
         $tz = $dt->getTimezone();
     }
     if (null === $locale) {
         $locale = \Locale::getDefault();
     }
     if (null === $dateFormat) {
         $dateFormat = \IntlDateFormatter::MEDIUM;
     }
     if (null === $timeFormat) {
         $timeFormat = \IntlDateFormatter::NONE;
     }
     $currentLocale = \Locale::getDefault();
     \Locale::setDefault($locale);
     $formatter = new \IntlDateFormatter($locale, $dateFormat, $timeFormat, $tz);
     $result = $formatter->format($dt);
     \Locale::setDefault($currentLocale);
     return $result;
 }
Beispiel #18
0
 /**
  * Listens to the route event.
  *
  * Detects the language to use and sets translator locale.
  * The language is detected either via query parameter "lang" or
  * browser setting (ACCEPT-LANGUAGE header)
  *
  * @param MvcEvent $e
  */
 public function onRoute(MvcEvent $e)
 {
     /* @var $request \Zend\Http\PhpEnvironment\Request */
     $request = $e->getRequest();
     /* Detect language */
     $lang = $request->getQuery('lang');
     if (!$lang) {
         $headers = $request->getHeaders();
         if ($headers->has('Accept-Language')) {
             /* @var $acceptLangs \Zend\Http\Header\AcceptLanguage */
             $acceptLangs = $headers->get('Accept-Language');
             $locales = $acceptLangs->getPrioritized();
             $locale = $locales[0];
             $lang = $locale->type;
         } else {
             $lang = 'en';
         }
     }
     /* Set locale */
     $translator = $e->getApplication()->getServiceManager()->get('mvctranslator');
     $locale = $lang . '_' . strtoupper($lang);
     setlocale(LC_ALL, array($locale . ".utf8", $locale . ".iso88591", $locale, substr($locale, 0, 2), 'de_DE.utf8', 'de_DE', 'de'));
     \Locale::setDefault($locale);
     $translator->setLocale($locale);
     $routeMatch = $e->getRouteMatch();
     if ($routeMatch && $routeMatch->getParam('lang') === null) {
         $routeMatch->setParam('lang', $lang);
     }
     /* @var $router \Zend\Mvc\Router\SimpleRouteStack */
     $router = $e->getRouter();
     $router->setDefaultParam('lang', $lang);
 }
Beispiel #19
0
 protected function setUp()
 {
     parent::setUp();
     // we test against "de_DE", so we need the full implementation
     IntlTestHelper::requireFullIntl($this);
     \Locale::setDefault('de_DE');
 }
Beispiel #20
0
 public function onBootstrap($e)
 {
     \Locale::setDefault('de_DE');
     $sm = $e->getApplication()->getServiceManager();
     $translator = $sm->get('translator');
     \Zend\Validator\AbstractValidator::setDefaultTranslator(new \Zend\Mvc\I18n\Translator($translator));
 }
 public function __construct(Dispatcher $dispatcher)
 {
     $request = $this->getDI()->get('request');
     $queryLang = $request->getQuery('lang');
     if (!$queryLang) {
         $lang = $dispatcher->getParam('lang');
     } else {
         $lang = $queryLang;
     }
     switch ($lang) {
         case 'uk':
             define('LANG', 'uk');
             define('LANG_SUFFIX', '_uk');
             define('LANG_URL', '/uk');
             define('LOCALE', 'uk_UA');
             break;
         case 'en':
             define('LANG', 'en');
             define('LANG_SUFFIX', '_en');
             define('LANG_URL', '/en');
             define('LOCALE', 'en_EN');
             break;
         default:
             define('LANG', 'ru');
             define('LANG_SUFFIX', '');
             define('LANG_URL', '/');
             define('LOCALE', 'ru_RU');
     }
     Locale::setDefault(LOCALE);
     $this->getDI()->set('translate', new \Application\Localization\GettextAdapter(array('locale' => LOCALE, 'lang' => LANG, 'file' => 'messages', 'directory' => APPLICATION_PATH . '/lang')));
 }
 public function testPassMoneyPatternToView()
 {
     \Locale::setDefault('de_DE');
     $form = $this->factory->create('money');
     $view = $form->createView();
     $this->assertSame('{{ widget }} €', $view->get('money_pattern'));
 }
 public function testValidateUsingCountrySpecificLocale()
 {
     \Locale::setDefault('fr_FR');
     $existingLanguage = 'en';
     $this->validator->validate($existingLanguage, new Language(array('message' => 'aMessage')));
     $this->assertNoViolation();
 }
Beispiel #24
0
 /**
  * setLocale
  * NOTE: We do NOT set LC_ALL because it causes "n tilde"
  * chars to be not json encodable after they have been strtolower'd
  *
  * @param string|null $locale
  *
  * @return void
  */
 public function setLocale($locale = null)
 {
     if (empty($locale)) {
         $locale = $this->getDefaultLocale();
     }
     if (empty($locale)) {
         // @todo warning or error
         return;
     }
     /* Conversion for Ubuntu and Mac local settings. */
     if (!setlocale(LC_MONETARY, $locale . '.utf8')) {
         if (!setlocale(LC_MONETARY, $locale . '.UTF-8')) {
             setlocale(LC_MONETARY, 'en_US.UTF-8');
         }
     }
     /* Conversion for Ubuntu and Mac local settings. */
     if (!setlocale(LC_NUMERIC, $locale . '.utf8')) {
         if (!setlocale(LC_NUMERIC, $locale . '.UTF-8')) {
             setlocale(LC_NUMERIC, 'en_US.UTF-8');
         }
     }
     /* Conversion for Ubuntu and Mac local settings. */
     if (!setlocale(LC_TIME, $locale . '.utf8')) {
         if (!setlocale(LC_TIME, $locale . '.UTF-8')) {
             setlocale(LC_TIME, 'en_US.UTF-8');
         }
     }
     \Locale::setDefault($locale);
 }
Beispiel #25
0
 public function __construct($applicationName = null)
 {
     \Locale::setDefault('en');
     $this->faker = FakerFactory::create();
     if (null !== $applicationName) {
         $this->applicationName = $applicationName;
     }
 }
 public function testReverseTransformWithPrecision()
 {
     // Since we test against "de_AT", we need the full implementation
     IntlTestHelper::requireFullIntl($this);
     \Locale::setDefault('de_AT');
     $transformer = new PercentToLocalizedStringTransformer(2);
     $this->assertEquals(0.1234, $transformer->reverseTransform('12,34'));
 }
 public function testSetData()
 {
     \Locale::setDefault("fr_FR");
     $form = $this->factory->create($this->moneyTypeClass, null, array("currency_type" => $this->currencyTypeClass));
     $form->setData(Money::EUR(120));
     $formView = $form->createView();
     $this->assertEquals("1,20", $formView->children["tbbc_amount"]->vars["value"]);
 }
 protected function setUp()
 {
     \Locale::setDefault('en');
     $dispatcher = new EventDispatcher();
     $this->csrfProvider = $this->getMock('Symfony\\Component\\Form\\Extension\\Csrf\\CsrfProvider\\CsrfProviderInterface');
     $storage = new \Symfony\Component\HttpFoundation\File\TemporaryStorage('foo', \sys_get_temp_dir());
     $this->factory = new FormFactory(array(new CoreExtension($storage), new CsrfExtension($this->csrfProvider)));
 }
 public function testValidateUsingCountrySpecificLocale()
 {
     IntlTestHelper::requireFullIntl($this, false);
     \Locale::setDefault('fr_FR');
     $existingLanguage = 'en';
     $this->validator->validate($existingLanguage, new Language(array('message' => 'aMessage')));
     $this->assertNoViolation();
 }
 public function testSetData()
 {
     \Locale::setDefault("fr_FR");
     $form = $this->factory->create($this->currencyTypeClass, null, array());
     $form->setData(new Currency("USD"));
     $formView = $form->createView();
     $this->assertEquals("USD", $formView->children["tbbc_name"]->vars["value"]);
 }