• Resolved vivalis

    (@vivalis)


    Dear UM Team,

    My multi-language website goes the easy way regarding languages and uses the Google Language Translator plug-in which translates the content on the fly. Of course, this unfortunately doesn’t affect emails. I see that you’re saving the custom email templates as php files in the child theme, e.g. “approved_email.php”. Do you think it would be possible to create templates for different languages (e.g. “approved_email_de.php”, “approved_email_en.php”), and then for member emails UM takes the right template according to the member’s language (which is stored as alpha-2 code (e.g. “de”, “en”) in a language field in the member data)?

    Kind regards, Roger

Viewing 6 replies - 1 through 6 (of 6 total)
  • missveronica

    (@missveronicatv)

    @vivalis

    You can look at this UM email templates structure in this guide

    https://docs.ultimatemember.com/article/1335-email-templates

    Thread Starter vivalis

    (@vivalis)

    Thanks for the link, Veronica. I’ve read the article. Does that mean that multi-language emails out of UM do only work with WPML? Or – if I have the structure as suggested in the article – is there another way of making UM choose the language for a member email based on a language field in the member data?

    missveronica

    (@missveronicatv)

    @vivalis

    You can use the “um_email_template_path” filter hook
    to select the right language file and/or sub-folder.

    https://ultimatemember.github.io/ultimatemember/hooks/um_email_template_path.html

    Thread Starter vivalis

    (@vivalis)

    Thank you, that sounds promising. However, I’m a bit challenged now and need a bit more help with this.
    – Is the my_email_template_path function called everytime UM sends an email?
    – To set the right language file/folder, I need to check the language field of the email recipient member via the member id. How does the function have access to the member id of the recipient? Is that in the $args array?

    missveronica

    (@missveronicatv)

    @vivalis

    Is the my_email_template_path function called everytime UM sends an email?

    Yes, it’s within the UM function get_email_template in class-mail.php

    How does the function have access to the member id of the recipient?

    You can use the um_user function to get all user meta_values
    for example um_user( 'ID' ).

    https://docs.ultimatemember.com/article/158-umuser

    Thread Starter vivalis

    (@vivalis)

    Dear Veronica,

    It’s finally working. Thanks to Google Language Translator and Ultimate Member, I have now a website that is multilingual – incl. multilingual emails. Rather simple and without the complexity of WPML. Love it! It’s really amazing how UM is built with many hooks and classes that are accessible for website creators so that UM can be tweaked and customized in many ways. Really amazing! I’m an even bigger fan of UM now ??

    In case someone is looking for a similar solution, here’s how I did it:

    1. When the user enters the webpage with registration or account form or when he changes the website language while being on the registration or account page, this JS snippet reads the current website language setting and stores it in a hidden UM field:

    // Set user correspondence language from selected website language when entering the form
    if ($($userCorrLang).val()=='') {
    	setTimeout(function() {$($userCorrLang).val(get_glt_language('en')).change()}, 500);
    }
    // GLT language change event to set user language in case user changes website language
    $('.nturl').on('click', function() {
    	setTimeout(function() {$($userCorrLang).val(get_glt_language('en')).change()}, 500);
    });

    (get_glt_language(‘default lang’) is a custom function that reads the Google Language Translator cookie “googtrans” from the document cookie.)

    2. My UM email templates are stored in the child theme in an admin and several language folders:
    – Email templates for admin notifications: child-theme/ultimate-member/email/_admin
    – Email templates for emails to users/members: child-theme/ultimate-member/email/<language> (a folder for each language, e.g. en)

    3. Instruct UM to send emails from the above _admin and language folders, the language depending on the language setting in the recipient’s UM user data (the field that was filled in step 1), by using the UM’s um_email_template_path filter:

    add_filter( 'um_email_template_path', 'my_email_template_path', 10, 3 );
    function my_email_template_path( $located, $slug, $args ) {
    	$corr_lang = '';
    	if (UM()->config()->email_notifications[$slug]['recipient'] == 'admin') {
    		$corr_lang='_admin';
    	} else {
        	$corr_lang = um_user('user_corr_language');
    		if (!$corr_lang) {
    			$corr_lang='en';
    		}
    	}
    	$located = get_stylesheet_directory() . '/ultimate-member/email/' . $corr_lang . '/' . $slug . '.php';
        return $located;
    }

    4. Instruct UM to use a subject in the appropriate language by using UM’s um_email_send_subject:

    add_filter( 'um_email_send_subject', 'my_email_send_subject', 10, 2 );
    function my_email_send_subject( $subject, $key ) {
        $corr_lang = um_user('user_corr_language');
    	if (!$corr_lang || UM()->config()->email_notifications[$slug]['recipient'] == 'admin') {
    		return $subject;
    	}
    	switch ([$key, $corr_lang]) {
    		case ['checkmail_email', 'en']:
    			$subject = 'Your new {site_name} account: Please confirm email address'; break;
    		case ['checkmail_email', 'fr']:
    		...
    		// further cases for all user/member email types and languages
    	}
    	return $subject;
    }

    Thank you, Veronica. I really love the Ultimate Member plug-in, and I highly appreciate your great support!

    Kind regards, Roger

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Emails in member’s language’ is closed to new replies.