3007 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 200+ members
  • Has sold $250,000+ on Envato Market
  • Had an item featured in an Envato Bundle
+7 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. :)
3503 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $750,000+ on Envato Market
  • Located in United States
  • Helps us moderate the forums
+10 more
sevenspark 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
  • Has been part of the Envato Community for over 4 years
  • Has referred 200+ members
  • Has sold $250,000+ on Envato Market
  • Had an item featured in an Envato Bundle
+7 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.
470 posts
  • Sells items exclusively on Envato Market
  • Has been part of the Envato Community for over 5 years
  • Has sold $250,000+ on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+6 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
  • Has been part of the Envato Community for over 4 years
  • Has referred 200+ members
  • Has sold $250,000+ on Envato Market
  • Had an item featured in an Envato Bundle
+7 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.

3503 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $750,000+ on Envato Market
  • Located in United States
  • Helps us moderate the forums
+10 more
sevenspark 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
  • Has been part of the Envato Community for over 4 years
  • Has referred 200+ members
  • Has sold $250,000+ on Envato Market
  • Had an item featured in an Envato Bundle
+7 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

1055 posts Best-dressed man at PressNomics 2013
  • Has sold $1M+ on Envato Market and is now a Power Elite Author
  • Power Elite Author: Sold more than $1M on Envato Market
  • Made it to the Authors' Hall of Fame
  • Had an item featured on Envato Market
+10 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
  • Has been part of the Envato Community for over 4 years
  • Has referred 200+ members
  • Has sold $250,000+ on Envato Market
  • Had an item featured in an Envato Bundle
+7 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 :(
3503 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $750,000+ on Envato Market
  • Located in United States
  • Helps us moderate the forums
+10 more
sevenspark 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).

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by