Skip to content

Commit

Permalink
Added alias fr_FR for french locale (fr) (#175)
Browse files Browse the repository at this point in the history
Co-authored-by: Karol Wnuk <[email protected]>
  • Loading branch information
Bryce-Colton and kwn authored Nov 4, 2024
1 parent 2144ba0 commit 4eecd25
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 40 deletions.
76 changes: 38 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,44 +75,44 @@ Note: The Currency Transformer within this library processes integers; ensure yo

## Available locale

| Language | Identifier | Number | Currency |
|----------------------|-----------| ------ | -------- |
| Albanian | al | + | + |
| Arabic | ar | + | + |
| Azerbaijani | az | + | + |
| Belgian French | fr_BE | + | - |
| Brazilian Portuguese | pt_BR | + | + |
| Bulgarian | bg | + | + |
| Czech | cs | + | - |
| Danish | dk | + | + |
| Dutch | nl | + | - |
| English | en | + | + |
| Estonian | et | + | - |
| Georgian | ka | + | + |
| German | de | + | + |
| French | fr | + | + |
| Hungarian | hu | + | + |
| Indonesian | id | + | + |
| Italian | it | + | - |
| Kurdish | ku | + | - |
| Lithuanian | lt | + | + |
| Latvian | lv | + | + |
| Macedonian | mk | + | - |
| Malay | ms | + | + |
| Persian | fa | + | - |
| Polish | pl | + | + |
| Romanian | ro | + | + |
| Serbian | sr | + | + |
| Slovak | sk | + | + |
| Spanish | es | + | + |
| Russian | ru | + | + |
| Swahili | sw | + | + |
| Swedish | sv | + | - |
| Turkish | tr | + | + |
| Turkmen | tk | + | + |
| Ukrainian | ua | + | + |
| Uzbek | uz | + | + |
| Yoruba | yo | + | + |
| Language | Identifier | Number | Currency | Alias |
|----------------------|------------| ------ | -------- |-------|
| Albanian | al | + | + | |
| Arabic | ar | + | + | |
| Azerbaijani | az | + | + | |
| Belgian French | fr_BE | + | - | |
| Brazilian Portuguese | pt_BR | + | + | |
| Bulgarian | bg | + | + | |
| Czech | cs | + | - | |
| Danish | dk | + | + | |
| Dutch | nl | + | - | |
| English | en | + | + | |
| Estonian | et | + | - | |
| Georgian | ka | + | + | |
| German | de | + | + | |
| French | fr | + | + | fr_FR |
| Hungarian | hu | + | + | |
| Indonesian | id | + | + | |
| Italian | it | + | - | |
| Kurdish | ku | + | - | |
| Lithuanian | lt | + | + | |
| Latvian | lv | + | + | |
| Macedonian | mk | + | - | |
| Malay | ms | + | + | |
| Persian | fa | + | - | |
| Polish | pl | + | + | |
| Romanian | ro | + | + | |
| Serbian | sr | + | + | |
| Slovak | sk | + | + | |
| Spanish | es | + | + | |
| Russian | ru | + | + | |
| Swahili | sw | + | + | |
| Swedish | sv | + | - | |
| Turkish | tr | + | + | |
| Turkmen | tk | + | + | |
| Ukrainian | ua | + | + | |
| Uzbek | uz | + | + | |
| Yoruba | yo | + | + | |

## Contributors

Expand Down
13 changes: 12 additions & 1 deletion src/Concerns/ManagesCurrencyTransformers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
use NumberToWords\CurrencyTransformer as Transformer;
use NumberToWords\Exception\InvalidArgumentException;
use NumberToWords\CurrencyTransformer\CurrencyTransformer;
use NumberToWords\Exception\NumberToWordsException;

trait ManagesCurrencyTransformers
{
use ManagesLocaleAlias;

private array $currencyTransformers = [
'ar' => Transformer\ArabicCurrencyTransformer::class,
'al' => Transformer\AlbanianCurrencyTransformer::class,
Expand Down Expand Up @@ -41,6 +44,7 @@ trait ManagesCurrencyTransformers
*/
public function getCurrencyTransformer(string $language): CurrencyTransformer
{
$language = $this->resolveAlias($language);
if (!array_key_exists($language, $this->currencyTransformers)) {
throw new InvalidArgumentException(sprintf(
'Currency transformer for "%s" language is not implemented.',
Expand All @@ -51,8 +55,15 @@ public function getCurrencyTransformer(string $language): CurrencyTransformer
return new $this->currencyTransformers[$language]();
}

/**
* @throws NumberToWordsException
* @throws InvalidArgumentException
*/
public static function transformCurrency(string $language, int $number, string $currency): string
{
return (new static())->getCurrencyTransformer($language)->toWords($number, $currency);
$static = new static();
$language = $static->resolveAlias($language);

return $static->getCurrencyTransformer($language)->toWords($number, $currency);
}
}
19 changes: 19 additions & 0 deletions src/Concerns/ManagesLocaleAlias.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace NumberToWords\Concerns;

trait ManagesLocaleAlias
{
private array $aliasedLocale = [
'fr_FR' => 'fr',
];

private function resolveAlias(string $alias): string
{
if (array_key_exists($alias, $this->aliasedLocale)) {
return $this->aliasedLocale[$alias];
}

return $alias;
}
}
13 changes: 12 additions & 1 deletion src/Concerns/ManagesNumberTransformers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace NumberToWords\Concerns;

use NumberToWords\Exception\NumberToWordsException;
use NumberToWords\NumberTransformer as Transformer;
use NumberToWords\Exception\InvalidArgumentException;
use NumberToWords\NumberTransformer\NumberTransformer;

trait ManagesNumberTransformers
{
use ManagesLocaleAlias;

private array $numberTransformers = [
'ar' => Transformer\ArabicNumberTransformer::class,
'al' => Transformer\AlbanianNumberTransformer::class,
Expand Down Expand Up @@ -52,6 +55,7 @@ trait ManagesNumberTransformers
*/
public function getNumberTransformer(string $language): NumberTransformer
{
$language = $this->resolveAlias($language);
if (!array_key_exists($language, $this->numberTransformers)) {
throw new InvalidArgumentException(sprintf(
'Number transformer for "%s" language is not implemented.',
Expand All @@ -62,8 +66,15 @@ public function getNumberTransformer(string $language): NumberTransformer
return new $this->numberTransformers[$language]();
}

/**
* @throws NumberToWordsException
* @throws InvalidArgumentException
*/
public static function transformNumber(string $language, int $number): string
{
return (new static())->getNumberTransformer($language)->toWords($number);
$static = new static();
$language = $static->resolveAlias($language);

return $static->getNumberTransformer($language)->toWords($number);
}
}
35 changes: 35 additions & 0 deletions tests/LocaleAliasTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace NumberToWords;

use NumberToWords\Exception\InvalidArgumentException;
use PHPUnit\Framework\TestCase;

class LocaleAliasTest extends TestCase
{

/**
* @dataProvider providerItUsesSameTransformersWithLocaleAlias
* @throws InvalidArgumentException
*/
public function testItUsesSameTransformersWithLocaleAlias($locale, $alias): void
{
$original = new NumberToWords();
$originalNumberTransformer = $original->getNumberTransformer($locale);
$originalCurrencyTransformer = $original->getCurrencyTransformer($locale);

$numberToWords = new NumberToWords();
$aliasedNumberTransformer = $numberToWords->getNumberTransformer($alias);
$aliasedCurrencyTransformer = $numberToWords->getCurrencyTransformer($alias);

$this->assertInstanceOf(get_class($originalNumberTransformer), $aliasedNumberTransformer);
$this->assertInstanceOf(get_class($originalCurrencyTransformer), $aliasedCurrencyTransformer);
}

public function providerItUsesSameTransformersWithLocaleAlias(): array
{
return [
['fr', 'fr_FR'],
];
}
}

0 comments on commit 4eecd25

Please sign in to comment.