• I have a question on get_language…

    it works, just that I would like to modify the output…

    from “en_US” to only “en”…

    Thanks!

Viewing 15 replies - 1 through 15 (of 16 total)
  • grosshat

    (@grosshat)

    I am not sure if you are using an own function get_language for fetching ISO language. If so, I do not know the specifics of this.

    If you just want to get the language used on a blog, use the standard call bloginfo('language'). That returns the ISO, ie. en-US. From there you can decorate it with a helper function so:

    <?php
    $iso = bloginfo('language');
    function get_short_lang( $lang ) {
        $lang = substr( $lang, 0, 2 );
        return $lang;
    }
    ?>
    Thread Starter 3×7

    (@3x7)

    Thanks,

    I tried with all the code including <?php and it didn’t work.
    I got Parse error: syntax error, unexpected…
    then
    I’ve put function on the top of my plugin where functions are, then where I used echo bloginfo('language'); I’ve replaced “echo” with “$iso”

    Where did I go wrong?

    thanks in advance!

    grosshat

    (@grosshat)

    Sorry, the code I wrote was wrong, since I was passing $lang to the function instead $iso. And I assumed that you would call the function somewhere to get the value.

    So the right code would be:

    <?php
    $iso = bloginfo('language');
    $lang = get_short_lang($iso);
    
    /**
     * Get the first two letters of a language ISO.
     *
     * @param string $iso
     * @return string $lang
     */
    function get_short_lang( $iso ) {
        $lang = substr( $iso, 0, 2 );
        return $lang;
    }
    ?>
    Thread Starter 3×7

    (@3x7)

    Sorry to bother you so much, I tried all kind of combinations and had no luck…
    It actually gets the language with no errors now, but it still outputs the original “en-US”.

    Thanks

    grosshat

    (@grosshat)

    Well, do not give up. ??

    Are you using the value returned by the function get_short_lang() (ie. $lang)? This is the value you need.

    Thread Starter 3×7

    (@3x7)

    yes I tried with echo also… no luck…

    grosshat

    (@grosshat)

    Where are you calling that code? At functions.php level or template level?

    Thread Starter 3×7

    (@3x7)

    From admin panel, it’s an export plugin.

    grosshat

    (@grosshat)

    Oki. The code I gave you is working as plain PHP. You can test it and see that. The problem could be the way the function is called on the plugin.

    So, do not call a function, just parse directly the value of the ISO.

    echo substr(bloginfo('language'), 0, 2);
    Thread Starter 3×7

    (@3x7)

    tried that before… strange but outputs “en-US”

    grosshat

    (@grosshat)

    That is weird. :/

    How do you get when you do print_r for each value?

    print_r(bloginfo('language'))
    print_r(substr(bloginfo('language'), 0, 2))

    Thread Starter 3×7

    (@3x7)

    same “en-US” ??

    grosshat

    (@grosshat)

    Something going wrong. :/

    If you give the value to a variable and then make a substr, what do you get?

    $iso = ‘en-US’;
    $lang = substr( $iso, 0, 2 );
    print_r( $lang );

    Thread Starter 3×7

    (@3x7)

    This works, I get only “en”

    grosshat

    (@grosshat)

    Yep. So, why does not work when you use the value returned by bloginfo('language')? :/

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘get_language question’ is closed to new replies.