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

[5.x] Fix collection title_format when using translations #11248

Merged
merged 1 commit into from
Dec 12, 2024

Conversation

ajnsn
Copy link
Contributor

@ajnsn ajnsn commented Dec 12, 2024

We are using a custom title_format in an entry collection that relies on translations. Unfortunately, when saving an entry, Statamic appears to set app()->locale() to the full site locale (e.g., de_DE), while Laravel expects the "short" locale or language (e.g., de). As a result, the translation is not found, and the fallback translation is used instead.

I traced this issue to the following line. Is there a specific reason for using $this->withLocale($this->site()->locale(), ...) instead of $this->withLocale($site->lang(), ...)? Using $site->lang() resolves the issue for us.

Could this be adjusted, or do you have any other suggestions for solving this issue?

I noticed that all other occurrences of $this->withLocale already use $site->lang().

Thanks!

@jasonvarga
Copy link
Member

jasonvarga commented Dec 12, 2024

Is there a specific reason for using locale() ?

It honestly just looks like an oversight. Looking back at the PR where this was introduced - the person who reported the issue was using the de_CH language which actually needed de_CH, not de. So it worked for him, and for most other people it probably just continued to fall back to the default or they didn't notice it.

Nice find, thanks!

@jasonvarga jasonvarga merged commit 684eb68 into statamic:5.x Dec 12, 2024
20 checks passed
@ajnsn
Copy link
Contributor Author

ajnsn commented Dec 12, 2024

Thanks, @jasonvarga !

This has turned into a bit of a chicken-and-egg situation. The date issue mentioned in the other PR will likely resolve the correct locale, particularly in cases where date or number formats differ within the same language. For example, German (Germany) and German (Switzerland) share the same date and time formats but use different decimal and thousand separators. There might be other (edge) cases.

To test, I created a lang/de_DE.json file and added a dummy translation. This works without this PR. However, I believe most Laravel users are more likely to use de.json or a similar key-based translation file in a two-letter language folder. Additionally, Laravel itself defaults to two-letter locales.

What do you think?

@ajnsn ajnsn deleted the fix-title-format-lang branch December 12, 2024 16:20
@jasonvarga
Copy link
Member

jasonvarga commented Dec 12, 2024

People using de_CH as their locale would/should also be explicitly setting lang to de_CH. They would have run into similar issues in all the other places where withLocale($site->lang() is used. It's fine. 👌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants