potsky / laravel-localization-helpers

🎌 Artisan commands to generate and update lang files automatically
GNU General Public License v3.0
187 stars 38 forks source link

Auto generated / dynamic keys not working #22

Closed marvinschroeder closed 8 years ago

marvinschroeder commented 8 years ago

In your config file is the option for never_obsolete_keys and your example for this option.

But when we have something like

trans("app.sections.$section->id")

this lemma is never found at all. So setting sections as a key in never_obsolete_keys has no effect because the inital lemma wasn't found.

potsky commented 8 years ago

Indeed, this lemma will be never found. In fact it is found but I reject it because there is a dollar char inside (so there is PHP code, so it is dynamic).

You need so to manually create lemma "sections.1", "sections.2", ... in your app lang file.

If you run the missing command without telling that sections is a reserved work in your code for dynamic lemmas, all manual entries of your app lang file will be considered as obsolete and then removed.

So setting sections as a key in never_obsolete_keys has the effect to not delete all your work :-)

marvinschroeder commented 8 years ago

I did this with the following data:

app.php

return [
  'sections' => [
        1 => [
            'name' => 'Example',
            'name-short' => 'Ex',
        ],
        2 => [
            'name' => 'Test',
            'name-short' => 'Test',
        ],
];

Then i defined the key sections in the never_obsolete_keys config option but the whole sections array is removed.

potsky commented 8 years ago

Have you set the configuration like this ?

'never_obsolete_keys' => array( 'sections' )
marvinschroeder commented 8 years ago

Yes i did!

potsky commented 8 years ago

Can you help me to solve this problem?

Go to the package folder and search for the /src/Potsky/LaravelLocalizationHelpers/Command/LocalizationMissing.php file

At line 365, you should see :

if ( count( $obsolete_lemmas ) > 0 )
{
    // Remove all dynamic fields
    foreach ( $obsolete_lemmas as $key => $value )
    {
        foreach ( $this->never_obsolete_keys as $remove )
        {
            if ( ( strpos( $key , '.' . $remove . '.' ) !== false ) || starts_with( $key , $remove . '.' ) )
            {
                unset( $obsolete_lemmas[ $key ] );
            }
        }
    }
}

Could you replace by :

if ( count( $obsolete_lemmas ) > 0 )
{
    // Remove all dynamic fields
    foreach ( $obsolete_lemmas as $key => $value )
    {
        error_log( '1. ' . $key );

        foreach ( $this->never_obsolete_keys as $remove )
        {
            error_log( '2. ' . $remove );

            if ( ( strpos( $key , '.' . $remove . '.' ) !== false ) || starts_with( $key , $remove . '.' ) )
            {
                error_log( 'UNSET' );

                unset( $obsolete_lemmas[ $key ] );
            }
        }
    }
}

and send me the server error log output please?

marvinschroeder commented 8 years ago
[2016-02-01 14:46:25] development.INFO: 1. sections.1.name  
[2016-02-01 14:46:25] development.INFO: 2. payment-methods  
[2016-02-01 14:46:25] development.INFO: 2. sections  
[2016-02-01 14:46:25] development.INFO: UNSET  
[2016-02-01 14:46:25] development.INFO: 1. sections.1.name-short  
[2016-02-01 14:46:25] development.INFO: 2. payment-methods  
[2016-02-01 14:46:25] development.INFO: 2. sections  
[2016-02-01 14:46:25] development.INFO: UNSET  
[2016-02-01 14:46:25] development.INFO: 1. sections.10.name  
[2016-02-01 14:46:25] development.INFO: 2. payment-methods  
[2016-02-01 14:46:25] development.INFO: 2. sections  
[2016-02-01 14:46:25] development.INFO: UNSET  
[2016-02-01 14:46:25] development.INFO: 1. sections.10.name-short  
[2016-02-01 14:46:25] development.INFO: 2. payment-methods  
[2016-02-01 14:46:25] development.INFO: 2. sections  
[2016-02-01 14:46:25] development.INFO: UNSET  
[2016-02-01 14:46:25] development.INFO: 1. sections.11.name  
[2016-02-01 14:46:25] development.INFO: 2. payment-methods  
[2016-02-01 14:46:25] development.INFO: 2. sections  
[2016-02-01 14:46:25] development.INFO: UNSET  
[2016-02-01 14:46:25] development.INFO: 1. sections.11.name-short  
[2016-02-01 14:46:25] development.INFO: 2. payment-methods  
[2016-02-01 14:46:25] development.INFO: 2. sections  
[2016-02-01 14:46:25] development.INFO: UNSET  
[2016-02-01 14:46:25] development.INFO: 1. sections.12.name  
[2016-02-01 14:46:25] development.INFO: 2. payment-methods  
[2016-02-01 14:46:25] development.INFO: 2. sections  
[2016-02-01 14:46:25] development.INFO: UNSET  
[2016-02-01 14:46:25] development.INFO: 1. sections.12.name-short  
[2016-02-01 14:46:25] development.INFO: 2. payment-methods  
[2016-02-01 14:46:25] development.INFO: 2. sections  
[2016-02-01 14:46:25] development.INFO: UNSET  
[2016-02-01 14:46:25] development.INFO: 1. sections.2.name  
[2016-02-01 14:46:25] development.INFO: 2. payment-methods  
[2016-02-01 14:46:25] development.INFO: 2. sections  
[2016-02-01 14:46:25] development.INFO: UNSET  
[2016-02-01 14:46:25] development.INFO: 1. sections.2.name-short  
[2016-02-01 14:46:25] development.INFO: 2. payment-methods  
[2016-02-01 14:46:25] development.INFO: 2. sections  
[2016-02-01 14:46:25] development.INFO: UNSET  
[2016-02-01 14:46:25] development.INFO: 1. sections.3.name  
[2016-02-01 14:46:25] development.INFO: 2. payment-methods  
[2016-02-01 14:46:25] development.INFO: 2. sections  
[2016-02-01 14:46:25] development.INFO: UNSET  
[2016-02-01 14:46:25] development.INFO: 1. sections.3.name-short  
[2016-02-01 14:46:25] development.INFO: 2. payment-methods  
[2016-02-01 14:46:25] development.INFO: 2. sections  
[2016-02-01 14:46:25] development.INFO: UNSET  
[2016-02-01 14:46:25] development.INFO: 1. sections.4.name  
[2016-02-01 14:46:25] development.INFO: 2. payment-methods  
[2016-02-01 14:46:25] development.INFO: 2. sections  
[2016-02-01 14:46:25] development.INFO: UNSET  
[2016-02-01 14:46:25] development.INFO: 1. sections.4.name-short  
[2016-02-01 14:46:25] development.INFO: 2. payment-methods  
[2016-02-01 14:46:25] development.INFO: 2. sections  
[2016-02-01 14:46:25] development.INFO: UNSET  
[2016-02-01 14:46:25] development.INFO: 1. sections.5.name  
[2016-02-01 14:46:25] development.INFO: 2. payment-methods  
[2016-02-01 14:46:25] development.INFO: 2. sections  
[2016-02-01 14:46:25] development.INFO: UNSET  
[2016-02-01 14:46:25] development.INFO: 1. sections.5.name-short  
[2016-02-01 14:46:25] development.INFO: 2. payment-methods  
[2016-02-01 14:46:25] development.INFO: 2. sections  
[2016-02-01 14:46:25] development.INFO: UNSET  
[2016-02-01 14:46:25] development.INFO: 1. sections.6.name  
[2016-02-01 14:46:25] development.INFO: 2. payment-methods  
[2016-02-01 14:46:25] development.INFO: 2. sections  
[2016-02-01 14:46:25] development.INFO: UNSET  
[2016-02-01 14:46:25] development.INFO: 1. sections.6.name-short  
[2016-02-01 14:46:25] development.INFO: 2. payment-methods  
[2016-02-01 14:46:25] development.INFO: 2. sections  
[2016-02-01 14:46:25] development.INFO: UNSET  
[2016-02-01 14:46:25] development.INFO: 1. sections.7.name  
[2016-02-01 14:46:25] development.INFO: 2. payment-methods  
[2016-02-01 14:46:25] development.INFO: 2. sections  
[2016-02-01 14:46:25] development.INFO: UNSET  
[2016-02-01 14:46:25] development.INFO: 1. sections.7.name-short  
[2016-02-01 14:46:25] development.INFO: 2. payment-methods  
[2016-02-01 14:46:25] development.INFO: 2. sections  
[2016-02-01 14:46:25] development.INFO: UNSET  
[2016-02-01 14:46:25] development.INFO: 1. sections.8.name  
[2016-02-01 14:46:25] development.INFO: 2. payment-methods  
[2016-02-01 14:46:25] development.INFO: 2. sections  
[2016-02-01 14:46:25] development.INFO: UNSET  
[2016-02-01 14:46:25] development.INFO: 1. sections.8.name-short  
[2016-02-01 14:46:25] development.INFO: 2. payment-methods  
[2016-02-01 14:46:25] development.INFO: 2. sections  
[2016-02-01 14:46:25] development.INFO: UNSET  
[2016-02-01 14:46:25] development.INFO: 1. sections.9.name  
[2016-02-01 14:46:25] development.INFO: 2. payment-methods  
[2016-02-01 14:46:25] development.INFO: 2. sections  
[2016-02-01 14:46:25] development.INFO: UNSET  
[2016-02-01 14:46:25] development.INFO: 1. sections.9.name-short  
[2016-02-01 14:46:25] development.INFO: 2. payment-methods  
[2016-02-01 14:46:25] development.INFO: 2. sections  
[2016-02-01 14:46:25] development.INFO: UNSET  
marvinschroeder commented 8 years ago

Maybe i need to add, that the sections is removed when we add another string to a view and the package adds the new string to the lang file. When nothing changes, whether in lang file or view or controller, the lang file is not changed and the sections key is kept.

potsky commented 8 years ago

Ok bug found!

potsky commented 8 years ago

Hi Marvin,

I have published a beta version with fixes for issues 21 and 22. Can you try the dev-master target in composer and tell me if it is ok for you?

marvinschroeder commented 8 years ago

Sorry, but that dev-master branch requires laravel 5.2.* - we are developing on 5.1.*

potsky commented 8 years ago

Ok, I have publish for 5.1 with dev-master#8e7d28b492eb1f99b22ca3019e38cc8e131489b9

marvinschroeder commented 8 years ago

Looking good! Keys were protected!

potsky commented 8 years ago

Keys were protected, nice !

And obsolete child keys are now injected in the LLH:obsolete array, correct?

marvinschroeder commented 8 years ago

Correct!

potsky commented 8 years ago

Ok, so I publish 2.x.2 right now!

Thank you for reporting and for your time!

marvinschroeder commented 8 years ago

No problem! We just opened another request/issue ;)