Title: Function to translate widget
Last modified: November 23, 2018

---

# Function to translate widget

 *  Resolved [franpol](https://wordpress.org/support/users/franpol/)
 * (@franpol)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/function-to-translate-widget/)
 * 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](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/function-to-translate-widget/#post-10911730)
 * [@franpol](https://wordpress.org/support/users/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](https://wordpress.org/support/users/bcworkz/).
 *  Thread Starter [franpol](https://wordpress.org/support/users/franpol/)
 * (@franpol)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/function-to-translate-widget/#post-10920501)
 * 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.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 2 replies
 * 2 participants
 * Last reply from: [franpol](https://wordpress.org/support/users/franpol/)
 * Last activity: [7 years, 5 months ago](https://wordpress.org/support/topic/function-to-translate-widget/#post-10920501)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
