Last active
January 4, 2022 19:40
-
-
Save gdarko/73799ebd3c7503e869d8104503955ffc to your computer and use it in GitHub Desktop.
PHP NumberFormatter with some corrections for Macedonian language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class MkNumberFormatter | |
*/ | |
class MkNumberFormatter extends NumberFormatter { | |
public function format( $num, $type = null ) { | |
$formatted = parent::format( $num, $type ); | |
if ( 'mk' === $this->getLocale() ) { | |
$formatted = $this->postFilter( $formatted, $num, $type ); | |
} | |
return $formatted; | |
} | |
private function postFilter( $formatted, $num, $type ) { | |
if ( ! is_null( $type ) && static::SPELLOUT !== $type ) { | |
return $formatted; | |
} | |
$corrections = array( | |
'еднасто' => 'сто', | |
'двесто' => 'двеста', | |
'тристо' => 'триста', | |
'четиристо' => 'четрсто', | |
); | |
if ( $num >= 2000 ) { | |
$corrections['илјада'] = 'илјади'; | |
} else if ( $num >= 1000 && $num < 2000 ) { | |
$corrections['една илјада'] = 'илјада'; | |
} | |
// Others... | |
foreach ( $corrections as $old => $new ) { | |
$formatted = str_replace( $old, $new, $formatted ); | |
} | |
return $formatted; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'MkNumberFormatter.php'; | |
$fmt = new MkNumberFormatter( "mk", NumberFormatter::SPELLOUT ); | |
$nums = array( 1, 1143, 2143, 12143, 22143, 102143, 1002143 ); | |
$delim = php_sapi_name() == "cli" ? PHP_EOL : '<br/>'; | |
foreach ( $nums as $num ) { | |
echo sprintf( '%s%s', $fmt->format( $num ), $delim ); | |
} | |
/** | |
Output | |
еден | |
илјада сто четириесет и три | |
две илјади сто четириесет и три | |
дванаесет илјади сто четириесет и три | |
дваесет и две илјади сто четириесет и три | |
сто две илјади сто четириесет и три | |
еден милион две илјади сто четириесет и три | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment