-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Implement gettext expression evaluator #11993
Comments
Of course viable alternative is finding composer package which can support ternary operator (or where you implement it). |
@nijel What about this? |
That's problem for using PHP's builtin gettext, that's why we need the one we have. |
So we can find/make eval() function which will be replace the eval used inside the PHP's builtin gettext ? |
No, in our library. |
Thanx, got it. I will try if i can get some available function or implement our own. |
where in the code is |
Tip: This can be implemented quite easily using the shunting-yard algorithm, but for the ternary operator you will have to hack it a little bit. Edit: The shunting-yard algorithm will only produce an abstract syntax tree or reverse Polish notation which can be easily evaluated. |
I haven't worked on any issue or enhancement yet. I would like to work on this enhancement :-) |
@udan11 can you please give a example of the implementation |
I've started to separate the gettext code to separate module, so implementing this might rather go there: https://github.com/phpmyadmin/motranslator |
@zaverichintan There are plenty of implementation on StackOverflow. You might want to start there. |
couldn't we have semicolon or assignment operator in the statement we have to evaluate? What I found on stackoverflow is this [http://stackoverflow.com/questions/13681293/how-can-i-incorporate-ternary-operators-into-a-precedence-climbing-algorithm/18910078#18910078] |
I think it's enough to evaluate the expression part, no need to understand semicolon and assignment, these are easy to parse separately (and existing code in motranslator already does this). Let's take Arabic as an example - the expression which we need to evaluate is |
@nijel Why php-gettext library is removed? I was implementing code for expression evaluator. |
See above comments, it was moved to separate repository - https://github.com/phpmyadmin/motranslator , related code is still same: https://github.com/phpmyadmin/motranslator/blob/master/src/Translator.php |
@nijel I wrote a code which takes expression like one given in example as string and prints the results. You can find code here : https://github.com/shuklank/testprojects/blob/master/php/gettext_eval.php . I want to know how to integrate this code into main library. I have only considered plural forms given in the link. |
Few thoughts before this can be integrated:
|
@nijel Thanks for suggestion. Can I improve this code or we have to write fresh code again? I want to know if use of array instead of substr will be effective. |
Of course you can improve it :-). It doesn't matter if array access of substr call, but once you have dozen of these, it's better to store it as variable |
thanks for motivation :-)
|
Closing in favor of phpmyadmin/motranslator#5 (as this is where this code currently lives). |
JFYI: The expressions are now being evaluated by https://github.com/phpmyadmin/simple-math |
Nice work @nijel! |
Hmm, probably not so good as I've wrongly evaluated existing libraries and there is existing library which does serve our purpose well - https://packagist.org/packages/symfony/expression-language. It is more powerful (so it will be probably easier to adapt Advisor to it) and also performs better than SimpleMath (see phpmyadmin/motranslator#5 (comment)). So in the end SimpleMath will be abandoned and we will use ExpressionLanguage from Symfony. |
See #6363, #11993 and phpmyadmin/motranslator#5 Signed-off-by: Michal Čihař <michal@cihar.com>
It changes API (to consistenly use camelCase) and removes use of eval(). See #6363, #11993 and phpmyadmin/motranslator#5 Signed-off-by: Michal Čihař <michal@cihar.com>
For gettext we need something what is able to evaluate it's plural expressions and not use
eval()
(see #6363), unfortunately I was not able to find single arithmetic expression evaluation library for PHP which supports ternary operator, but they tend to support dozen of other stuff we don't need like functions. Therefore I think it will be better to implement own code for this particular use case. The final goal is to remove eval usage in gettext library.Example expression we need to evaluate for any arbitrary value of
n
:n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5
The syntax is quite limited:
n
variable? :
==
<
>
<=
=>
!=
/
%
PS: More plural examples can be found in the Localization Guide.
The text was updated successfully, but these errors were encountered: