From 504ede06a69a504d3d531d789186bd2aee81cabb Mon Sep 17 00:00:00 2001 From: Dmitrii Fediuk Date: Wed, 31 Jan 2024 16:11:43 +0300 Subject: [PATCH] https://github.com/innomuebles/m2/issues/25 --- .gitignore | 6 + vendor/magento/framework/Locale/Format.php | 207 ++++++++++++++++++ .../library/Zend/Locale/Data/es_CR.xml | 32 +++ 3 files changed, 245 insertions(+) create mode 100644 vendor/magento/framework/Locale/Format.php create mode 100644 vendor/magento/zendframework1/library/Zend/Locale/Data/es_CR.xml diff --git a/.gitignore b/.gitignore index b723095..75b6ea1 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,9 @@ # "How to fix the «Invalid template file» / «require_js.phtml» failure of Magento 2.3.x in Windows?" # https://mage2.pro/t/5774 !/vendor/magento/framework/View/Element/Template/File/Validator.php +# 2024-01-31 Dmitrii Fediuk https://upwork.com/fl/mage2pro +# "Add the modified Magento files to the source control": https://github.com/innomuebles/m2/issues/25 +!/vendor/magento/framework/Locale/Format.php # 2021-05-14 Dmitrii Fediuk https://upwork.com/fl/mage2pro # 1) «Broken reference: the 'view.addto.compare' tries to reorder itself towards 'view.addto.wishlist', # but their parents are different: 'product.info.addto' and 'product.info.addtocart' respectively»: @@ -29,6 +32,9 @@ # but their parents are different: 'skip_gallery_before.wrapper' and 'product.info.media' respectively»: # https://github.com/innomuebles/m2/issues/19 !/vendor/magento/module-catalog/view/frontend/layout/catalog_product_view.xml +# 2024-01-31 Dmitrii Fediuk https://upwork.com/fl/mage2pro +# "Add the modified Magento files to the source control": https://github.com/innomuebles/m2/issues/25 +!/vendor/magento/zendframework1/library/Zend/Locale/Data/es_CR.xml /dev /vendor/codeception /vendor/magento/framework/Test diff --git a/vendor/magento/framework/Locale/Format.php b/vendor/magento/framework/Locale/Format.php new file mode 100644 index 0000000..7048cf7 --- /dev/null +++ b/vendor/magento/framework/Locale/Format.php @@ -0,0 +1,207 @@ +_scopeResolver = $scopeResolver; + $this->_localeResolver = $localeResolver; + $this->currencyFactory = $currencyFactory; + } + + /** + * Returns the first found number from a string. + * + * Parsing depends on given locale (grouping and decimal) + * + * Examples for input: + * ' 2345.4356,1234' = 23455456.1234 + * '+23,3452.123' = 233452.123 + * ' 12343 ' = 12343 + * '-9456km' = -9456 + * '0' = 0 + * '2 054,10' = 2054.1 + * '2'054.52' = 2054.52 + * '2,46 GB' = 2.46 + * + * @param string|float|int $value + * @return float|null + */ + public function getNumber($value) + { + if ($value === null) { + return null; + } + + if (!is_string($value)) { + return (float)$value; + } + + /** Normalize for Arabic locale */ + if (in_array($this->_localeResolver->getLocale(), self::ARABIC_LOCALE_CODES)) { + $value = $this->normalizeArabicLocale($value); + } + + //trim spaces and apostrophes + $value = preg_replace('/[^0-9^\^.,-]/m', '', $value); + + $separatorComa = strpos($value, ','); + $separatorDot = strpos($value, '.'); + + if ($separatorComa !== false && $separatorDot !== false) { + if ($separatorComa > $separatorDot) { + $value = str_replace(['.', ','], ['', '.'], $value); + } else { + $value = str_replace(',', '', $value); + } + } elseif ($separatorComa !== false) { + $locale = $this->_localeResolver->getLocale(); + /** + * It's hard code for Japan locale. + * Comma separator uses as group separator: 4,000 saves as 4,000.00 + */ + $value = str_replace( + ',', + $locale === self::JAPAN_LOCALE_CODE ? '' : '.', + $value + ); + } + + return (float)$value; + } + + /** + * Returns an array with price formatting info + * + * @param string $localeCode Locale code. + * @param string $currencyCode Currency code. + * @return array + */ + public function getPriceFormat($localeCode = null, $currencyCode = null) + { + $localeCode = $localeCode ?: $this->_localeResolver->getLocale(); + if ($currencyCode) { + $currency = $this->currencyFactory->create()->load($currencyCode); + } else { + $currency = $this->_scopeResolver->getScope()->getCurrentCurrency(); + } + + $formatter = new \NumberFormatter( + $currency->getCode() ? $localeCode . '@currency=' . $currency->getCode() : $localeCode, + \NumberFormatter::CURRENCY + ); + $format = $formatter->getPattern(); + $decimalSymbol = $formatter->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL); + $groupSymbol = $formatter->getSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL); + + // custom code starts here + if($localeCode == "es_CR") { + $decimalSymbol = '.'; + $groupSymbol = ','; + } + // custom code end here + + $pos = strpos($format, ';'); + if ($pos !== false) { + $format = substr($format, 0, $pos); + } + $format = preg_replace("/[^0\#\.,]/", '', $format); + $totalPrecision = 0; + $decimalPoint = strpos($format, '.'); + if ($decimalPoint !== false) { + $totalPrecision = strlen($format) - (strrpos($format, '.') + 1); + } else { + $decimalPoint = strlen($format); + } + $requiredPrecision = $totalPrecision; + $t = substr($format, $decimalPoint); + $pos = strpos($t, '#'); + if ($pos !== false) { + $requiredPrecision = strlen($t) - $pos - $totalPrecision; + } + + if (strrpos($format, ',') !== false) { + $group = $decimalPoint - strrpos($format, ',') - 1; + } else { + $group = strrpos($format, '.'); + } + + $result = [ + //TODO: change interface + 'pattern' => $currency->getOutputFormat(), + 'precision' => $totalPrecision, + 'requiredPrecision' => $requiredPrecision, + 'decimalSymbol' => $decimalSymbol, + 'groupSymbol' => $groupSymbol, + 'groupLength' => $group, + 'integerRequired' => $totalPrecision == 0, + ]; + + return $result; + } + + /** + * Normalizes the number of Arabic locale. + * + * Substitutes Arabic thousands grouping and Arabic decimal separator symbols (U+066C, U+066B) + * with common grouping and decimal separator + * + * @param string $value + * @return string + */ + private function normalizeArabicLocale($value): string + { + $arabicGroupSymbol = '٬'; + $arabicDecimalSymbol = '٫'; + $value = str_replace([$arabicGroupSymbol, $arabicDecimalSymbol], [',', '.'], $value); + + return $value; + } +} diff --git a/vendor/magento/zendframework1/library/Zend/Locale/Data/es_CR.xml b/vendor/magento/zendframework1/library/Zend/Locale/Data/es_CR.xml new file mode 100644 index 0000000..2d3cb82 --- /dev/null +++ b/vendor/magento/zendframework1/library/Zend/Locale/Data/es_CR.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + . + , + + + + + ¤#,##0.00;¤-#,##0.00 + + + + + + + + + +