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

Incorrect Locale Handling During Model Creation #432

Open
yorgobf opened this issue Sep 17, 2024 · 0 comments
Open

Incorrect Locale Handling During Model Creation #432

yorgobf opened this issue Sep 17, 2024 · 0 comments
Labels

Comments

@yorgobf
Copy link

yorgobf commented Sep 17, 2024

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
@yorgobf yorgobf added the bug label Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

1 participant