• Resolved franpol

    (@franpol)


    Hi,
    I’m trying to automatically translate my widget into different languages, so I use the following code :

    
    /**
     * Return translation text
     *
     * @param      $token
     *
     * @return mixed
     */
     $LANG="fr";
     
     
    function __($token) {
        global $LANG;
    
        $translations = include('./lang/' . $LANG . '.php');
    
        if (empty($LANG) || empty('translations') || !array_key_exists($token, $translations))
        {
            return $token;
        }
        else
        {
            return $translations[$token];
        }
    }
     }
    
    /**
     * Display translation text
     *
     * @param      $token
     * @param null $lang
     */
    function _e($token, $lang = null) {
        echo __($token, $lang);
    }
    

    But with this code I get the error : Fatal error: Cannot redeclare __() (previously declared …

    I don’t know if this function is used in a hook cause the name is very strange.

    Have you a solution ?

    Thanks, for your help.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    @franpol – are you in control of the rushabh77 user account that posted above [ETA: post archived]? If so, don’t do that. Not only is it confusing, but using multiple accounts is very suspicious. There is rarely any good reason for this. Until this can be clarified, the rushabh77 account is under moderation watch.

    To answer your question, you cannot override WP translation functions like __() and _e(). Nor is there ever a good reason to. You should use the gettext() translation functionality as intended. Themes and plugins in the WP repository can be translated for you into multiple languages by the community. You can of course opt out of this and provide your own .mo translations files used by the gettext system. Do not try to work against gettext, work with it 🙂

    • This reply was modified 7 years, 5 months ago by bcworkz.
    Thread Starter franpol

    (@franpol)

    Thanks bcworkz for your answer.

    P.S : I’m not in control of the rushabh77. I thought that he tried to modify the code, but no. I think you can block this user account, and delete his message which had no sense.

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

The topic ‘Function to translate widget’ is closed to new replies.