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