Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Even if the product variation is translated, the "add to cart" is not translated in the product variation label.
Here is code that i added, please create patch for this.
Drupal\commerce_cart\Form\AddToCartForm;
function public function submitForm ....
after $purchased_entity = $order_item->getPurchasedEntity();
$language = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_INTERFACE)->getId();
if($purchased_entity->hasTranslation($language)){
$purchased_entity = $purchased_entity->getTranslation($language);
}
Comment | File | Size | Author |
---|---|---|---|
#96 | Relationship.jpg | 43.12 KB | joro78 |
#96 | Filter.jpg | 67.96 KB | joro78 |
#81 | 2860840-81.patch | 23.96 KB | mglaman |
#80 | Screen Shot 2017-12-11 at 6.43.44 PM.png | 59.9 KB | sorabh.v6 |
#77 | 2860840-77.patch | 19.65 KB | mglaman |
Comments
Comment #2
mglamanWe also need a test for this. We have no i18n testing
Comment #3
mgoncalves CreditAttribution: mgoncalves at CI&T commentedComment #4
mgoncalves CreditAttribution: mgoncalves at CI&T commentedAdded the suggested code.
Comment #5
mgoncalves CreditAttribution: mgoncalves at CI&T commentedComment #6
mglamanWe'll want to properly inject this. And make a PR on GitHub so we can run tests, at least.
We could run i18n test as a follow up, since there's no existing structure for that.
Comment #7
CulacovPavel CreditAttribution: CulacovPavel commentedThanks :)
Comment #8
vasikeThere is a new PR about translations (multilingual) on Add to cart form
https://github.com/drupalcommerce/commerce/pull/682
What's there
- new CommerceContentEntityBase to be used to get translations of the referenced entities
- Update product and product variations to use this
- Update also the Order item to use this for purchased entities
- Update product variation to get translated values for the Attributes
- Make sure the Ajax loads the translated product variation.
- Add language to addToCartForm code available in a previous PR
https://github.com/drupalcommerce/commerce/pull/681
@todo : tests & tests of course
Comment #9
vasikePR updated with tests AddToCartMultilingualTest - 2 Tests available for the 2 widgets available:
- testProductVariationAttributesWidget
- testProductVariationTitleWidget
PR green now.
Comment #10
andypostClosed as duplicate
Comment #11
vasikePR "reroll"
Comment #12
0Sarah0Al CreditAttribution: 0Sarah0Al commentedHi
I am having a problem translating product variations to the Arabic language.
Please see attached picture.
I create the product in English language then hit the translate button. Everything is translatable except some parts of the 'variations' section.
For example, 'Title', 'Active', 'Published'
Thanks for your help.
Comment #13
ThirstySix CreditAttribution: ThirstySix commentedI added this patch in the AddToCartForm.php. Commerce 8.x-2.0-rc1
But, It's not working.
I tried in same 8.x-2.x-dev. but, no luck.
Thanks in Advance
Comment #14
vasikePR update - fix merge conflict
Comment #15
mglamanAttaching vasike's latest PR as a patch (don't trust revolving PR patches anymore :))
Comment #16
mglamanThat patch did not apply. I merged 8.x-2.x into vasike's branch on https://github.com/drupalcommerce/commerce/pull/682
This patch applied.
Comment #17
mglamanWe have heavily customized add to cart forms. This solves most normal use cases but does not handle
\Drupal\commerce_product\ProductVariationStorage::loadEnabled
The way I fixed it was to add the following after executing the entity query, and before running the filter event.
Comment #18
mglamanPatch with fix added. However needs tests.
Comment #19
mglamanFound more
Does not support langcode.
Comment #20
mglamanAdding related issue.
Comment #21
mglamanThis patch caused bug reported in #2915548: Attribute selection widget includes invalid variations when rendering form element.. Missed it in the diffs. New patch.
Comment #22
0Sarah0Al CreditAttribution: 0Sarah0Al commentedShould I go ahead and apply #21 or should I wait for more fixes?
I have been waiting for this patch for a long time.. :)
My website depends on it. I can not deploy without this patch..
Thanks
Comment #23
mglamanWith patch #21 I'm seeing success on a customer site which we've hooked into Lingotek. However it is a custom add to cart form. But we also customized the cart page which re-uses the attributes widget. We're seeing good things end to end.
I still plan on adding the patch to a more typical 2.x install and reviewing.
But for all intents and purposes.. I think #21 can be considered "final draft" waiting for better review and more tests.
Comment #24
0Sarah0Al CreditAttribution: 0Sarah0Al commentedConfirming.
Patch #21 works ... Yay..
I am so happy..
Thanks Matt
Comment #25
CulacovPavel CreditAttribution: CulacovPavel commentedPatch work.
Comment #26
CulacovPavel CreditAttribution: CulacovPavel commentedPatch, work, but i must resave all product variant :( because add to card form is not show
Comment #27
bojanz CreditAttribution: bojanz at Centarro commentedWe'll need to review every bit of code that loads an entity, and then see if a call to ->getTranslationFromContext() is needed. Core does it all over the place.
Aside from the fact that the middle condition is redundant, this is not how we should be loading translations.
We need $product = $this->entityRepository->getTranslationFromContext($product, $langcode);
That's what core uses everywhere, and what we should be using outside of entity classes. It does the usual thing, but also ensures language fallback.
The core logic is to fallback to a different language when a translation is missing. Seems odd to introduce new behavior here ("Hide untranslated entities").
We could introduce a getTranslatedReferencedEntity() I guess?
It's a nitpick, but we should order these namespaces properly (alphabetically).
Typo in variable name.
Needs a single line description.
Comment #28
ptmkenny CreditAttribution: ptmkenny commentedfixing spelling mistakes to make this issue more searchable
Comment #29
ptmkenny CreditAttribution: ptmkenny commentedI applied the patch in #21. The patch successfully causes the label to be translated, but the values (in the select dropdown on the Add to Cart form) still do not appear to be translated.
Potential factor: My site has a default language of Japanese, and English is a translated language. (I have had problems in the past when code assumes English is the default language.)
Comment #30
mglamanI have seen this problem, too. Where auto-generated titles are not re-generated for the language.
Comment #31
mglamanLatest patch. Adds some kernel testing to variation storage, which is where most of this all stems from. Pushed all to PR as well.
Comment #32
mglamanNew patch. Kernel test is fixed. However the functional JavaScript test is broken
Comment #34
mglamanNew patch attempt. Adds test coverage for variation title generation (with multilingual, too.)
Comment #36
mglamanFound out why the FunctionalJavascript tests may be failing. Even though the site's default language is set to FR, toUrl still uses the entities current language
Comment #37
mglamanThis should fix the tests now that toUrl is being used properly :)
Comment #39
mglamanGenerating a translation before the product back reference is generated causes the translated label to go out of sync, when auto-generated. When in the UI this won't happen as a product and its variations will be created, then translated. This only occurs when using
commerce_product_variation_title
. OtherwisegetOrderItemTitle
always returns a generated title if there is not one set. Explaining why the first test always passed but second one failed (empty labels in select.)Comment #40
mglamanUpdated version. Removes some changes to a checkout test. Also use
getTranslatedReferencedEntity
when appropriate (order, order item)Comment #42
mglamanComment #47
bojanz CreditAttribution: bojanz at Centarro commentedChanging the variation selection logic in a multilingual patch is making me super nervous. Looks like a bundled unrelated fix, on an already huge patch.
This is a big problem. We can't inject the entity repository into a storage. And we can't have some storage methods return translated entities (such as loadBySku()), while others return non-translated entities (loadMultiple(), etc).
Selecting a translation must live on a layer above the storage.
In both Product and ProductVariation we are manually doing getTranslation().
This is bad, we are not only duplicating logic with getTranslatedReferencedEntity(), but it's also not consistent, the helper methods do hasTranslation() and then getTranslation(), but here we only do getTranslation(). So in one case we get the original entity when no translation is found, in the other we get a newly created translation without any translated data.
Luckily we have $this->ensureTranslations(), and we can introduce $this->ensureTranslation() as well.
These two methods do the same thing, but have different descriptions.
I suggest this:
Comment #48
mglamanI'm going to try and dissect this patch. For instance, I added two areas of test coverage which did not exist so that I could test the multilingual version of it. Those should be spun off in new issues.
Comment #49
mglamanHere is an updated patch built off of a few split-off issues. This is much more focused on the add to cart aspect. I'd like to review the variation storage issue outside of this one, however.
Comment #51
mglamanNo idea what I did with the last patch. Apparently ".patch" is that much difference than ".diff".
Comment #52
pyxio CreditAttribution: pyxio commented#51 did not work for me ... i get a diff complaint in cli
Comment #53
durum CreditAttribution: durum commentedExcuse me if this very obvious to you or if it is a newbie stuff but I think it may be very relevant to note it under this issue. And I hope I'm not hijacking.
Right now I can translate a paragraphs field attached to a product variation by checking the "Users may translate this field" option illegally. (Illegally, because they discourage to do so under field settings, see the attached image).
This was possible before the patch in #40 and after that it is still working very nicely.
Nowadays things are getting done with paragraphs module and I can't think of a product variation page without paragraphs. Since paragraphs module has its own way for translation settings and its field type is not entity reference but entity reference revisions, I thought it would be good to consider it while fixing this issue.
Comment #54
mglamanIt requires -dev changes.
Comment #55
mglamanThis should be its own issue.
This should be its own issue.
Comment #56
mglamanPer #2923158: Test that the variation storage works in a multilingual context we will not be adjusting the storage and need to ensure translations in the layer above, our add to cart forms and such.
Comment #57
mglamanHere is the latest patch against 2.x HEAD. Injected field translation is now fixed.
Comment #59
mglamanFixup usages to ensure entity repository is used.
Comment #61
mglamanNew patch which adds helpers than should ensure variations are in proper translations, which means attributes will be.
Comment #62
mglamanWOO! We are green. 27kb smaller patch thanks to child issues which provide better test coverage. Assigning to bojanz for review so we can commit this sucker.
Comment #63
ptmkenny CreditAttribution: ptmkenny commentedI tested the patch in #61 but I'm still having the same problem I described in #29:
I cleared the cache multiple times, went back and editing the product variation translation, and added a new product variation translation to test whether it was just the old strings not being refreshed. Although the translation shows up correctly in the admin UI (/admin/commerce/product-attributes/manage/MYTYPE/translate/en/edit), it still shows up in Japanese (default language) instead of English (UI language, and everything else is in English on the page).
Comment #64
mglamanCan you please specify what values in the dropdowns are not translated: Attributes or Variation Titles. We have two widgets. There is test coverage in this patch which asserts translated attribute titles.
Perhaps there are some flaws in our test through language negotiation. The tests change the site's default language. I guess this is an inappropriate approach.
Comment #65
ptmkenny CreditAttribution: ptmkenny commented@mglaman: I think they are attributes. The specific text from the UI that is not showing up as translated (even though it has already been translated in the UI) is the text at /admin/commerce/product-attributes/manage/MY_TYPE in the "Name" field under the "Value" collection that appears when you check the box "Enable attribute translation."
Comment #66
mglamanYes, those are attributes.
Okay, I need to review our test coverage.
Comment #67
ptmkenny CreditAttribution: ptmkenny commentedIf it helps, this is some of my configuration for the form:
commerce_product.commerce_proudct_attribute.TYPE.yml
core.entity_form_display.commerce_order_item.TYPE.add_to_cart.yml
Comment #68
mglamanYour site has Japanese as the primary language and English second, correct? Today might also be vital to proper testing
Comment #69
0Sarah0Al CreditAttribution: 0Sarah0Al commentedFor my case, I don't see any change after the update!!
I have to go back and make sure commerce is updated properly,
I will let you know of my findings ,,
Comment #70
drugan CreditAttribution: drugan as a volunteer commentedJust as a note. I've built a couple of multilingual commerce sites on Drupal 7.x and I remember from my experience that despite Drupal system allows to create a site with other than English primary language I'd not recommend anyone to do this if they don't want language issues appearing there and here.
As for me the best way is to translate a site to your own language from English.
Comment #71
ptmkenny CreditAttribution: ptmkenny commented@mglaman Yes, Japanese is the primary language and English is the second language.
@drugan I had the same experience in Drupal 7, but in Drupal 8, non-English language install is supported, and generally speaking I've had a lot less trouble using languages other than English as the default language.
Comment #72
CulacovPavel CreditAttribution: CulacovPavel commentedHello witch patch must apply ?
I update drupal to 4.2 and commerce 2.1
Before update use patch #21, variation work label translation work, before apply patch #61 not working nothing.
Comment #73
CulacovPavel CreditAttribution: CulacovPavel commentedSorry for comment, after read all messages and the child issues i understand that you are separate into multiple patch.
But if apply patch #61, nothing change. Main Language Romanian second Russian.
It change before if I resave all product variation in second language, after this work.
Comment #74
mglamanI'm going to merge #61.
I am going to open the following follow-ups:
Comment #75
mglamanSeeing how latest merge with 8.x-2.x turns out.
https://travis-ci.org/drupalcommerce/commerce/builds/310260922
Comment #77
mglamanI guess generating patches from GitHub just explodes, now.
Comment #79
ptmkenny CreditAttribution: ptmkenny commented@mglaman After four hours of testing today, I accidentally found the cause of the bad behavior in #29 (values in the select dropdown on the Add to Cart form do not display as translated even though they have been translated in the UI).
The problem occurs if you have "Translation enabled" on the Product Variation Type page:
/admin/commerce/config/product-variation-types/default/edit
I mistakenly thought this was necessary to enable translation of product variations, but it is not-- this is just the translation for the product variation type itself, and if this is turned on, the add to cart form translation doesn't work.
The "fix" for me was as simple as turning this off.
Comment #80
sorabh.v6@mglaman I applied patch and added product to cart. But the message is not translated. Please refer the attached image.
Thanks
Comment #81
mglamanMy last patch forgot this method to ensure variations loaded from storage were translated.
An interesting follow up needs to be ptmkenny's comment in #79
Comment #82
mglamanTravis passed. DrupalCI passed. Adjusting credits for those who did manual review
Comment #84
mglamanThank you, everyone! This has landed. Expect it in the 2.3 release slated for Jan 3rd.
Comment #86
pyxio CreditAttribution: pyxio commentedi am using commerce 8.x-2.6 and this is still an issue.
Comment #87
willeaton CreditAttribution: willeaton commentedHi all, +1 for getting this released :-)
Comment #88
willeaton CreditAttribution: willeaton commentedUpdate, it is included, I can see the patch code in my files already, but... the cart still shows the English name of the product. Can anyone tell me what I am doing wrong?
Comment #89
oakulm CreditAttribution: oakulm as a volunteer and at Drontti Oy commentedYes I can confirm this is still an issue and a big one...
Comment #90
bojanz CreditAttribution: bojanz at Centarro commented@oakulm
Responding to an issue closed 17 months ago is a good way to never receive help.
The fix for this bug was shipped in Commerce 2.3, 10 releases ago.
Unless you're running an even older version than that, your problem is a different one and requires its own issue.
Comment #91
oakulm CreditAttribution: oakulm as a volunteer and at Drontti Oy commentedThank you for the reply especially as this is quite old issue... Status is set to closed but there are two comments before me stating the same problem is still there... And I'm running the lates versio of Commerce 2.. I probably have to create a new issue than :)
Comment #92
iamfredrik CreditAttribution: iamfredrik commentedI can confirm that this problem still exists in Commerce 2.17.
Comment #93
Annelies Van der Wee CreditAttribution: Annelies Van der Wee as a volunteer commentedComment #94
Annelies Van der Wee CreditAttribution: Annelies Van der Wee as a volunteer commentedsame here...
Comment #95
iamfredrik CreditAttribution: iamfredrik commentedIn case anyone still finds this thread, the procedure to make variation translations work can be found here: https://www.drupal.org/project/commerce/issues/3247257#comment-14282039
Comment #96
joro78 CreditAttribution: joro78 commentedProblems may occur if you have a product variation with an attributes. In that case you have to translate the product name first, and then add the product variation(s), and translate them. Otherwise the variation name won't display as translated (just the attributes), no matter that they appear so in the store. Translation options for the product type and variations have to be enabled. In Cart form (Order) view you have to filter the product variation based on the user Interface text language selected for page. Thus you have to have a relationship for the product variation too.
#95 is not a solution, besides that will erase all translations so far.
Comment #97
mazze CreditAttribution: mazze commentedAlso make sure that the default language has the language prefix defined. I fiddled around for hours because having a structure like
Comment #98
aren3k CreditAttribution: aren3k commentedstill an issue for 2.28