#54858 closed defect (bug) (invalid)
i18n: Missing locale data if part of the translation are empty
Reported by: | arthur791004 | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | |
Component: | I18N | Keywords: | dev-feedback |
Focuses: | Cc: |
Description
As https://github.com/WordPress/gutenberg/pull/37686 and https://github.com/WordPress/gutenberg/pull/37704 mentioned, print_translations
function will register an empty locale data object to ensure the domain still exists even if there is no translation for the specific domain. However, if we call wp.i18n.setLocaleData
with the empty locale data, the lang
property in the domain configuration will be overridden and gone. See https://github.com/WordPress/WordPress/blob/48aa21e9d9894696de4102783f17e81441c8e134/wp-includes/class.wp-scripts.php#L610.
I propose a change to add a new method wp.i18n.addLocaleData
and the domain configuration will be merged rather than overridden so that the lang
property can be kept even if we call that function with an empty locale.
Thus, we should also update the print_translations
function to use wp.i18n.addLocaleData
.
If we have any concerns about the breaking change, I'm not sure is there a better way to use wp.i18n.addLocaleData
.
Change History (8)
#2
@
3 years ago
Oh, I'm sorry for mistyping. It should be language
property in our use case. See https://github.com/Automattic/wp-calypso/blob/94fe59f75d919c439f182764eed7d86f1f63da48/packages/i18n-utils/src/locale-context.tsx#L41. The language property comes from the translation json file and we will try to get the language property from the domain configuration but it might be undefined
. But what I want to say is that the property in the domain configuration might be overridden and become undefined
if we use setLocaleData
function.
#3
@
3 years ago
Just so I understand thid correctly, somewhere in your application you are setting the language property, _before WP loads the translations_.
When WP loads the translations but can‘t find any, it overrides the locale data with an empty object, causing your language property to be lost.
Is that correct?
If so, is there a way for you do set this property later on instead?
I hesitate to change the default call in core because it seems like a potentially breaking change.
Alternatively, what if core would always set some properties like language instead of using an empty object?
#4
@
3 years ago
Just so I understand thid correctly, somewhere in your application you are setting the language property, _before WP loads the translations.
When WP loads the translations but can‘t find any, it overrides the locale data with an empty object, causing your language property to be lost.
Is that correct?
No, the language property comes from translations. Here is the example of our translation file:
- {"":{"plural_forms":"nplurals=2; plural=n != 1;","language":"ta_IN","localeSlug":"ta"}, ... }
So, I think we cannot set the language property later. But I can ask whether our application is able to generate translation files all the time to avoid falling back to an empty locale data.
Alternatively, what if core would always set some properties like language instead of using an empty object?
This sounds good! But I'm not sure what property can be used as the developers like us have already set some kind of property.
#5
@
3 years ago
@swissspidy Do you have any further ideas for this?
Alternatively, what if core would always set some properties like language instead of using an empty object?
This sounds good! But I'm not sure what property can be used as the developers (like us) have already set some kind of property.
#6
@
3 years ago
- Keywords dev-feedback added
I'd love to hear thoughts from other core devs / component maintainers on this. Again, because of potentially breaking changes and backward compatibility concerns.
Still struggling to understand why/how this is causing issues for you.
You are loading translations / setting locale data in some custom way and WordPress sets an empty object because it doesn't have these translations?
Do you have any plugin / code repo so we can reproduce this issue?
Where is that
lang
property coming from?