WordPress.org

Ready to get started?Download WordPress

Forums

get_language question (17 posts)

  1. 3x7
    Member
    Posted 1 year ago #

    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!

  2. grosshat
    Member
    Posted 1 year ago #

    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;
    }
    ?>
  3. 3x7
    Member
    Posted 1 year ago #

    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!

  4. grosshat
    Member
    Posted 1 year ago #

    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;
    }
    ?>
  5. 3x7
    Member
    Posted 1 year ago #

    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

  6. grosshat
    Member
    Posted 1 year ago #

    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.

  7. 3x7
    Member
    Posted 1 year ago #

    yes I tried with echo also... no luck...

  8. grosshat
    Member
    Posted 1 year ago #

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

  9. 3x7
    Member
    Posted 1 year ago #

    From admin panel, it's an export plugin.

  10. grosshat
    Member
    Posted 1 year ago #

    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);
  11. 3x7
    Member
    Posted 1 year ago #

    tried that before... strange but outputs "en-US"

  12. grosshat
    Member
    Posted 1 year ago #

    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))

  13. 3x7
    Member
    Posted 1 year ago #

    same "en-US" :|

  14. grosshat
    Member
    Posted 1 year ago #

    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 );

  15. 3x7
    Member
    Posted 1 year ago #

    This works, I get only "en"

  16. grosshat
    Member
    Posted 1 year ago #

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

  17. 3x7
    Member
    Posted 1 year ago #

    Who knows? :|

    For now, until I solve it, I'll use your previous solution.

    Thanks allot!

Topic Closed

This topic has been closed to new replies.

About this Topic