CPAN Release of Time::Verbal module.
作者:gugod 發佈於: ,更新於: #perl #cpanTime::Verbal 1.1.1 made its way to CPAN.
This module dose one little things, that is to convert the time differece to a verbal format, rounded to some unit that "makes sense", just as how you would say such thing as a human.
my o = Time::Verbal->new();
say $o->distance( time(), time() - 4567 );
#=> about 1 hour
Besides the default English format, other 67 languages identified by these ISO language codes are supported:
ar bg bn-IN bs ca cy da de-AT de-CH de dsb el en-AU en-GB en-US eo es-AR es-CL es-CO es-MX es-PE es et eu fa fi fr-CA fr-CH fr fur gl-ES gsw-CH he hi-IN hi hr hsb hu id is it ja ko lo lt lv mk mn nb nl nn pl pt-BR pt-PT rm ro ru sk sl sr-Latn sr sv-SE sw tr uk vi zh-CN zh-TW
I don't know all these languages (nor their language codes), but Locale::Wolowiz does. Or, at least it is the library that does the translation, given a bunch of JSON files that contain the body of translations.
So here's how you say "about 1 hour" in Vietnamese:
my o = Time::Verbal->new( "locale" => "vi" );
say $o->distance( time(), time() - 4567 );
#=> khoảng 1 giờ
A default version of translations of all the above-mentioned 68 languages are provided with Time::Verbal
itself and is ready to use once installed properly -- a usual cpan
or cpanm
or cpm
command would suffice. However, in case some customize version is need because, say, you wish to provide better UX to whom speak the Vulcan variant of Klingon, you need to create a file named after the language code -- which can be arbitrary such as tlh-Vulcan
. You would create tlh-Vulcan.json
and put it under /app/i18n
, or really any directory of your choice, then do:
my o = Time::Verbal->new( "locale" => "tlh-Vulcan", "i18n_dir" => "/app/i18n" );
say $o->distance( time(), time() - 4567 );
#=> Chaq wa' rep
Disclaimer -- I don't know if that's how Vulcanized Klingon speaks. Don't take my example as if it is authentic.
To prepare the translation, take a look of the json files under the default i18n_dir
, which can be printed by running this oneliner:
perl -MTime::Verbal -E 'say Time::Verbal->new()->i18n_dir'
It shouldn't be difficult to grasp the structure of those translation files and produce a new one. It is however, very difficult to start describing the schema of thoes json file as an indolent developer.
The translations are accquired from rails-i18n project -- which is one of the first modules that does things like this.