Make WordPress Core

Opened 2 years ago

Closed 19 months ago

Last modified 18 months ago

#54858 closed defect (bug) (invalid)

i18n: Missing locale data if part of the translation are empty

Reported by: arthur791004's profile 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)

#1 @swissspidy
2 years ago

the lang property in the domain configuration will be overridden and gone

Where is that lang property coming from?

#2 @arthur791004
2 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.

Last edited 2 years ago by arthur791004 (previous) (diff)

#3 @swissspidy
2 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 @arthur791004
2 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 @arthur791004
2 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 @swissspidy
2 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?

#7 @arthur791004
19 months ago

  • Resolution set to maybelater
  • Status changed from new to closed

I think we could close this one as we never receive any reports from our customers. We could re-open this once we get any reports in the future. Thanks for your feedback 🙂

#8 @desrosj
18 months ago

  • Milestone Awaiting Review deleted
  • Resolution changed from maybelater to invalid
Note: See TracTickets for help on using tickets.