|
@@ -32,8 +32,25 @@ class MyTwigExtension extends \Twig_Extension {
|
32
|
32
|
|
33
|
33
|
return $timestamp;
|
34
|
34
|
}
|
|
35
|
+
|
|
36
|
+ protected function translate_date_relative($tr, $type, $x)
|
|
37
|
+ {
|
|
38
|
+ if ($x != 1)
|
|
39
|
+ {
|
|
40
|
+ return $this->translator->trans(
|
|
41
|
+ $tr.'x_'.$type,
|
|
42
|
+ array('%x%' => $x),
|
|
43
|
+ 'messages'
|
|
44
|
+ );
|
|
45
|
+ }
|
|
46
|
+ return $this->translator->trans(
|
|
47
|
+ $tr.'one_'.$type,
|
|
48
|
+ array(),
|
|
49
|
+ 'messages'
|
|
50
|
+ );
|
|
51
|
+ }
|
35
|
52
|
|
36
|
|
- public function date_or_relative_date($sentence, $expr = null)
|
|
53
|
+ public function date_or_relative_date($sentence, $context = "default")
|
37
|
54
|
{
|
38
|
55
|
$iTimeDifference = time() - $this->datetime2timestamp($sentence);
|
39
|
56
|
if( $iTimeDifference<0 )
|
|
@@ -48,57 +65,35 @@ class MyTwigExtension extends \Twig_Extension {
|
48
|
65
|
$iMonths = round( $iTimeDifference/2419200 );
|
49
|
66
|
$iYears = round( $iTimeDifference/29030400 );
|
50
|
67
|
|
|
68
|
+ $tr = 'date_since.'.$context.'.';
|
|
69
|
+
|
51
|
70
|
if( $iSeconds<60 )
|
52
|
71
|
{
|
53
|
|
- return $this->translator->trans('date.less_than_minute', array(), 'userui');
|
|
72
|
+ return $this->translator->trans('date_since.'.$context.'.less_min', array(), 'messages');
|
54
|
73
|
}
|
55
|
74
|
elseif( $iMinutes<60 )
|
56
|
75
|
{
|
57
|
|
- return $this->translator->transChoice(
|
58
|
|
- 'il y a une minute|Il y a %count% minutes',
|
59
|
|
- $iMinutes,
|
60
|
|
- array('%count%' => $iMinutes)
|
61
|
|
- );
|
|
76
|
+ return $this->translate_date_relative($tr, 'min', $iMinutes);
|
62
|
77
|
}
|
63
|
78
|
elseif( $iHours<24 )
|
64
|
79
|
{
|
65
|
|
- return $this->translator->transChoice(
|
66
|
|
- 'il y a une heure|Il y a %count% heures',
|
67
|
|
- $iHours,
|
68
|
|
- array('%count%' => $iHours)
|
69
|
|
- );
|
|
80
|
+ return $this->translate_date_relative($tr, 'hour', $iHours);
|
70
|
81
|
}
|
71
|
82
|
elseif( $iDays<7 )
|
72
|
83
|
{
|
73
|
|
- return $this->translator->transChoice(
|
74
|
|
- 'il y a un jour|Il y a %count% jours',
|
75
|
|
- $iDays,
|
76
|
|
- array('%count%' => $iDays)
|
77
|
|
- );
|
|
84
|
+ return $this->translate_date_relative($tr, 'day', $iDays);
|
78
|
85
|
}
|
79
|
86
|
elseif( $iWeeks <4 )
|
80
|
87
|
{
|
81
|
|
- return $this->translator->transChoice(
|
82
|
|
- 'il y a une semaine|Il y a %count% semaines',
|
83
|
|
- $iWeeks,
|
84
|
|
- array('%count%' => $iWeeks)
|
85
|
|
- );
|
|
88
|
+ return $this->translate_date_relative($tr, 'week', $iWeeks);
|
86
|
89
|
}
|
87
|
90
|
elseif( $iMonths<12 )
|
88
|
91
|
{
|
89
|
|
- return $this->translator->transChoice(
|
90
|
|
- 'il y a un mois|Il y a %count% mois',
|
91
|
|
- $iMonths,
|
92
|
|
- array('%count%' => $iMonths)
|
93
|
|
- );
|
|
92
|
+ return $this->translate_date_relative($tr, 'month', $iMonths);
|
94
|
93
|
}
|
95
|
94
|
else
|
96
|
95
|
{
|
97
|
|
- return $this->translator->transChoice(
|
98
|
|
- 'il y a un an|Il y a %count% ans',
|
99
|
|
- $iYears,
|
100
|
|
- array('%count%' => $iYears)
|
101
|
|
- );
|
|
96
|
+ return $this->translate_date_relative($tr, 'year', $iYears);
|
102
|
97
|
}
|
103
|
98
|
}
|
104
|
99
|
|