Skip to content

Incorrect Locale Handling During Model Creation #432

@yorgobf

Description

@yorgobf

Describe the bug

While using the Astrotomic\Translatable package, I encountered an issue where the model's translatable fields are being created in the wrong locale, despite explicitly setting the locale via app()->setLocale().

To Reproduce

  • Set the Accept-Language header in the request to "en".
  • In the application logic, set the locale to English using: app()->setLocale('fr');
  • Create a translatable model like this:
    $skill = Skill::create(array_merge($skillData, [
    'is_original' => 1,
    'original_language' => app()->getLocale(),
    ]));

Expected behavior

  • The model's translatable attributes should be saved in all languages defined in the config file same as the second screenshot.
  • The original_language field in the translation table should correctly reflect 'fr' as true, since the locale was set with app()->setLocale('fr') before the model creation.

Actual Behavior

  • A translation is created in English (en) instead of French, despite the locale being set to 'fr'.
  • Additionally, sometimes only one language is being created other times both French and English translations are being created, with Englsih (en) set as the original language, even though it should be French (fr).

Troubleshooting Attempts

  • Manually setting the default locale using setDefaultLocale() did not resolve the issue.
  • Attempted to explicitly set the translation using translateOrNew(app()->getLocale()), but this is not scalable for my case and will be redundant since my app supports 5 languages for now

Screenshots
with header having Accept-Language 'en' and before creating the model setting the app()->setLocale('fr')
Screenshot 2024-09-17 at 8 08 36 PM
without changing the locale in the app using app()->setLocale():
Screenshot 2024-09-17 at 8 09 16 PM

Versions (please complete the following information)

  • PHP: 8.1
  • Database: postgres
  • Laravel: 10.10
  • Package: 11.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions