language specific money and date display (#180)
* improved date translation * language specific date and money display
This commit is contained in:
committed by
Simon Schaufelberger
parent
f563e0703d
commit
d4a46cf257
@@ -35,10 +35,18 @@ kimai:
|
||||
number_generator:
|
||||
default: 'App\Invoice\DateNumberGenerator'
|
||||
|
||||
# Language specific settings, like the date formats
|
||||
languages:
|
||||
de:
|
||||
date_short: "d.m.Y"
|
||||
en:
|
||||
date_short: "Y-m-d"
|
||||
ru:
|
||||
date_short: "d.m.Y"
|
||||
|
||||
twig:
|
||||
globals:
|
||||
kimai_context:
|
||||
date_1: "d.m.Y" # used for display in timesheets
|
||||
box_color: "green" # a color for ???
|
||||
active_warning: 3 # display a warning color if the user has at least X active recordings
|
||||
control_sidebar: # all tabs in the control sidebar
|
||||
|
||||
@@ -82,7 +82,12 @@ services:
|
||||
# ================================================================================
|
||||
|
||||
App\Twig\Extensions:
|
||||
arguments: ['%app_locales%']
|
||||
arguments:
|
||||
$locales: "%app_locales%"
|
||||
|
||||
App\Twig\DateExtensions:
|
||||
arguments:
|
||||
$dateSettings: "%kimai.languages%"
|
||||
|
||||
# ================================================================================
|
||||
# TIMESHEET RECORD CALCULATOR
|
||||
|
||||
@@ -33,6 +33,8 @@ class AppExtension extends Extension implements PrependExtensionInterface
|
||||
$config = [];
|
||||
}
|
||||
|
||||
$container->setParameter('kimai.languages', $config['languages']);
|
||||
|
||||
$this->createTimesheetParameter($config, $container);
|
||||
$this->createInvoiceParameter($config, $container);
|
||||
}
|
||||
|
||||
@@ -111,6 +111,13 @@ class Configuration implements ConfigurationInterface
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->arrayNode('languages')
|
||||
->arrayPrototype()
|
||||
->children()
|
||||
->scalarNode('date_short')->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end();
|
||||
|
||||
|
||||
88
src/Twig/DateExtensions.php
Normal file
88
src/Twig/DateExtensions.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Twig;
|
||||
|
||||
use DateTime;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Twig\TwigFilter;
|
||||
|
||||
/**
|
||||
* Date specific twig extensions
|
||||
*/
|
||||
class DateExtensions extends \Twig_Extension
|
||||
{
|
||||
private const FALLBACK_SHORT = 'Y-m-d';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $dateSettings;
|
||||
|
||||
/**
|
||||
* @var RequestStack
|
||||
*/
|
||||
protected $requestStack;
|
||||
|
||||
/**
|
||||
* DateExtensions constructor.
|
||||
* @param RequestStack $requestStack
|
||||
* @param array $dateSettings
|
||||
*/
|
||||
public function __construct(RequestStack $requestStack, array $dateSettings)
|
||||
{
|
||||
$this->requestStack = $requestStack;
|
||||
$this->dateSettings = $dateSettings;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFilters()
|
||||
{
|
||||
return [
|
||||
new TwigFilter('month_name', [$this, 'monthName']),
|
||||
new TwigFilter('date_short', [$this, 'dateShort']),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function getLocale()
|
||||
{
|
||||
return $this->requestStack->getCurrentRequest()->getLocale();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $context
|
||||
* @param DateTime $date
|
||||
* @return string
|
||||
*/
|
||||
public function dateShort(DateTime $date)
|
||||
{
|
||||
$locale = $this->getLocale();
|
||||
$format = self::FALLBACK_SHORT;
|
||||
|
||||
if (isset($this->dateSettings[$locale]['date_short'])) {
|
||||
$format = $this->dateSettings[$locale]['date_short'];
|
||||
}
|
||||
|
||||
return date_format($date, $format);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \DateTime $date
|
||||
* @return string
|
||||
*/
|
||||
public function monthName(\DateTime $date)
|
||||
{
|
||||
return 'month.' . $date->format('n');
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,8 @@ namespace App\Twig;
|
||||
|
||||
use App\Entity\Timesheet;
|
||||
use App\Utils\Duration;
|
||||
use NumberFormatter;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\Intl\Intl;
|
||||
use Twig\TwigFilter;
|
||||
|
||||
@@ -22,19 +24,36 @@ class Extensions extends \Twig_Extension
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $locales;
|
||||
protected $locales;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $locale;
|
||||
|
||||
/**
|
||||
* @var Duration
|
||||
*/
|
||||
protected $durationFormatter;
|
||||
|
||||
/**
|
||||
* @var NumberFormatter
|
||||
*/
|
||||
protected $numberFormatter;
|
||||
|
||||
/**
|
||||
* @var RequestStack
|
||||
*/
|
||||
protected $requestStack;
|
||||
|
||||
/**
|
||||
* Extensions constructor.
|
||||
* @param string $locales
|
||||
* @param string $locale
|
||||
*/
|
||||
public function __construct($locales)
|
||||
public function __construct(RequestStack $requestStack, $locales)
|
||||
{
|
||||
$this->requestStack = $requestStack;
|
||||
$this->locales = explode('|', $locales);
|
||||
$this->durationFormatter = new Duration();
|
||||
}
|
||||
@@ -107,14 +126,32 @@ class Extensions extends \Twig_Extension
|
||||
*/
|
||||
public function money($amount, $currency = null)
|
||||
{
|
||||
$result = number_format(round($amount, 2), 2);
|
||||
$locale = $this->getLocale();
|
||||
|
||||
if ($this->locale !== $locale) {
|
||||
$this->locale = $locale;
|
||||
$this->numberFormatter = new NumberFormatter($locale, NumberFormatter::DECIMAL);
|
||||
}
|
||||
|
||||
$fractionDigits = Intl::getCurrencyBundle()->getFractionDigits($currency);
|
||||
$amount = round($amount, $fractionDigits);
|
||||
$result = $this->numberFormatter->format($amount);
|
||||
|
||||
if (null !== $currency) {
|
||||
$result .= ' ' . Intl::getCurrencyBundle()->getCurrencySymbol($currency);
|
||||
$result .= ' ' . Intl::getCurrencyBundle()->getCurrencySymbol($currency, $locale);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function getLocale()
|
||||
{
|
||||
return $this->requestStack->getCurrentRequest()->getLocale();
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes the list of codes of the locales (languages) enabled in the
|
||||
* application and returns an array with the name of each locale written
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
|
||||
{% for entry in entries %}
|
||||
<tr>
|
||||
<td>{{ entry.begin|date(kimai_context.date_1) }}</td>
|
||||
<td>{{ entry.begin|date_short }}</td>
|
||||
|
||||
{% if not duration_only %}
|
||||
<td class="hidden-xs">{{ entry.begin|date("H:i") }}</td>
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<div class="col-xs-12">
|
||||
<h2 class="page-header">
|
||||
<i class="fa fa-globe"></i> <span contenteditable="true">{{ model.template.title }}</span>
|
||||
<small class="pull-right">{{ 'label.date'|trans }}: {{ model.invoiceDate|date(kimai_context.date_1) }}</small>
|
||||
<small class="pull-right">{{ 'label.date'|trans }}: {{ model.invoiceDate|date_short }}</small>
|
||||
</h2>
|
||||
</div>
|
||||
</div>
|
||||
@@ -36,7 +36,7 @@
|
||||
<b>{{ 'invoice.number'|trans }}: {{ model.numberGenerator.invoiceNumber }}</b>
|
||||
</p>
|
||||
<p contenteditable="true">
|
||||
<b>{{ 'invoice.due_days'|trans }}:</b> {{ model.dueDate|date(kimai_context.date_1) }}
|
||||
<b>{{ 'invoice.due_days'|trans }}:</b> {{ model.dueDate|date_short }}
|
||||
{% if model.customer.number is not empty %}
|
||||
<br><b>{{ 'label.customer_number'|trans }}:</b> {{ model.customer.number }}
|
||||
{% endif %}
|
||||
@@ -61,7 +61,7 @@
|
||||
<tbody>
|
||||
{% for entry in model.calculator.entries %}
|
||||
<tr>
|
||||
<td>{{ entry.begin|date(kimai_context.date_1) }}</td>
|
||||
<td>{{ entry.begin|date_short }}</td>
|
||||
<td>{{ entry.activity.name }} / {{ entry.activity.project.name }}</td>
|
||||
<td>{{ entry.duration|duration }}</td>
|
||||
<td>{{ entry.rate|money(model.calculator.currency) }}</td>
|
||||
@@ -84,7 +84,7 @@
|
||||
</div>
|
||||
|
||||
<div class="col-xs-6">
|
||||
<p class="lead" contenteditable="true">{{ 'invoice.due_days'|trans }} {{ model.dueDate|date(kimai_context.date_1) }}</p>
|
||||
<p class="lead" contenteditable="true">{{ 'invoice.due_days'|trans }} {{ model.dueDate|date_short }}</p>
|
||||
|
||||
<div class="table-responsive">
|
||||
<table class="table">
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
</tr>
|
||||
<tr>
|
||||
<th style="width:50%">{{ 'label.date'|trans }}</th>
|
||||
<td contenteditable="true">{{ model.invoiceDate|date('F Y') }}</td>
|
||||
<td contenteditable="true">{{ model.invoiceDate|month_name|trans }} {{ model.invoiceDate|date('Y') }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>{{ 'label.customer'|trans }}</th>
|
||||
@@ -57,7 +57,7 @@
|
||||
<tbody>
|
||||
{% for entry in model.calculator.entries %}
|
||||
<tr>
|
||||
<td>{{ entry.begin|date(kimai_context.date_1) }}</td>
|
||||
<td>{{ entry.begin|date_short }}</td>
|
||||
<td>{{ entry.activity.name }} / {{ entry.activity.project.name }}</td>
|
||||
<td>{{ entry.duration|duration }}</td>
|
||||
</tr>
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
{% block page_title %}{{ 'admin_invoice_template.title'|trans }}{% endblock %}
|
||||
{% block page_subtitle %}{{ 'admin_invoice_template.subtitle'|trans }}{% endblock %}
|
||||
{% block page_actions %}{{ widgets.page_actions({'plus-square': path('admin_invoice_template_create'), 'print': path('invoice')}) }}{% endblock %}
|
||||
|
||||
{% block main %}
|
||||
{% if entries.count == 0 %}
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
|
||||
{% for entry in entries %}
|
||||
<tr>
|
||||
<td>{{ entry.begin|date(kimai_context.date_1) }}</td>
|
||||
<td>{{ entry.begin|date_short }}</td>
|
||||
|
||||
{% if not duration_only %}
|
||||
<td>{{ entry.begin|date("H:i") }}</td>
|
||||
|
||||
96
tests/Twig/DateExtensionsTest.php
Normal file
96
tests/Twig/DateExtensionsTest.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Tests\Twig;
|
||||
|
||||
use App\Twig\DateExtensions;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Twig\TwigFilter;
|
||||
|
||||
/**
|
||||
* @covers \App\Twig\DateExtensions
|
||||
*/
|
||||
class DateExtensionsTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @param string $locale
|
||||
* @param array $dateSettings
|
||||
* @return DateExtensions
|
||||
*/
|
||||
protected function getSut($locale, array $dateSettings)
|
||||
{
|
||||
$request = new Request();
|
||||
$request->setLocale($locale);
|
||||
$requestStack = new RequestStack();
|
||||
$requestStack->push($request);
|
||||
|
||||
return new DateExtensions($requestStack, $dateSettings);
|
||||
}
|
||||
|
||||
public function testGetFilters()
|
||||
{
|
||||
$filters = ['month_name', 'date_short'];
|
||||
$sut = $this->getSut('de', []);
|
||||
$twigFilters = $sut->getFilters();
|
||||
$this->assertCount(count($filters), $twigFilters);
|
||||
$i = 0;
|
||||
foreach ($twigFilters as $filter) {
|
||||
$this->assertInstanceOf(TwigFilter::class, $filter);
|
||||
$this->assertEquals($filters[$i++], $filter->getName());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $locale
|
||||
* @param \DateTime $date
|
||||
* @param string $result
|
||||
* @dataProvider getDateShortData
|
||||
*/
|
||||
public function testDateShort($locale, \DateTime $date, $result)
|
||||
{
|
||||
$sut = $this->getSut($locale, [
|
||||
'de' => ['date_short' => 'd.m.Y'],
|
||||
'en' => ['date_short' => 'Y-m-d'],
|
||||
'ru' => ['date_short' => 'd.m.Y'],
|
||||
]);
|
||||
$this->assertEquals($result, $sut->dateShort($date));
|
||||
}
|
||||
|
||||
public function getDateShortData()
|
||||
{
|
||||
return [
|
||||
['en', new \DateTime('7 January 2010'), '2010-01-07'],
|
||||
['en', new \DateTime('2016-06-23'), '2016-06-23'],
|
||||
['de', new \DateTime('1980-12-14'), '14.12.1980'],
|
||||
['ru', new \DateTime('1980-12-14'), '14.12.1980'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \DateTime $date
|
||||
* @param string $result
|
||||
* @dataProvider getMonthData
|
||||
*/
|
||||
public function testMonthName(\DateTime $date, $result)
|
||||
{
|
||||
$sut = $this->getSut('en', []);
|
||||
$this->assertEquals($result, $sut->monthName($date));
|
||||
}
|
||||
|
||||
public function getMonthData()
|
||||
{
|
||||
return [
|
||||
[new \DateTime('January 2016'), 'month.1'],
|
||||
[new \DateTime('2016-06-23'), 'month.6'],
|
||||
[new \DateTime('2016-12-23'), 'month.12'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,8 @@ namespace App\Tests\Twig;
|
||||
use App\Entity\Timesheet;
|
||||
use App\Twig\Extensions;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Twig\TwigFilter;
|
||||
|
||||
/**
|
||||
@@ -19,10 +21,25 @@ use Twig\TwigFilter;
|
||||
*/
|
||||
class ExtensionsTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @param string $locales
|
||||
* @param string $locale
|
||||
* @return Extensions
|
||||
*/
|
||||
protected function getSut($locales, $locale = 'en')
|
||||
{
|
||||
$request = new Request();
|
||||
$request->setLocale($locale);
|
||||
$requestStack = new RequestStack();
|
||||
$requestStack->push($request);
|
||||
|
||||
return new Extensions($requestStack, $locales);
|
||||
}
|
||||
|
||||
public function testGetFilters()
|
||||
{
|
||||
$filters = ['duration', 'money', 'currency', 'country'];
|
||||
$sut = new Extensions('de');
|
||||
$sut = $this->getSut('de');
|
||||
$twigFilters = $sut->getFilters();
|
||||
$this->assertCount(count($filters), $twigFilters);
|
||||
$i = 0;
|
||||
@@ -35,7 +52,7 @@ class ExtensionsTest extends TestCase
|
||||
public function testGetFunctions()
|
||||
{
|
||||
$functions = ['locales'];
|
||||
$sut = new Extensions('de');
|
||||
$sut = $this->getSut('de');
|
||||
$twigFunctions = $sut->getFunctions();
|
||||
$this->assertCount(count($functions), $twigFunctions);
|
||||
$i = 0;
|
||||
@@ -53,7 +70,7 @@ class ExtensionsTest extends TestCase
|
||||
['code' => 'ru', 'name' => 'русский'],
|
||||
];
|
||||
|
||||
$sut = new Extensions('en|de|ru');
|
||||
$sut = $this->getSut('en|de|ru');
|
||||
$this->assertEquals($locales, $sut->getLocales());
|
||||
}
|
||||
|
||||
@@ -65,7 +82,7 @@ class ExtensionsTest extends TestCase
|
||||
'RUB' => 'RUB',
|
||||
];
|
||||
|
||||
$sut = new Extensions('en');
|
||||
$sut = $this->getSut('en');
|
||||
foreach ($symbols as $name => $symbol) {
|
||||
$this->assertEquals($symbol, $sut->currency($name));
|
||||
}
|
||||
@@ -79,33 +96,48 @@ class ExtensionsTest extends TestCase
|
||||
'ES' => 'Spain',
|
||||
];
|
||||
|
||||
$sut = new Extensions('en');
|
||||
$sut = $this->getSut('en');
|
||||
foreach ($countries as $locale => $name) {
|
||||
$this->assertEquals($name, $sut->country($locale));
|
||||
}
|
||||
}
|
||||
|
||||
public function testMoney()
|
||||
/**
|
||||
* @param string $result
|
||||
* @param int $amount
|
||||
* @param string $currency
|
||||
* @param string $locale
|
||||
* @dataProvider getMoneyData
|
||||
*/
|
||||
public function testMoney($result, $amount, $currency, $locale)
|
||||
{
|
||||
$money = [
|
||||
[2222, 'EUR', '2,222.00 €'],
|
||||
[13.75, 'USD', '13.75 $'],
|
||||
];
|
||||
$sut = $this->getSut('en', $locale);
|
||||
$this->assertEquals($result, $sut->money($amount, $currency));
|
||||
}
|
||||
|
||||
$sut = new Extensions('en');
|
||||
foreach ($money as $entry) {
|
||||
$amount = $entry[0];
|
||||
$currency = $entry[1];
|
||||
$expected = $entry[2];
|
||||
$this->assertEquals($expected, $sut->money($amount, $currency));
|
||||
}
|
||||
public function getMoneyData()
|
||||
{
|
||||
return [
|
||||
['2,345 €', 2345, 'EUR', 'en'],
|
||||
['2,345 €', 2345, 'EUR', 'en'],
|
||||
['2.345,01 €', 2345.009, 'EUR', 'de'],
|
||||
['2.345,01 €', 2345.009, 'EUR', 'de'],
|
||||
['13.75 $', 13.75, 'USD', 'en'],
|
||||
['13,75 $', 13.75, 'USD', 'de'],
|
||||
['13,75 RUB', 13.75, 'RUB', 'de'],
|
||||
['13,5 RUB', 13.50, 'RUB', 'de'],
|
||||
['13,75 ₽', 13.75, 'RUB', 'ru'],
|
||||
['14 ¥', 13.75, 'JPY', 'de'],
|
||||
['13 933 ¥', 13933.49, 'JPY', 'ru'],
|
||||
['1.234.567,89 $', 1234567.891234567890000, 'USD', 'de'],
|
||||
];
|
||||
}
|
||||
|
||||
public function testDuration()
|
||||
{
|
||||
$record = $this->getTimesheet(9437);
|
||||
|
||||
$sut = new Extensions('en');
|
||||
$sut = $this->getSut('en');
|
||||
$this->assertEquals('02:37 h', $sut->duration($record->getDuration()));
|
||||
$this->assertEquals('02:37:17 h', $sut->duration($record->getDuration(), true));
|
||||
|
||||
|
||||
@@ -111,10 +111,5 @@ kimai:
|
||||
default:
|
||||
begin: 15
|
||||
end: 15
|
||||
|
||||
twig:
|
||||
globals:
|
||||
kimai_context:
|
||||
date_1: "Y-m-d"
|
||||
```
|
||||
After changing the file you have to clear the cache with `bin/console cache:clear` or `bin/console cache:clear --env=prod`.
|
||||
|
||||
Reference in New Issue
Block a user