-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NEW: custom FORMATS #156
NEW: custom FORMATS #156
Conversation
Thanks a lot, but i'm afraid i do not understand how to use in dictionary files and then in views. Could You give some example how to use with templating engine like |
Let's say you are running an English-Polish website. You would declare two dictionary files:
Then in your template:
Expected output:
|
Thanks, i'll try this soon. |
hm, do you think that it would be possible to use a regex in the plural statement instead of defining all those custom lang format handlers?! I'm thinking about something like:
which transforms the regex part between |
I'm not sure that's a good idea, for at least 3 reasons:
Also the idea of this PR is to provide the ability to define any custom format, not only pluralization formats. I don't have any practical use case at the moment, but I'm sure it may arise some time ^^ NB: Nice regex 👍 That makes the code for Polish plural a bit more concise. I'll update my example. |
Good points. alright I'm with you.. I could imagine a format for humanized "time ago" labels could also be made with this. it's a good and small addition 👍 |
We're waiting for @SylwesterZarebski's feedback on this. |
works great. here is a little sample to format like time ago: $f3->set('FORMATS.timeago',function($nb,$mod){
if (is_string($nb))
$nb=strtotime($nb);
$nb=time()-$nb;
preg_match_all('/(\w+)\s*\{\s*(.+?)\s*\}/',$mod,$matches,PREG_SET_ORDER);
$terms=[];
$gaps=[
's'=>0,
'm'=>60,
'mm'=>60*2,
'h'=>60*60,
'hh'=>60*60*2,
'd'=>60*60*24,
'dd'=>60*60*24*2,
'M'=>60*60*24*30,
'MM'=>60*60*24*30*2,
'Y'=>60*60*24*30*12,
'YY'=>60*60*24*30*12*2,
];
foreach ($matches as $m)
$terms[$m[1]]=$m[2];
$k='s';
foreach ($gaps as $gk=>$v)
if ($nb>$v)
$k=$gk;
if (strlen($k)>1)
$nb=round($nb/$gaps[$k[0]]);
return isset($terms[$k]) ? str_replace('#',$nb,$terms[$k]) : $nb;
});
$posted='Comment was posted {0, timeago,
s {just now},
m {a minute ago},
mm {# minutes ago},
h {an hour ago},
hh {# hours ago},
d {yesterday}
dd {# days ago}
M {last month}
MM {# months ago}
Y {last year}
YY {# years ago}
}';
echo $f3->format($posted,strtotime('-30 seconds'))."\n"; // just now
echo $f3->format($posted,strtotime('-65 seconds'))."\n"; // a minute ago
echo $f3->format($posted,strtotime('-300 seconds'))."\n"; // 5 minutes ago
echo $f3->format($posted,'25.10.2016')."\n"; // 8 days ago
echo $f3->format($posted,'10.06.2016')."\n"; // 5 months ago
echo $f3->format($posted,'02.08.2015')."\n"; // last year |
Nice |
@ikkez great 👍 Maybe we could hold a small repository in F3Community with various useful formats, such as this one and some non-English plurals. |
good idea @xfra35 |
It works and translation surely needs comprehensive Wiki page - just spend few hours to discover PREFIX variable needs DOT at end (of course it is in docs, but looks like end of the sentence dot). Sorry for big delay, but multilingual project was abandoned and resurrected just now. |
This is a proposal to allow users to define custom formats.
In particular, it would help fixing bcosca/fatfree#970 by providing a means to define plural schemes for languages other than English: