Skip to content

Instantly share code, notes, and snippets.

Created May 21, 2012 08:19
Show Gist options
  • Save anonymous/2761141 to your computer and use it in GitHub Desktop.
Save anonymous/2761141 to your computer and use it in GitHub Desktop.
Get decimal point from cldr f.e.
<?php
namespace BLEICKER\FLOW3\I18n\Cldr\Formatter;
/* *
* This script belongs to the FLOW3 framework. *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License, either version 3 *
* of the License, or (at your option) any later version. *
* *
* The TYPO3 project - inspiring people to share! *
* */
use TYPO3\FLOW3\Annotations as FLOW3;
/**
* Formatter for symbols.
*
* @FLOW3\Scope("singleton")
*/
class NumbersSymbolFormatter implements \TYPO3\FLOW3\I18n\Formatter\FormatterInterface {
/**
* @var \BLEICKER\FLOW3\I18n\Cldr\Reader\NumbersSymbolsReader
* @FLOW3\Inject
*/
protected $numbersSymbolsReader;
/**
* Formats provided value using optional style properties
*
* @param mixed $value Formatter-specific variable to format (can be integer, \DateTime, etc)
* @param \TYPO3\FLOW3\I18n\Locale $locale Locale to use
* @param array $styleProperties Integer-indexed array of formatter-specific style properties (can be empty)
* @return string String representation of $value provided, or (string)$value
* @api
*/
public function format($value, \TYPO3\FLOW3\I18n\Locale $locale, array $styleProperties = array()) {
return $this->numbersSymbolsReader->parseSymbolFromCldr($locale,$value);
}
}
?>
<?php
namespace BLEICKER\FLOW3\I18n\Cldr\Reader;
/* *
* This script belongs to the FLOW3 framework. *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License, either version 3 *
* of the License, or (at your option) any later version. *
* *
* The TYPO3 project - inspiring people to share! *
* */
use TYPO3\FLOW3\Annotations as FLOW3;
/**
* A reader for data placed in "symbols" tag in CLDR.
*
* @FLOW3\Scope("singleton")
* @see http://www.unicode.org/reports/tr35/#Number_Elements
* @see http://www.unicode.org/reports/tr35/#Number_Format_Patterns
*/
class NumbersSymbolsReader {
/**
* @var \TYPO3\FLOW3\I18n\Cldr\CldrRepository
* @FLOW3\Inject
*/
protected $cldrRepository;
/**
* @var \TYPO3\FLOW3\Cache\Frontend\VariableFrontend
* @FLOW3\Inject
*/
protected $cache;
/**
* Associative array of symbols used in particular locales.
*
* Locale identifiers are keys for this array. Values are arrays of symbols,
* as defined in /ldml/numbers/symbols path in CLDR files.
*
* @var array
*/
protected $localizedSymbols;
/**
* An array which stores references to formats used by particular locales.
*
* @var array
*/
protected $parsedSymbolIndices;
/**
* Constructs the reader, loading parsed data from cache if available.
*
* @return void
*/
public function initializeObject() {
if ($this->cache->has('localizedSymbols')) {
$this->localizedSymbols = $this->cache->get('localizedSymbols');
}
}
/**
* Shutdowns the object, saving parsed format strings to the cache.
*
* @return void
*/
public function shutdownObject() {
$this->cache->set('localizedSymbols', $this->localizedSymbols);
}
/**
* @param \TYPO3\FLOW3\I18n\Locale $locale
* @param string $symbolType A type of symbol
* @return string the symbol
* @throws \TYPO3\FLOW3\I18n\Cldr\Reader\Exception\UnableToFindFormatException When there is no proper format string in CLDR
*/
public function parseSymbolFromCldr(\TYPO3\FLOW3\I18n\Locale $locale, $symbolType) {
if (isset($this->localizedSymbols[(string)$locale][$symbolType])) {
return $this->localizedSymbols[(string)$locale][$symbolType];
}
$formatPath = 'numbers/symbols/'.$symbolType;
$model = $this->cldrRepository->getModelForLocale($locale);
$format = $model->getElement($formatPath);
if (empty($format)) {
throw new \TYPO3\FLOW3\I18n\Cldr\Reader\Exception\UnableToFindFormatException('Symbol format was not found. Please check whether CLDR repository is valid.', 1280219795);
}
$this->localizedSymbols[(string)$locale][$symbolType] = $format;
return $format;
}
}
?>
<?php
namespace BLEICKER\Fluid\ViewHelpers\Format;
/* *
* This script belongs to the FLOW3 package "Fluid". *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License, either version 3 *
* of the License, or (at your option) any later version. *
* *
* The TYPO3 project - inspiring people to share! *
* */
use TYPO3\FLOW3\Annotations as FLOW3;
/**
* Returns translated symbol using source message or key ID.
*/
class NumbersSymbolViewHelper extends \TYPO3\Fluid\Core\ViewHelper\AbstractViewHelper {
/**
* @var \TYPO3\FLOW3\I18n\Service
* @FLOW3\Inject
*/
protected $localizationService;
/**
* @var \BLEICKER\FLOW3\I18n\Cldr\Formatter\NumbersSymbolFormatter
* @FLOW3\Inject
*/
protected $numbersSymbolFormatter;
/**
* Renders the translated label.
*
* Replaces all placeholders with corresponding values if they exist in the
* translated label.
* @var string $symbolType
* @return string
*/
public function render($symbolType) {
$locale = $this->localizationService->getConfiguration()->getDefaultLocale();
return $this->numbersSymbolFormatter->format($symbolType,$locale);
}
}
?>
# Using same Cache like NumberReader for Symbols
BLEICKER\FLOW3\I18n\Cldr\Reader\NumbersSymbolsReader:
properties:
cache:
object:
factoryObjectName: TYPO3\FLOW3\Cache\CacheManager
factoryMethodName: getCache
arguments:
1:
value: FLOW3_I18n_Cldr_Reader_NumbersReaderCache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment