Skip to content
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

Merged
merged 1 commit into from
Nov 2, 2016
Merged

NEW: custom FORMATS #156

merged 1 commit into from
Nov 2, 2016

Conversation

xfra35
Copy link
Member

@xfra35 xfra35 commented Oct 7, 2016

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:

// custom format definition
$f3->set('FORMATS.polish',function($nb,$mod){
  preg_match_all('/(\d)\s*\{\s*(.+?)\s*\}/',$mod,$matches,PREG_SET_ORDER);
  $terms=[];
  foreach ($matches as $m)
    $terms[$m[1]]=$m[2];
  $k=$nb==1?1:(preg_match('/(?<!1)[234]$/',$nb)?2:5);
  return isset($terms[$k]) ? str_replace('#',$nb,$terms[$k]) : $nb;
});

// usage
$line='{0, polish, 1 {# linia}, 2 {# linie}, 5 {# linii}}';
echo $f3->format($line,1); // 1 linia
echo $f3->format($line,2); // 2 linie
echo $f3->format($line,5); // 2 linii
echo $f3->format($line,12); // 12 linii
echo $f3->format($line,21); // 21 linii
echo $f3->format($line,22); // 22 linie

@SylwesterZarebski
Copy link

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 {{ @phrase }} or in code $f3->get('phrase');?

@xfra35
Copy link
Member Author

xfra35 commented Oct 7, 2016

Let's say you are running an English-Polish website.

You would declare two dictionary files:

;en.ini
score = Score: {0, plural, zero {0}, one {1 point}, other {# points}}

;pl.ini
score = Wynik: {0, polish, 1 {1 punkt}, 2 {# punkty}, 5 {# punktów}}

Then in your template:

{{ @score, 1202 | format }}

Expected output:

  • English: 1202 points
  • Polish: 1202 punkty

@SylwesterZarebski
Copy link

Thanks, i'll try this soon.

@ikkez
Copy link
Member

ikkez commented Oct 10, 2016

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:

;en.ini
score = Score: {0, plural, zero {0}, one {1 point}, other {# points}}

;pl.ini
score = Wynik: {0, plural, one {1 punkt}, #(?<!1)[2|3|4]$# {# punkty}, other {# punktów}}

which transforms the regex part between # # into \d*(?<!1)[2|3|4]$

@xfra35
Copy link
Member Author

xfra35 commented Oct 10, 2016

I'm not sure that's a good idea, for at least 3 reasons:

  • the dictionary file is meant to be filled up by a translator, usually not a techie
  • the regex would be the same everywhere, so why defining it 100 times? It just increases the probability of mistakes
  • there's a probability that some other languages require some even more complex plural forms that can't easily be handled by a regex

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.

@ikkez
Copy link
Member

ikkez commented Oct 10, 2016

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 👍

@eazuka
Copy link

eazuka commented Oct 26, 2016

@ikkez @bcosca would this PR be merged?

@xfra35
Copy link
Member Author

xfra35 commented Oct 26, 2016

We're waiting for @SylwesterZarebski's feedback on this.

@ikkez
Copy link
Member

ikkez commented Nov 2, 2016

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

@ikkez ikkez merged commit 2d85d48 into f3-factory:master Nov 2, 2016
ikkez added a commit that referenced this pull request Nov 2, 2016
@eazuka
Copy link

eazuka commented Nov 2, 2016

Nice

@xfra35
Copy link
Member Author

xfra35 commented Nov 2, 2016

@ikkez great 👍

Maybe we could hold a small repository in F3Community with various useful formats, such as this one and some non-English plurals.

@eazuka
Copy link

eazuka commented Nov 2, 2016

good idea @xfra35

@ikkez
Copy link
Member

ikkez commented Nov 2, 2016

@xfra35 sure, feel free to create one. Or maybe it's something for the wiki page?

@SylwesterZarebski
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants