thiagocordeiro / laravel-translator

Search translation keys and insert into json to be translated
MIT License
84 stars 22 forks source link

Various improvements #23

Closed leetdev closed 4 years ago

leetdev commented 4 years ago

I have made a few more improvements to the code:

  1. Made sure all translatable strings in the code are evaluated the same way PHP evaluates them when running the code. That includes all sorts of escape sequences, which may or may not differ based on whether you're using single or double quotes. Edit: Sadly, this caused Codacy to complain over the use of the eval function, but doing this in any other way would likely require a serious overhaul of the way the strings are parsed. Edit 2: Avoided using eval by using stripcslashes for double quoted strings, and simply replacing escaped single quotes and backslashes otherwise, which should mimic the behavior of PHP exactly.
  2. Previously, when a string included a closing parenthesis, the parser would count it as the ending parenthesis of the translation function, which would break the detection of the string (usually resulting in a truncated result, or even no detection at all in certain cases). I refactored the parsing function to account for such cases properly.
  3. Added JSON_UNESCAPED_UNICODE flag to the json_encode() function call when writing the translations file. This avoids ugly escape sequences in the results as well as the case where existing keys were not found due to previously escaped unicode characters being present in the JSON file but unescaped in the actual string used in the translation function call.
  4. A minor one, but spaces before and after the opening parentheses of the translation function calls are also now supported.
thiagocordeiro commented 4 years ago

eval was a clever solution, but it's better like this once we don't use an unsafe function, thanks @leetdev for this amazing improvements

thiagocordeiro commented 4 years ago

Changes available on v1.0.3