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

feat: Command lang:find behavior with parameters #8899

Open
neznaika0 opened this issue May 17, 2024 · 13 comments
Open

feat: Command lang:find behavior with parameters #8899

neznaika0 opened this issue May 17, 2024 · 13 comments
Labels
enhancement PRs that improve existing functionalities

Comments

@neznaika0
Copy link
Contributor

Now, when scanning the application files, only "clean" translation keys will be added. For example, lang('File.key') but not lang('File.key', [$val]) lang('File.key', [$val], 'de')

It would be good if such options could be processed. I'm not sure about the final result yet, I'll leave this task as a reminder.

  • If you learn to recognize all these variables, you can improve the translation hints.
'key' => 'File.key {0} {1}'
'key' => 'File.key {name} {date}'
  • Identify the selected locale
@kenjis kenjis added the enhancement PRs that improve existing functionalities label Jun 2, 2024
@kenjis
Copy link
Member

kenjis commented Aug 2, 2024

I know this is a bit late, but isn't it a bug that this command can't find lang() with parameters?

@neznaika0
Copy link
Contributor Author

Initially, I did not plan this. There are many options for "gluing" the function lang("App." . $langKey . $optionKey), etc. Main mode is to search for clean keys.

@kenjis
Copy link
Member

kenjis commented Aug 2, 2024

Code like lang("App." . $langKey . $optionKey) is not good. We cannot know the key unless we run the code.
I think few users write such code.

But code like lang('Tests.apples', [3]) is more common. It is documented.
https://codeigniter4.github.io/CodeIgniter4/outgoing/localization.html#replacing-parameters

@neznaika0
Copy link
Contributor Author

I personally write this code.
That's why I implemented lang:sync - not all translations can be found automatically.

        $groups = [
            self::USER  => lang('UserGroup.role.' . self::USER),
            self::ADMIN => lang('UserGroup.role.' . self::ADMIN),
        ];

Your second example is also not absolutely correct. Most likely lang('Tests.apples', [$val])

I still haven't found a good solution to the problem.

@kenjis
Copy link
Member

kenjis commented Aug 2, 2024

Yes, lang('Tests.apples', [$val]) would be actual code.

How about this?

--- a/system/Commands/Translation/LocalizationFinder.php
+++ b/system/Commands/Translation/LocalizationFinder.php
@@ -192,7 +192,7 @@ class LocalizationFinder extends BaseCommand
         }
 
         $fileContent = file_get_contents($file->getRealPath());
-        preg_match_all('/lang\(\'([._a-z0-9\-]+)\'\)/ui', $fileContent, $matches);
+        preg_match_all('/lang\(\'([._a-z0-9\-]+)\'/ui', $fileContent, $matches);
 
         if ($matches[1] === []) {
             return compact('foundLanguageKeys', 'badLanguageKeys');

If you want a perfect solution, I think the following is better:

  • remove lang command to a Composer package
  • parse PHP code with nikic/PHP-Parser to find lang() function

@kenjis
Copy link
Member

kenjis commented Aug 11, 2024

Another feature request.
It would be nice that the command can find the case in codeigniter4/shield#1159.
When there are placeholders in messages, and lang() does not have proper arguments,
the command shows the lang() without proper arguments.

@neznaika0
Copy link
Contributor Author

Yes, this case is stated in the first message.
I have not studied the parser. It can probably be done .

@rozpuh
Copy link

rozpuh commented Aug 22, 2024

Doesn't work with Cyrillic

@neznaika0
Copy link
Contributor Author

@kenjis wrote:

parse PHP code with nikic/PHP-Parser to find lang() function

We discussed the situation when CI4 is installed from the archive. How do I use PHP-Parser?
If there is a parser, then there will be a php-cs-fixer 😐

@kenjis
Copy link
Member

kenjis commented Sep 15, 2024

I think if you really use PHP-Parser, you should create a Composer package for this command.

CI4 still supports Zip installation, that is no Composer at all.
So if we really add the feature that depends on PHP-Parser, we need to add PHP-Parser in system/ThirdParty/.
However, I don't think the members would like to add such a huge development library to ThirdParty.

@neznaika0
Copy link
Contributor Author

It's a good option. But it will be an unofficial package. I won't be able to support on github.

@neznaika0
Copy link
Contributor Author

I have created a repository with improvements. If you are interested, check it out, I will release it as a package
https://github.com/neznaika0/codeigniter-lang-finder/tree/dev

+------------------+---------------------------------------------------------------------+
| File             | Key                                                                 |
+------------------+---------------------------------------------------------------------+
| Translation-Four | Translation-Four.dashed.key-with-dash                               |
| TranslationFive  | TranslationFive.action...{0} {1} {2} {filter} {page} {search}       |
| TranslationFive  | TranslationFive.users.action...{0} {1} {2} {filter} {page} {search} |
| TranslationFive  | TranslationFive.users.post...{0} {?} {page}                         |
| TranslationThree | TranslationThree.alerts.CANCELED                                    |
| TranslationThree | TranslationThree.alerts.DELETED                                     |
| Статья           | Статья.список.действие...{0} {?} {page}                             |
+------------------+---------------------------------------------------------------------+
  1. The hint in translation means: ? - dynamic parameter, number or string - array keys.
  2. Cyrillic support.
  3. php-cs-fixer has not been added yet., depends on the developer

@neznaika0
Copy link
Contributor Author

Another feature request. It would be nice that the command can find the case in codeigniter4/shield#1159. When there are placeholders in messages, and lang() does not have proper arguments, the command shows the lang() without proper arguments.

I think it is possible to display a warning after searching for translations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement PRs that improve existing functionalities
Projects
None yet
Development

No branches or pull requests

3 participants