0

I tried to change text using the below code

function ctxtlearn_gettext( $ctxttranslation, $ctxttext ) {
   $ctxttrans = array_values(changeTxt_setting('trans',true));
   $ctxtdirty = false;
   $ctxtstrings_map = array();
   $ctxttext_words = explode( ' ', $ctxttext );

   foreach($ctxttrans  as $ctxtmytrans) {
       $ctxtstrings_map[$ctxtmytrans['key']] = $ctxtmytrans['val'];
   }

   foreach ( $ctxttext_words as $ctxtk => $ctxtword ) {
       $ctxtw = trim( $ctxtword, '.,:!?-_()' ); 
       if ( isset( $ctxtstrings_map[ $ctxtw ] ) ) {

           $ctxttext_words[ $ctxtk ] = str_replace( $ctxtw, $ctxtstrings_map[ $ctxtw ], $ctxtword );
           $ctxtdirty = true;
       }
   }

   if ( $ctxtdirty ) {
       return implode( ' ', $ctxttext_words );
   }
   return $ctxttranslation;
}
add_filter( 'gettext', 'ctxtlearn_gettext', 10, 2 );

If i give the below input it works

Post => My Book
Page => My Vertical
Comments => My Feedback

The Word before => is old string and after that is new string. if i pass it as an array it work's. but if i provide the below input

First Name => My Name
Username => User ID

The username text changes but First Name Not Changing in profile page. But if i split and provide its getting changed.

Who i can do this by providing Whole String..?

  • What has this to do with WordPress? – vancoder Sep 23 '14 at 16:46
2
+50

You are exploding the text with explode( ' ', $ctxttext ).
Therefore you are translating word by word, not strings. That is why it's working with single word string, and not with multiple words.

When you call First Name you are translating First and Name.

Cheers

  • if i use like that then where ever the word first name or name is called will be changed.. i need the full parse to be changed when its called fully.. for eg if its called as first should not change.. if its called as first name it should change as Your ID – Varun Sridharan Sep 26 '14 at 15:37
  • Yes, and that is why you need to remove the explode and implode parts from your function. Can you also paste the changeTxt_setting function code, so it's easier to see the problem? – Diogo Gomes Sep 26 '14 at 15:49
  • i think you don't understand the exact issue – Varun Sridharan Sep 26 '14 at 16:02
0

I itself found the answer..

below code is working

add_filter(  'gettext',  'wps_translate_words_array'  );
add_filter(  'ngettext',  'wps_translate_words_array'  );
function wps_translate_words_array( $translated ) {

     $words = array(
                        // 'word to translate' = > 'translation'
                        'Posts' => 'Article',
                        'Post' => 'Articles',
                        'Pages' => 'Stuffing',
                        'Media' => 'Upload Images',
                        'Links' => 'Blog Roll',
                    );

     $translated = str_ireplace(  array_keys($words),  $words,  $translated );
     return $translated;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.