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

Bring back i18n_string integration #67

Closed
laryn opened this issue Nov 2, 2021 · 10 comments · Fixed by #148
Closed

Bring back i18n_string integration #67

laryn opened this issue Nov 2, 2021 · 10 comments · Fixed by #148

Comments

@laryn
Copy link
Member

laryn commented Nov 2, 2021

I'm working to clean up Rules and the i18n_rules submodule extends EntityDefaultI18nStringController -- this doesn't exist in Backdrop. Maybe we should add entity.i18n.inc into Entity Plus from the Entity module for D7?

@laryn
Copy link
Member Author

laryn commented Feb 2, 2022

WIP here: #116

@argiepiano
Copy link
Collaborator

@laryn, as per your message in the Rules issue queue, I'll take over working on bringing this back to Entity Plus. I'll close your PR and will submit a new one soon.

Thanks!

@laryn
Copy link
Member Author

laryn commented Jan 5, 2023

Thank you!

@argiepiano
Copy link
Collaborator

PR #148 submitted and ready for review (based on #116 - thanks, @laryn!).

This issue has two "sister issues" that are needed for Rules translation (and tests) to work: #144 and #146

Testing this is tricky, as this is an API used mostly by Rules and maybe other contrib. I'll figure out some steps to do so, though.

@argiepiano
Copy link
Collaborator

argiepiano commented Jan 7, 2023

I've made some substantial changes in the approach:

Instead of bringing back this functionality the way it was in D7 (as an 'inc' to the main module), I've decided to create a new submodule called entity_plus_i18n

The reasoning behind this is that this functionality, which integrates with i18n_string, creates new "Translate" tabs for custom entities configuration UIs, and to do so, it works together with Entity UI. In D7, Entity UI was part of Entity API, and there was no issue. In Backdrop, now that Entity UI and Entity Plus have been split, bringing back this functionality is impossible without the presence of Entity UI. So, this new submodule depends on Entity UI.

Since all this functionality never existed in Backdrop's Entity Plus, this addition of a submodule should not present any backward compatibility problems. The only possible problem may be D7 upgrades that have custom entities that relied on this functionality. In those cases, the "Translate" tab will not show up until the submodule is enabled, but the translations should still work for strings that were translated in D7.

One more aspect that needs to be resolved before this is ready to be merged:

When using entity metadata wrappers, most entity properties (e.g. non-field-API fields that have their own columns in the entity table, such as a "Title" or "Created") use a callback named entity_plus_property_verbatim_get to get the value from the appropriate table's column. For custom entities that have "turned on" internationalization for its properties, when String Translation is enabled, and once the submodule entity_plus_i18n is also enabled, that callback will call a entity class method called getTranslation(). Unfortunately that method was removed in Backdrop's Entity, and therefore that part will produce a fatal error. This could happen in very rare occasions when (1) the custom entity uses B's core Entity class, and (2) when using entity metadata wrappers to get the value of a translatable property.

Still, while rare, to avoid this problem, we'll need to check whether the entity class contains method getTranslation() (it could happen that the contrib module extends Entity to add it), and if not, create a helper function that will do the same thing as the original D7 getTranslation() method.

I'll add this soon.

@argiepiano
Copy link
Collaborator

argiepiano commented Jan 7, 2023

What I have so far can be tested in the following way...

But first a quick explanation of what this functionality does: it creates the ability for users to manually translate text entered in custom entity properties. For example, if your custom entity has a property called "Title", you can enter any text there, and the Internationalization functionality will "pick it up" and allow you to translate it through the translation interface. Then when you visit the entity page in another language (example.com/es/my-entity/1), then you'll see the translated title (in this case, in Spanish).

In addition, this functionality adds a tab "Translate" in the Admin UI for the custom entity type.

So, to test:

  1. Be sure to patch Entity Plus with the linked PR
  2. Download and enable Basic Entity Plus Example, which provides an example custom entity that uses a lot of Entity Plus and Entity UI
  3. Enable all the following modules: Localize, Languages, Internationalization and String Translation
  4. Enable the new Entity Plus Internationalization Integration submodule (entity_plus_i18n)
  5. Enable a language at /admin/config/regional/language, for example Spanish
  6. Enable URL detection at /admin/config/regional/language/detection
  7. Visit the Default Entity configuration page at admin/structure/basic_entity_plus-types/manage/basic_entity_plus_default. This is a default entity type provided by the Example module
  8. Once there, you'll see a Translate tab. Clicking it will allow you to translate the two properties of the entity type (the label, in this case "Default entity", and the description property). Those values are also made available by the submodule in the translation interface /admin/config/regional/translate
  9. Create a new entity of type "Default entity" at /basic_entity_plus/add/basic_entity_plus_default. If you haven't added a field to the entity type, you'll only see one field, "Title". Enter a text in English and save
  10. Go to the translation interface > strings at admin/config/regional/translate/i18n_string and refresh the strings for Basic Entity Plus. This will make the title you entered above available for translation
  11. Go back to the translation interface /admin/config/regional/translate and search for the title you entered
  12. You will now see that text. Create a translation by clicking Edit
  13. Now go to the entity page in the other language (for example /es/basic_entity_plus/1 or the correct ID). The title of the page should now show in Spanish

@argiepiano
Copy link
Collaborator

argiepiano commented Jan 7, 2023

The latest commit modifies entity_plus_property_verbatim_get() to use an alternative method to translate custom entity properties. through metadata wrapper methods. Formerly, this used the entity class method getTranslation(), but this method was removed from core's Entity. So, property translation for custom entity properties that use Entity is now handled through entity_plus_i18n_string() (which was part of D7 Entity API).

To test:

  1. Follow ALL the steps in my previous comment (enabling Basic Entity Plus etc) up to step 12
  2. Try this on the PHP form from Devel
$w = entity_metadata_wrapper('basic_entity_plus', 1); // Use the correct ID
$w->language('es');
dpm($w->title->value());

You should see the translated title.

@bugfolder
Copy link

I'm going to let you guys (@argiepiano, @laryn) sort this one out. I don't think I can probe much beyond blindly following @argiepiano's recipe above.

@laryn laryn changed the title entity.i18n.inc Bring back i18n_string integration Jan 21, 2023
@laryn
Copy link
Member Author

laryn commented Jan 21, 2023

Looks and works great from my testing. My only comment is that we should also make i18n_string a dependency in the new module. 👍

@argiepiano
Copy link
Collaborator

Thanks, @laryn! Done.

argiepiano added a commit that referenced this issue Jan 23, 2023
Issue #67. Bring back i18n functionality
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants