Translate Error & Success Messages Answered

Post author
George Liu

Hello Guys, just wondering, I might need to translate Error & Success Messages into more than 4 languages, is it possible with memberstack?

Comments

2 comments

  • Comment author
    Marc Hudson

    EDITED: I’m not aware of this being available with MS right now (hopefully I’m wrong) but this is on my TODO list as well.

    Here’s some rough code. It wraps the MS method in your own function. Seems to work well but needs thorough testing. It also relies on the MS message Strings not changing in future, so if you can use a library or API to translate any text String, it would be more robust:

    const originalMethod = $memberstackDom._showMessage; // Store the original method
    
    $memberstackDom._showMessage = function(text, isError) {
      // Your code to run before the original method.
      console.log('$memberstackDom._showMessage was called.', text);
    
      if (!!text) {
        switch(text) {
          case 'Your password has been updated.':
            text = 'Su contraseña ha sido actualizada.';// Spanish
            break;
          case 'The provided password is invalid.':
            text = 'La contraseña proporcionada no es válida.';// Spanish
        }
      } 
      // Call the original method.
      originalMethod.apply(this, [text, isError]);
    };

    * I will not be held responsible for my Spanish :)

    For anyone who’s interested in future, here’s a working version:
    HELP.waitFor(window, '$memberstackDom', 50, function() {
            // Original MS method.
            const msShowMessage = $memberstackDom._showMessage;
            // Wrapper function.
            $memberstackDom._showMessage = function(text, isError) {
              let lang = LANG.currentLang();
              // If not language != English and Weglot has loaded.
              if (lang != 'en' && HELP.checkKeyExists(window, 'Weglot')) {
                Weglot.translate({
                  'words':[{"t": 1, "w": text}],
                  'languageTo': lang
                }, function(data) {
                  // Call original method with translated message.
                  msShowMessage.apply(this, [data[0], isError]);
                });
              }
              else {
                // Show original message without translating.
                msShowMessage.apply(this, [text, isError]);
              }
            };
        });
     
    It’a also checks to see if Weglot has loaded (if you’re using that service…), if it hasn’t or you’re using the base language (‘en’ for English, in my case), it just outputs the message with no translation.
    LANG.currentLang() just returns a 2-character country code String such as ‘es’ for Spain, or ‘en’.
     
    Seems to be working nicely but needs more testing to cover edge cases.
    1
  • Comment author
    Duncan from Memberstack

    Thanks for sharing this solution, Marc! 🙏

    0

Please sign in to leave a comment.