Skip to content

Commit

Permalink
Italian Language Support (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
icyz authored Sep 28, 2021
1 parent 8178bec commit 8f4d2c3
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Speller.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class Speller
const LANGUAGE_RUSSIAN = Language::RUSSIAN;
const LANGUAGE_SPANISH = Language::SPANISH;
const LANGUAGE_POLISH = Language::POLISH;

const LANGUAGE_ITALIAN = Language::ITALIAN;

const CURRENCY_EURO = 'EUR';
const CURRENCY_BRITISH_POUND = 'GBP';
const CURRENCY_LATVIAN_LAT = 'LVL';
Expand Down
174 changes: 174 additions & 0 deletions src/languages/Italian.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

namespace js\tools\numbers2words\languages;

use js\tools\numbers2words\exceptions\UnsupportedCurrencyException;
use js\tools\numbers2words\Speller;

/**
* @internal
*/
final class Italian extends Language
{
public function spellMinus(): string
{
return 'meno';
}

public function spellMinorUnitSeparator(): string
{
return 'e';
}

public function spellHundred(int $number, int $groupOfThrees, bool $isDecimalPart, string $currency): string
{
static $tens = [
1 => 'dieci',
2 => 'venti',
3 => 'trenta',
4 => 'quaranta',
5 => 'cinquanta',
6 => 'sessanta',
7 => 'settanta',
8 => 'ottonta',
9 => 'novanta',
];
static $teens = [
11 => 'undici',
12 => 'dodici',
13 => 'tredici',
14 => 'quattordici',
15 => 'quindici',
16 => 'sedici',
17 => 'diciassette',
18 => 'diciotto',
19 => 'diciannove',
];
static $singles = [
0 => 'zero',
1 => 'uno',
2 => 'due',
3 => 'tre',
4 => 'quattro',
5 => 'cinque',
6 => 'sei',
7 => 'sette',
8 => 'otto',
9 => 'nove',
];

$text = '';

if ($number >= 100)
{
$firstDigit = intval(substr("$number", 0, 1));
if ($firstDigit === 1)
{
$text .= 'cento';
}
else
{
$text .= $singles[$firstDigit] . ' cento';
}

$number = $number % 100;

if ($number === 0) // exact hundreds
{
return $text;
}

$text .= ' ';
}

if ($number === 1 && $groupOfThrees > 1)
{
//$groupOfThrees === 2 is empty ...
if ($groupOfThrees === 3)
{
$text .= 'un';
}
}
else if ($number < 10)
{
$text .= $singles[$number];
}
else if (($number > 10) && ($number < 20))
{
$text .= $teens[$number];
}
else
{
$text .= $tens[intval(substr("$number", 0, 1))];

if ($number % 10 > 0)
{
$text .= ' ' . $singles[$number % 10];
}
}

return $text;
}

public function spellExponent(string $type, int $number, string $currency): string
{
if ($type === 'million')
{
if ($number === 1)
{
return 'milione';
}

return 'milioni';
}

if ($type === 'thousand')
{
if ($number === 1)
{
return 'mille';
}

return 'mila';
}

return '';
}

public function getCurrencyNameMajor(int $amount, string $currency): string
{
static $names = [
Speller::CURRENCY_EURO => ['euro', 'euro'],
Speller::CURRENCY_BRITISH_POUND => ['sterlina', 'sterline'],
Speller::CURRENCY_LATVIAN_LAT => ['lats', 'lats'],
Speller::CURRENCY_LITHUANIAN_LIT => ['litas', 'litas'],
Speller::CURRENCY_RUSSIAN_ROUBLE => ['rublo', 'rubli'],
Speller::CURRENCY_US_DOLLAR => ['dollaro', 'dollari'],
Speller::CURRENCY_PL_ZLOTY => ['zloty', 'zlote'],
];

return self::getCurrencyName($names, $amount, $currency);
}

public function getCurrencyNameMinor(int $amount, string $currency): string
{
static $names = [
Speller::CURRENCY_EURO => ['centesimo', 'centesimi'],
Speller::CURRENCY_BRITISH_POUND => ['penny', 'pennies'],
Speller::CURRENCY_LATVIAN_LAT => ['santim', 'santims'],
Speller::CURRENCY_LITHUANIAN_LIT => ['centas', 'centai'],
Speller::CURRENCY_RUSSIAN_ROUBLE => ['copeche', 'copechi'],
Speller::CURRENCY_US_DOLLAR => ['centesimo', 'centesimi'],
Speller::CURRENCY_PL_ZLOTY => ['grosz', 'groszy'],
];

return self::getCurrencyName($names, $amount, $currency);
}

private static function getCurrencyName(array $names, int $amount, string $currency): string
{
$index = (($amount === 1) ? 0 : 1);

return $names[$currency][$index] ?? self::throw(new UnsupportedCurrencyException($currency));
}
}
4 changes: 3 additions & 1 deletion src/languages/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ abstract class Language
public const RUSSIAN = 'ru';
public const SPANISH = 'es';
public const POLISH = 'pl';

public const ITALIAN = 'it';

private const SUPPORTED_LANGUAGE_CLASSES = [
self::ENGLISH => English::class,
self::ESTONIAN => Estonian::class,
Expand All @@ -25,6 +26,7 @@ abstract class Language
self::RUSSIAN => Russian::class,
self::SPANISH => Spanish::class,
self::POLISH => Polish::class,
self::ITALIAN => Italian::class,
];

/**
Expand Down

0 comments on commit 8f4d2c3

Please sign in to comment.