3007 posts
  • Author had a File in an Envato Bundle
  • Bought between 1 and 9 items
  • Elite Author
  • Europe
  • Exclusive Author
  • Has been a member for 3-4 years
  • Referred between 100 and 199 users
+2 more
duotive says
Hello. I want to add a new feature to our themes. The ability the change the labels in the admin panel. I do that by using php define and it works just great. One problem i encounter is how do i make those variables translatable also by mo and po files? Any suggestions would be appreciated. :D The code i have ( until now ) is something like so:
    //GENEARAL
    define('dt_ReadMore','Read More »');
    define('dt_Permalink','Permalink to ');    
instead of the texts i will have variables that will get the values from metaboxes in the admin panel so i will have strings, not texts. How can i make them translatable. I cannot find any good ideas at the moment and would appreciate any help. :)
3415 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Community Moderator
  • Bought between 100 and 499 items
  • Referred more than 2000 users
  • Has been a member for 4-5 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
+4 more
sevenspark Volunteer moderator says

Have you utilized WordPress’ standard i18n techniques?

http://codex.wordpress.org/I18n_for_WordPress_Developers http://codex.wordpress.org/Translating_WordPress

Can’t tell if this is what you’re looking for or if you’re trying to do something beyond these functions :)

3007 posts
  • Author had a File in an Envato Bundle
  • Bought between 1 and 9 items
  • Elite Author
  • Europe
  • Exclusive Author
  • Has been a member for 3-4 years
  • Referred between 100 and 199 users
+2 more
duotive says
I am trying beyond them because i am overwhelmed with customers that want to change just one word in the whole theme and that is a hard task to do for that many customers and i want to avoid it. I cannot cover a php string with the __ like so
__($dt_ReadMore,'duotive');
because the scanner will not know what to do there and will just add the variable as normal text. The end point i want to make strings translatable from the backend but also with the po and mo files because some users may want to use multi language.
469 posts
  • Author had a File in an Envato Bundle
  • Sold between 250 000 and 1 000 000 dollars
  • Elite Author
  • Contributed a Tutorial to a Tuts+ Site
  • Exclusive Author
  • Has been a member for 4-5 years
  • India
+1 more
SaurabhSharma says

Just tested the following on a test theme and I got the output successfully:

<?php define('dt_ReadMore',__('Read More &#187;', 'duotive'));
echo dt_ReadMore; ?>

Now since the constant is still accepting the value and echoing correctly, I hope the gettext will parse __() too.

3007 posts
  • Author had a File in an Envato Bundle
  • Bought between 1 and 9 items
  • Elite Author
  • Europe
  • Exclusive Author
  • Has been a member for 3-4 years
  • Referred between 100 and 199 users
+2 more
duotive says

Now see that is the problem? I want instead of the text to have a php string that i get from the backend.

3415 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Community Moderator
  • Bought between 100 and 499 items
  • Referred more than 2000 users
  • Has been a member for 4-5 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
+4 more
sevenspark Volunteer moderator says

I guess I’m probably missing something, but I thought the issue was just that if $dt_ReadMore doesn’t have a translation, it’ll just spit back out the value of $dt_ReadMore. So couldn’t you do something like this?

//Define default
define( 'dt_ReadMore', 'Read More &#187;' );

//Retrive special value from DB (with 'dt_ReadMore' key), or set to default
$dt_ReadMoreText = get_option('dt_ReadMore', dt_ReadMore);

$readMoreText = __($dt_ReadMoreText, 'duotive');

Of course, this code could be condensed, or made into a function of its own. But at this point I think we have the following result:

1. If no translation exists, and no ‘special value’ is set, the default is returned.

2. If a translation exists, and no ‘special value’ is set, the translation is returned.

3. If a ‘special value’ is set, it is returned (unless there is a translation for the special value)

I think that would allow users to change individual words but still make use of translations?

3007 posts
  • Author had a File in an Envato Bundle
  • Bought between 1 and 9 items
  • Elite Author
  • Europe
  • Exclusive Author
  • Has been a member for 3-4 years
  • Referred between 100 and 199 users
+2 more
duotive says

Yes this is true, excerpt for the 3rd conclusion, witch in my opinion cannot be true, because when scanning with POEdit you cannot have the strings from the database. This was one of my options, if onone has any better ideas i will go with this. Thanks man

1018 posts Best-dressed man at PressNomics 2013
  • Attended a Community Meetup
  • Author had a File in an Envato Bundle
  • Bought between 1 and 9 items
  • Contributed a Tutorial to a Tuts+ Site
  • Exclusive Author
  • Has attended an Envato Live event
  • Has been a member for 5-6 years
+5 more
Parallelus says

I’ve always used this:

$myString = sprintf (  __('Use %s to include the text.', THEME_NAME),  $theTerm );

echo $myString;

When translating they must keep the “%s” in the translation but it works. If I remember correctly, POEdit recognizes strings with “%s” and knows they’re special, but I could be mixing that up with something else.

3007 posts
  • Author had a File in an Envato Bundle
  • Bought between 1 and 9 items
  • Elite Author
  • Europe
  • Exclusive Author
  • Has been a member for 3-4 years
  • Referred between 100 and 199 users
+2 more
duotive says
Parallelus said
I’ve always used this:
$myString = sprintf (  __('Use %s to include the text.', THEME_NAME),  $theTerm );

echo $myString;
When translating they must keep the “%s” in the translation but it works. If I remember correctly, POEdit recognizes strings with “%s” and knows they’re special, but I could be mixing that up with something else.
Thanks :) so this is tested and it works.. good :D. Please correct me if i am wrong:
define( 'dt_ReadMore', sprintf ( __('Read More &#187;', 'duotive'), get_option('dt_ReadMore') ) );
I do not know regex and sprintf functions :(
3415 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Community Moderator
  • Bought between 100 and 499 items
  • Referred more than 2000 users
  • Has been a member for 4-5 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
+4 more
sevenspark Volunteer moderator says
duotive said
Parallelus said
I’ve always used this:
$myString = sprintf (  __('Use %s to include the text.', THEME_NAME),  $theTerm );

echo $myString;
When translating they must keep the “%s” in the translation but it works. If I remember correctly, POEdit recognizes strings with “%s” and knows they’re special, but I could be mixing that up with something else.
Thanks :) so this is tested and it works.. good :D. Please correct me if i am wrong:
define( 'dt_ReadMore', sprintf ( __('Read More &#187;', 'duotive'), get_option('dt_ReadMore') ) );
I do not know regex and sprintf functions :(

sprintf is a formatter – basically it replaces variables in a preformatted string. So without a %s or %d or the like in the first argument, I don’t think it’s doing anything.

See http://php.net/manual/en/function.sprintf.php

I was going to suggest this earlier, but I think the problem I think is that you’re not replacing part of a string, but the whole thing. I think the sprintf i18n method is intended to be used when a string needs to be translated, but a part of that string varies, for example, a number. It basically allows you to translate a string, then replace the variable. In your case, the entire string is a variable, so I don’t think this does anything for you (you need to pass something to the translation function).

by
by
by
by
by
by