• Resolved joenuzzz

    (@joenuzzz)


    I have an input field on the homepage where customer can enter their postcode. Through an AJAX call (which calls a PHP function) I check if their postcode is available for shipping. If so, they receive a message. If not, they receive another message.

    The language of my site is Dutch, but the response message from the AJAX call is in English.

    In the PHP function, I use the following code:
    $delivery_message = __( ‘There are no shipping options available. Please ensure that your address has been entered correctly, or contact us if you need any help.’, ‘woocommerce’ );

    The response I get is in English, but I want the Dutch translation of this message. How can I fix this?

Viewing 11 replies - 1 through 11 (of 11 total)
  • Hello @joenuzzz,

    You can translate text strings like that one so they use your language. There is a guide on how to do that at

    https://docs.woocommerce.com/document/woocommerce-localization/

    that you can walk you through how to do that. With Loco Translate you can filter for that text and then change it.

    If you have any questions or trouble, let us know.

    Thread Starter joenuzzz

    (@joenuzzz)

    Yes, I’m using the Loco Translate plugin and this works perfectly. But when I do an AJAX call and call an function where a Woocommerce string is present, it returns the default English string and not the Dutch translation which I changed in Loco Translate.

    So, in my example, the variable $delivery_message returns ‘There are no shipping options available…any help.’ instead of the Dutch translation ‘Helaas er zijn verzendmethoden beschikbaar’.

    In the function which I call through AJAX I use this, which in my opinion is the right way:

    __( ‘There are no shipping options available. Please ensure that your address has been entered correctly, or contact us if you need any help.’, ‘woocommerce’ );

    Hey @joenuzzz,

    Are you using this string in a custom function? If so do you have the translation for this in your theme or custom plugin? If that’s the case, I would change the text domain to match the custom plugin or theme. Then make a separate translation in Loco Translate.

    Thread Starter joenuzzz

    (@joenuzzz)

    The function that I call is a custom function which is in functions.php of my theme. The only thing I did in Loco Translate is creating a new Dutch language file and change the translation. I also tried it with my theme, but I always get the English text instead of the Dutch translation.

    I don’t know what I am doing wrong.

    So the new translation you created in Loco Translate was of your theme, correct? I don’t believe the translation of that string from inside WooCommerce’s translation will carry over to the theme.

    Since this is custom code in your theme, are you serving it in multiple languages? If not you could always just use the translated text instead of English in that function. Outside of serving the site in multiple languages, there isn’t a direct need to translate this string if it’s in custom code.

    Thread Starter joenuzzz

    (@joenuzzz)

    I tried it with a new translation of WooCommerce, this doesn’t work. Also tried a new translation of the theme, also not working.

    No the site is only Dutch, so yes I can put the string hardcoded in the function. But for other projects (which are multilingual) I need the same function, so I want this to work with the translate functions.

    If you want to make a copy of the function and post it at https://gist.github.com/ and then add the URL to that here, I can take a quick look and see if I can spot anything in the function that is keeping the translation from being picked up.

    Thread Starter joenuzzz

    (@joenuzzz)

    Here is my code, the function is in functions.php of my theme, the second part is a part of the Javascript file where I executed the function.

    https://gist.github.com/joenuzzz/b364afc3fab2587fb5de735fa9f4cc14

    Thank you for sending that over. One more question for you. Did you add the string to your theme’s .pot file?

    Thread Starter joenuzzz

    (@joenuzzz)

    Yes, here is a part of the .pot file op my theme

    #, fuzzy
    msgid “”
    msgstr “”
    “Project-Id-Version: De Barbecue Chef\n”
    “Report-Msgid-Bugs-To: \n”
    “POT-Creation-Date: 2019-03-27 14:16+0000\n”
    “PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n”
    “Last-Translator: FULL NAME <EMAIL@ADDRESS>\n”
    “Language-Team: \n”
    “Language: \n”
    “Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n”
    “MIME-Version: 1.0\n”
    “Content-Type: text/plain; charset=UTF-8\n”
    “Content-Transfer-Encoding: 8bit\n”
    “X-Generator: Loco https://localise.biz/\n”
    “X-Loco-Version: 2.2.1; wp-5.1.1”

    #: functions.php:260
    msgid “”
    “There are no shipping options available. Please ensure that your address has ”
    “been entered correctly, or contact us if you need any help.”
    msgstr “”

    jessepearson

    (@jessepearson)

    Automattic Happiness Engineer

    @joenuzzz Not sure if you determined the solution here. If you are using a string in your theme that needs to be translated, then the text domain for the translation would need to be defined in your theme’s style.css file. From there you would use that text domain in the string you’ve added into your theme’s files.

    Hope that helps.

    Due to the age of this thread I am setting it to resolved. If you still need further assistance you can change the status to not resolved.

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘Wrong language in AJAX call’ is closed to new replies.