Yii2 formatter relativeTime() is not working on shared host

1.3k views Asked by At

It's working properly on local server, but when I upload it to the shared host, instead of displaying relative date it shows raw string.

code:

Yii::$app->formatter->asRelativeTime($timestamp, 'now');

local host:

in 6 days

shared host:

in {delta, plural, =1{a day} other{# days}}

Locale is set and timestamp is being passed. Intl extension is enabled. Other formatter functions (i.e. asDate) are working properly.

2

There are 2 answers

0
mt.andi On BEST ANSWER

I guess it was because of outdated ICU library of PHP intl extension. Mine was 4.2.1, while yii2 requires at least ICU version 49 to be able to use all the features, which probably include asRelativeTime().

Cannot say for sure because I'm unable to update it on my hosting plan.

3
robsch On

in 6 days is a localized string (English).

in {delta, plural, =1{a day} other{# days}} is the unlocalized template.

With each request Yii tries to localize the template. If Yii cannot localize the template for some reason it will output the template.

So it could be, that the localization files are missing: Check if vendor/yiisoft/yii2/messages contains the folders and files for your language. And check if the current localization is set to a valid value.

Read also about internationalization with Yii2.