• Resolved jeromeeee

    (@jeromeeee)


    Hi,

    I have a JavaScript that displays some content on a Google map interface. Is there any way to decode this within the JS file?

    This is what is being displayed currently:
    {:en}4 Room Flat{:}{:zh}四房式{:} in {:en}Sale{:}{:zh}出售{:}

    Optimally, it should display the content based on the locale, and this is how I’m getting the variable in the JS (if it helps):

    var category = decodeURIComponent ( this.category.replace(/-/g,’ ‘) );

    Thanks in advance!

    https://ww.wp.xz.cn/plugins/wpglobus/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support Alex Gor

    (@alexgff)

    you can use filter in your template to get appropriate language string

    echo apply_filters( 'the_content', '{:en}4 Room Flat{:}{:zh}四房式{:}' );
    echo 'in';
    echo apply_filters( 'the_content', '{:en}Sale{:}{:zh}出售{:}' );

    Thread Starter jeromeeee

    (@jeromeeee)

    Problem is the output is directly from the JS file, not the PHP template file. Is there any other workaround?

    Plugin Support Alex Gor

    (@alexgff)

    for js you can use

    WPGlobusCore.TextFilter('{:en}4 Room Flat{:}{:zh}四房式{:}',WPGlobusCoreData.language);

    Thread Starter jeromeeee

    (@jeromeeee)

    Pardon my dumb question, but I am getting the error:

    VM4335:2 Uncaught ReferenceError: WPGlobusCore is not defined

    I have moved the wpglobus.min.js file to the header, and it is loaded in the page above my file, but can’t seem to use the code in my JS script. Any suggestions?

    Plugin Support Alex Gor

    (@alexgff)

    You can find solution at PHP using filters when data are retrieved from DB

    Thread Starter jeromeeee

    (@jeromeeee)

    Turns out that it doesn’t work as I’m reading the information from a file rather than a DB. As such, the PHP functions apply_filers did not work after many tests.

    In the end, I wrote a custom JS function to do text filtering based on the language. In case if any one stumbles across the same problem as me:

    function switch_content_language(text){
    if (typeof WPGlobus === ‘undefined’) { return; }

    var lang = WPGlobus.language;
    var res = text.split(“{:}”);
    for (i = 0; i < res.length; i++) {
    if (res[i].toLowerCase().indexOf(lang) >= 0) {
    return res[i].substring(res[i].indexOf(“}”) + 1);
    }
    }
    }

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

The topic ‘Decode from JS’ is closed to new replies.