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

[Question] Using dynamic fields #14

Closed alariva closed 8 years ago

alariva commented 8 years ago

I'm using the following technique to load dynamic translations like so:

trans('appointments.status.'.$appointment->statusLabel)

return array (
  //==================================== Translations ====================================//
  'text' => 
  array (
    'minutes' => 'minutes',
    'to' => 'to',
    'duration' => 'duration',
  ),
  //================================== Obsolete strings ==================================//
  'alert' => 
  array (
    'annulated' => 'Advice! Reservation was ANNULATED',
  ),
  'status' => 
  array (
    'annulated' => 'Annulated',
    'confirmed' => 'Confirmed',
    'reserved' => 'Reserved',
    'served' => 'Served',
  ),
);

I've seen I can avoid them getting into Obsolete by naming the key like trans('appointments.status.dynamic.'.$appointment->statusLabel) for which I'd use a translation like so:

return array (
  //==================================== Translations ====================================//
  'text' => 
  array (
    'minutes' => 'minutes',
    'to' => 'to',
    'duration' => 'duration',
  ),
  //================================== Obsolete strings ==================================//
  'alert' => 
  array (
    'annulated' => 'Advice! Reservation was ANNULATED',
  ),
  'status' => array ( 'dynamic' =>
    array (
      'annulated' => 'Annulated',
      'confirmed' => 'Confirmed',
      'reserved' => 'Reserved',
      'served' => 'Served',
    ),
  ),
);

But after running php artisan localization:missing the set does not appear at all, either in Obsolete or Translated sections.

Am I missing anything about how to use the command? Have you got any suggestions?

Thank you in advance!

You can find the project here to see other cases where I'm using this approach.

potsky commented 8 years ago

Hi!

I often use dynamic translations too and unfortunately, the only way I have found to handle this case is to put a trans with all possible dynamic values in comments before the line where I use them.

/*
trans('message.field.one')
trans('message.field.two')
*/
echo trans('message.field.' . $var );

There is no other workaround with my plugin at this time...

alariva commented 8 years ago

Seems like a good workaround, though. Thank you!

potsky commented 8 years ago

I just have read my code and fortunately there is something about obsolete lemma !

You need to define a configuration parameter named never_obsolete_keys. It is an array of words. All lemma (the id you put in the trans function) containing one of these words will never be considered as obsolete.

Unfortunately the plugin cannot guess what will be the different values of your dynamic php variable... So the plugin cannot create the translations in the Lang files.

alariva commented 8 years ago

Yeah, I've seen that, but I was willing to actually have this separation or real obsolete (as to clean them up) and the dynamic fields. Maybe I can get this done by two runs with different parameters, but would take some manual task and human-error risk.