Welcome Guest, Not a member yet? Register   Sign In
feat: Find lang() strings and update localization keys
#1

How would you like to add the lang:find command? It will scan all the files of the app/ project (except Languages), find all applications of lang() and update non-existent keys.
Reply
#2

The algorithm is something like this:
* We scan the system for .php files.
* We read the files and use regular expressions to find lang() inserts.
* The found array with "Filename.translate" is run in a loop.
* We match the first element with the translation file, the remaining elements are keys for translation.
If the language file exists, compare the keys, otherwise we will create a new file.
* Compare translations and update/add keys.
* Since there is no actual localizatiin, we fill it in as a key. 'title' => '# title #'

I'm sorry, I'm writing from my phone. I will add to the topic over time, if it's interesting.
Reply
#3

See https://github.com/clsmedia/ci4-translation-finder
I think the implementation of this package can be used as a reference.
Reply
#4

(This post was last modified: 08-21-2023, 09:05 PM by ozornick.)

No, i create new variant. He add multi array from Test.alert.succes.title (to [ ... ]).
I didn't see it saving to the right file. Only table with strings.
Regex for search, ok, i like...
Reply
#5

@kenjis Tell me how best to do tests when scanning the APPPATH? I will need to check the creation of files and add fakes to app/Controllers. And delete them after the tests.

Do I still need to add php-cs-fixer after execution? The array dump has incorrect formatting.
Reply
#6

(08-21-2023, 10:59 PM)ozornick Wrote: I will need to check the creation of files and add fakes to app/Controllers. And delete them after the tests.

If you need that, do it. Add fakes to app/Controllers. And delete them after the tests.

But it may be better to use Tests\Support namespace.
It is the namespace for testing. You do not need to delete after the tests.
See https://github.com/codeigniter4/CodeIgni...s/_support

(08-21-2023, 10:59 PM)ozornick Wrote: Do I still need to add php-cs-fixer after execution? The array dump has incorrect formatting.

Yes. Run `composer cs-fix` always.
Reply
#7

(This post was last modified: 08-31-2023, 06:37 AM by ozornick.)

@kenjis see pls fork https://github.com/neznaika0/codeigniter...age-finder

* The command lang :find searches for translation (lines lang('File.key')) files in the default APPPATH folder and ignoring APPPATH . Language/ path.
* The command is able to recognize nested keys normally (File.form.placeholder.text). After the update, the developer only needs to add the necessary translation, and not search through the entire lang() code
* Bad lines are skipped.
* Added some tests. After checking, the created files are cleared. I need a response for a reliable PR.

Known issues:
Each update overwrites comments in the language file
! php-cs-fixer may not work for appstarter project (if require-dev disabled)
! I would like to add translations where they possibly should be, for example, Config/Validation
It would be nice to add hints (boolean) if the translation contains variables. For example, File.form.placeholder.text {vars} will indicate the presence of some dependencies in the phrase

1. Update all in APPPATH and default locale:




Code:
$ php spark lang:find


2. Find and update translations in a specific folder (+ subfolders) and select a new locale:
Code:
$ php spark lang:find --dir Controllers/Translation --locale ru


3. Find translations and show table with new translations:


Code:
$ php spark lang:find --dir Controllers/Translation --show-new 
$ php spark lang:find --dir Controllers/Translation --show-new --locale ru


4. Detailed output. Can be added to other options:

Code:
$ php spark lang:find --verbose --dir Controllers/Translation --show-new
$ php spark lang:find --verbose --locale ru


Code:
aleksandr@aleksandr-debian:~/www/ci-demo$ php spark lang:find --dir Controllers/Translation --verbose --show-new

CodeIgniter v4.3.7 Command Line Tool - Server Time: 2023-08-31 16:15:26 UTC+03:00

Current locale: ru
Find phrases in /home/aleksandr/www/ci-demo/app/Controllers/Translation folder...
File found: Controllers/Translation/TranslationTwo.php
File found: Controllers/Translation/TranslationOne.php
File found: Controllers/Translation/TranslationThree.php
+------------------+----------------------------------------------------+
| File            | Key                                                |
+------------------+----------------------------------------------------+
| TranslationOne  | TranslationOne.Copyright                          |
| TranslationOne  | TranslationOne.DESCRIPTION                        |
| TranslationOne  | TranslationOne.last_operation_success              |
| TranslationOne  | TranslationOne.metaTags                            |
| TranslationOne  | TranslationOne.overflow_style                      |
| TranslationOne  | TranslationOne.subTitle                            |
| TranslationOne  | TranslationOne.title                              |
| TranslationThree | TranslationThree.alerts.CANCELED                  |
| TranslationThree | TranslationThree.alerts.DELETED                    |
| TranslationThree | TranslationThree.alerts.Updated                    |
| TranslationThree | TranslationThree.alerts.created                    |
| TranslationThree | TranslationThree.alerts.failed_insert              |
| TranslationThree | TranslationThree.alerts.missing_keys              |
| TranslationThree | TranslationThree.formErrors.edit.INVALID_TEXT      |
| TranslationThree | TranslationThree.formErrors.edit.empty_name        |
| TranslationThree | TranslationThree.formErrors.edit.missing_short_tag |
| TranslationThree | TranslationThree.formFields.edit.TEXT              |
| TranslationThree | TranslationThree.formFields.edit.name              |
| TranslationThree | TranslationThree.formFields.edit.short_tag        |
| TranslationThree | TranslationThree.formFields.new.TEXT              |
| TranslationThree | TranslationThree.formFields.new.name              |
| TranslationThree | TranslationThree.formFields.new.short_tag          |
+------------------+----------------------------------------------------+

Files found: 3
New translates found: 22
All operations done!

PHP Code:
// Language/{locale}/TranslationThree.php
<?php

return [
    'alerts' => [
        'created'      => 'TranslationThree.alerts.created',
        'failed_insert' => 'TranslationThree.alerts.failed_insert',
        'CANCELED'      => 'TranslationThree.alerts.CANCELED',
        'missing_keys'  => 'TranslationThree.alerts.missing_keys',
        'Updated'      => 'TranslationThree.alerts.Updated',
        'DELETED'      => 'TranslationThree.alerts.DELETED',
    ],
    'formFields' => [
        'new' => [
            'name'      => 'TranslationThree.formFields.new.name',
            'TEXT'      => 'TranslationThree.formFields.new.TEXT',
            'short_tag' => 'TranslationThree.formFields.new.short_tag',
        ],
        'edit' => [
            'name'      => 'TranslationThree.formFields.edit.name',
            'TEXT'      => 'TranslationThree.formFields.edit.TEXT',
            'short_tag' => 'TranslationThree.formFields.edit.short_tag',
        ],
    ],
    'formErrors' => [
        'edit' => [
            'empty_name'        => 'TranslationThree.formErrors.edit.empty_name',
            'INVALID_TEXT'      => 'TranslationThree.formErrors.edit.INVALID_TEXT',
            'missing_short_tag' => 'TranslationThree.formErrors.edit.missing_short_tag',
        ],
    ],
]; 
Reply
#8

The PR is in progress.
See https://github.com/codeigniter4/CodeIgniter4/pull/7896
Reply




Theme © iAndrew 2016 - Forum software by © MyBB