qtranslate / qtranslate-xt

qTranslate-XT (eXTended) - reviving qTranslate-X multilingual plugin for WordPress. A new community-driven plugin soon. Built-in modules for WooCommerce, ACF, slugs and others.
GNU General Public License v2.0
553 stars 104 forks source link

WooCommerce module relies on 'Filter all WordPress options for translation at front-end' #961

Open BenceSzalai opened 3 years ago

BenceSzalai commented 3 years ago

Split from #949.

Apparently some translations are not working, when 'Filter all WordPress options for translation at front-end' is disabled under Advanced. Since the explanatory text on the Advanced tab says “...this may cause a considerable performance degradation...” it is to be expected that users may turn off this option, so modules should be prepared to continue working even in that case.

These "missing" translations can be fixed by either applying additional filters or by including additional options to be translated. So generally speaking Modules should be able to detect if 'Filter all WordPress options for translation at front-end' is disabled, and load additional filters and include additional options to be translated. (Should probably be another issue, but this may be already possible with some PHP if-s and so.)

For the WooCommerce module one of the option wildcards required is: woocommerce_%_settings - responsible for the translation of text related to shipping methods. While these are translated fine when they appear in the frontend, these text are also included in the default email templates and there they appear as raw, untranslated text. Also I couldn't find any usable filters for these, so the only way to get this working is to include these options in ranslation. Each shipping method is stored in the options table as woocommerce_{shipping_class_slug}_{shipping_method_id}_settings, e.g.: woocommerce_flat_rate_1_settings or woocommerce_free_shipping_3_settings etc. as json, which include title and description.

Some filters that are also required to make sure the emails are sent fully translated are:

 {
  ...
  "front-config": {
    "all-pages": {
      "filters": {
        "text": {
          "woocommerce_email_heading_cancelled_order": "20",
          "woocommerce_email_heading_customer_completed_order": "20",
          "woocommerce_email_heading_customer_invoice": "20",
          "woocommerce_email_heading_customer_note": "20",
          "woocommerce_email_heading_customer_on_hold_order": "20",
          "woocommerce_email_heading_customer_processing_order": "20",
          "woocommerce_email_heading_customer_refunded_order": "20",
          "woocommerce_email_heading_customer_reset_password": "20",
          "woocommerce_email_heading_failed_order": "20",
          "woocommerce_email_heading_new_order": "20",

          "woocommerce_email_subject_cancelled_order": "20",
          "woocommerce_email_subject_customer_completed_order": "20",
          "woocommerce_email_subject_customer_invoice": "20",
          "woocommerce_email_subject_customer_note": "20",
          "woocommerce_email_subject_customer_on_hold_order": "20",
          "woocommerce_email_subject_customer_processing_order": "20",
          "woocommerce_email_subject_customer_refunded_order": "20",
          "woocommerce_email_subject_customer_reset_password": "20",
          "woocommerce_email_subject_failed_order": "20",
          "woocommerce_email_subject_new_order": "20",

          "woocommerce_email_additional_content_cancelled_order": "20",
          "woocommerce_email_additional_content_customer_completed_order": "20",
          "woocommerce_email_additional_content_customer_invoice": "20",
          "woocommerce_email_additional_content_customer_note": "20",
          "woocommerce_email_additional_content_customer_on_hold_order": "20",
          "woocommerce_email_additional_content_customer_processing_order": "20",
          "woocommerce_email_additional_content_customer_refunded_order": "20",
          "woocommerce_email_additional_content_customer_reset_password": "20",
          "woocommerce_email_additional_content_failed_order": "20",
          "woocommerce_email_additional_content_new_order": "20",

          "woocommerce_gateway_title": "20"
        }
      }
    }
  }
}

Please note that woocommerce_gateway_title is included in the qwc-front.php config of the WC module, but is most likely not loaded when the emails are being sent. (To be checked.) My assumption is that $url_info['doing_front_end'] is false in qwc_init_language(), so the whole qwc-front.php may not be loaded, when WooCommerce is processing the submitted order and sending the emails. However adding the above filters to i18n-config.json works fine for the emails.

herrvigg commented 3 years ago

Taken from #949:

Thank you for looking at this. I must admit i never cared about that filter option mode, i've always used the default. The related code comes from the qtranxf_filter_options function:

https://github.com/qtranslate/qtranslate-xt/blob/e9d0748170acef17448238afaf7ebf0185e839a0/qtranslate_frontend.php#L412-L440

I have never looked at the performance of this. Perhaps it could be improved by refining a bit the WHERE query, or maybe building some index. I performance were really an issue it would be nice to find a solution that is maybe more generic.

BenceSzalai commented 3 years ago

To be honest I've not seen any performance issues. I've just read the explanation for the setting and thought to myself, it's best to turn this off, because the description itself suggested to do so.

Speculating that some plugins may store quite huge objects and arrays serialised in wp_options I can imagine applying translation to all of them may cause slowdowns.

If i18n-config.json would provide a way to define the options to be filtered, that would be a good step to move away from relying on the global options filtering, as then all integrations could define the options they need to get filtered.