Skip to content

Commit

Permalink
language specific money and date display (#180)
Browse files Browse the repository at this point in the history
* improved date translation
* language specific date and money display
  • Loading branch information
kevinpapst authored and simonschaufi committed Jun 25, 2018
1 parent f563e07 commit d4a46cf
Show file tree
Hide file tree
Showing 14 changed files with 308 additions and 37 deletions.
10 changes: 9 additions & 1 deletion config/packages/kimai.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/DependencyInjection/AppExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public function load(array $configs, ContainerBuilder $container)
$config = [];
}

$container->setParameter('kimai.languages', $config['languages']);

$this->createTimesheetParameter($config, $container);
$this->createInvoiceParameter($config, $container);
}
Expand Down
7 changes: 7 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->arrayNode('languages')
->arrayPrototype()
->children()
->scalarNode('date_short')->end()
->end()
->end()
->end()
->end()
->end();

Expand Down
88 changes: 88 additions & 0 deletions src/Twig/DateExtensions.php
Original file line number Diff line number Diff line change
@@ -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');
}
}
45 changes: 41 additions & 4 deletions src/Twig/Extensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

use App\Entity\Timesheet;
use App\Utils\Duration;
use NumberFormatter;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Intl\Intl;
use Twig\TwigFilter;

Expand All @@ -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();
}
Expand Down Expand Up @@ -107,14 +126,32 @@ public function country($country)
*/
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
Expand Down
2 changes: 1 addition & 1 deletion templates/admin/timesheet.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
8 changes: 4 additions & 4 deletions templates/invoice/renderer/print.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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 %}
Expand All @@ -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>
Expand All @@ -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">
Expand Down
4 changes: 2 additions & 2 deletions templates/invoice/renderer/timesheet.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions templates/invoice/templates.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
2 changes: 1 addition & 1 deletion templates/timesheet/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Loading

0 comments on commit d4a46cf

Please sign in to comment.