I'm running a WPML site with woocommerce and have replace some words in woocommerce with code such as:
add_filter( 'wc_add_to_cart_message_html', function ( $message ) {
$text = 'Product added to your cart.';
return sprintf(
'%s %s',
esc_url( wc_get_cart_url() ),
esc_html__( 'View cart', 'woocommerce' ),
esc_html( $text )
);
} );
(courtesy of Christopher, Thank you!)
My question is, how to I translate something like this?
I can only find in the strings the original text:
"%s has been added to your cart." and translating that does not work..
Is there any solutions?
I'd like to change the “%s has been added to your cart." text
while still keeping the "view cart" button there. (Also changed the text there)
Thanks in advance!
steve
Not sure I understand the question. What exactly do you want to translate?
If it's the value of $text, then you should wrap it with a gettext function, then use WPML's auto register strings for translation' functionality to find this string and translate it, because you're using a filter to modify the string (more on this below).
Before:
$text = 'Product added to your cart.';
After:
$text = __( 'Product added to your cart.', 'my-theme-domain' );
More info on how to use WPML's auto register strings for translation' functionality:
In some cases, the static code-scan cannot reliably find all strings. This often happens when strings are generated dynamically using code.
https://wpml.org/documentation/getting-started-guide/string-translation/finding-strings-that-dont-appear-on-the-string-translation-page/#auto-register-strings-for-translation
Related
I've had a look at various StackOverflow questions but still can't seem to find a successful way to use a variable inside a shortcode. The code in question is as follows (the variable I'm trying to insert into the shortcode is $track_audio.
<?php
$track_audio = get_sub_field('mp3_url');
$renderedShortcode = do_shortcode('[fusion_audio src=". $track_audio . loop="off"][/fusion_audio]');
echo $renderedShortcode; ?>
I've read a fair few conversations on Stackoverflow but have not found a solution that works. The last idea i tried was to concatenate the variable (see code) but that hasn't worked either. Is anyone able to tell me the correct way to do this?
Many thanks in advance for your help with this
Phil
Try this way working for me
Change code from
$renderedShortcode = do_shortcode('[fusion_audio src=". $track_audio . loop="off"][/fusion_audio]');
TO
$renderedShortcode = do_shortcode('[fusion_audio src="'.$track_audio.'" loop="off"][/fusion_audio]');
Final conclision:
$track_audio = get_sub_field('mp3_url');
$renderedShortcode = do_shortcode('[fusion_audio src="'.$track_audio.'" loop="off"][/fusion_audio]');
echo $renderedShortcode;
function fusion_audio_shortcode_func( $atts, $content = null ) {
extract( shortcode_atts(
array(
'loop' => '',
), $atts )
);
$loop;
return '<awesomeness>' . $content . '</awesomeness>';
}
add_shortcode( 'fusion_audio', 'fusion_audio_shortcode_func' );
My string is: [slide image="http://themes.devatic.com/konzept/wp-content/themes/konzept/includes/uploadify/uploads/bas_006.jpg" slide_desc="
How to get the part after "image=" from it?
In short, use RegEx to get the data between 2 strings. For the most part, the same Reg Expressions between different languages should work just fine.
In PHP, you would want to use a preg_match.
$string = "[slide image=\"http://themes.devatic.com/konzept/wp-content/themes/konzept/includes/uploadify/uploads/bas_006.jpg\" slide_desc=\""
preg_match("/image=\"(.*)\"/i", $string, $results)
var_dump($results)
Update regex (a little more strict):
/image\="([^"]*)"/i
All in all, going to depend on the language being used and in what context. You can get much more advanced with the RegEx, but this is just quick & dirty.
Full PHP Code Example:
<?php
$string = '[slide image="http://themes.devatic.com/konzept/wp-content/themes/konzept/includes/uploadify/uploads/bas_006.jpg" slide_desc="<h4>Promotional Package</h4> Project description sentence" text_color="#464646" slide_horizontal="false"] [slide image="http://themes.devatic.com/konzept/wp-content/themes/konzept/includes/uploadify/uploads/bas_005.jpg" slide_desc="<h4></h4>" text_color="#464646" slide_horizontal="false"]';
preg_match_all('/image\="([^"]*)"/i', $string, $results);
foreach ($results[1] as $res):
echo 'Image URL:'.$res."\n";
endforeach;
?>
I use this function for BBcode parsing:
function bbcode ($message) {
$search = array(
'#\[(?i)b\](.*?)\[/(?i)b\]#si',
'#\[(?i)i\](.*?)\[/(?i)i\]#si',
'#\[(?i)u\](.*?)\[/(?i)u\]#si',
'#\[color=rgb(.*?)\](.*?)\[\/color\]#si',
'#\[quote](.*?)\[\/quote\]#si',
'#\[li](.*?)\[\/li\]#si',
'#\[ul](.*?)\[\/ul\]#si',
);
$replace = array(
'<b>\\1</b>',
'<i>\\1</i>',
'<u>\\1</u>',
'<span style=\"color:rgb\\1\">\\2</span>',
'<span class=\"quote">\\1</span>',
'<li>\\1</li>',
'<ul>\\1</ul>',
);
return preg_replace($search , $replace, $message);
}
In most cases it works ok, but not always.
For example:
[color=rgb(102, 0, 102)]H[color=rgb(204, 0, 0)]e[/color]llo[/color]
The result is:
<span style="color:rgb(102, 0, 102)">H[color=rgb(204, 0, 0)]e</span>llo[/color]
As you can see, only the first [color=...][/color] has been converted to html. The second stays as it is. Any ideas?
It's working correctly as you specified it. The problem is with embedded sequences.
I suggest you perform two replaces. One for the starting tags and one for the ending tags.
You might also be able to get away with specifying all of the starting tags first and
all of the ending tags last in the array of replacements.
That makes the search-replace values simpler anyway and in most cases you don't
need to use back-references, especially for simple tags like [b].
That should fix your problem.
In Drupal 6, how do you print a taxonomy term as a CSS body class?
I have found this snippet that lets you print almost every aspect of Drupal content as a body class, but it doesn't include taxonomy terms:
http://www.davidnewkerk.com/book/122
Being able to print taxonomy terms as a body class is essential for theming processes, so I am surprised that a solution is not readily available.
Check what variables are passed to the page template by either doing print_r($vars) or dpm($vars) in your page pre-process function or using the http://drupal.org/project/devel_themer module. The usage of dpm require you to install the devel module.
You will find that some themes will pass $taxonomy as a variable to page.tpl.php . If that is not the case you can find the taxonomy terms in the $node variable which is also available in the page.tpl.php in some themes.
(The above holds true for my fusion based theme acquia marina http://drupal.org/project/acquia_marina ). Once you have these taxonomy terms available you can easily print them out in your body classes.
After much hard work, I found a very easy way to do this.
On Drupal Snippets, there is a snippet that lets you print out the taxonomy terms applied to each page as text.
The only problem is that the snippet will print any spaces or punctuation that are in the taxonmy term, which is no good for body classess.
However, by adding a str_replace command, you can strip out all the spaces and punctuation.
I'm sure there are other people who wants to print taxonmy terms as body classes, so to save them the bother, here is the code that I used with the str_replace command added.
Put the following in template.php:
function getTerm($label, $vid, $link) {
$node = node_load(array('nid'=>arg(1)));
foreach((array)$node->taxonomy as $term){
if ($term->vid == $vid){
if ($link){
$link_set[] = l($term->name, taxonomy_term_path($term));
} else {
$link_set[] = $term->name;
}
}
}
if (!empty($link_set)){
$label = ($label) ? "<strong>$label </strong>" : "";
$link_set = $label.implode(', ', $link_set);
}
$link_set = str_replace(' ', '_', $link_set);
$link_set = str_replace('&', 'and', $link_set);
$link_set = strtolower($link_set);
return $link_set;
}
Put the following in Page.tpl.php:
<body class="taxonomy-<? print getTerm(false, 1, false);?>">
I hope this helps anyone who has the same problem.
Extra tips:
(1)In the code I have posted, the only punctuation that is striped out is the ampersand (i.e. '&').
If you have other punctuation to strip out use the following:
$link_set = str_replace('INSET_PUNCTUATION_HERE', 'INSERT_REPLACEMENT_HERE', $link_set);
Place this command under the other $link_set lines in the code I have posted for template.php.
(2) In the page.tpl.php code I have posted, the "taxonomy-" part places the words taxonomy and a dash before each body class term. You can edit this as you wish to get the results your require.
Do you know of any way to reference an object in the replacement part of preg_replace. I'm trying to replace placeholders (delimited with precentage signs) in a string with the values of attributes of an object. This will be executed in the object itself, so I tried all kinds of ways to refer to $this with the /e modifier. Something like this:
/* for instance, I'm trying to replace
* %firstName% with $this->firstName
* %lastName% with $this->lastName
* etc..
*/
$result = preg_replace( '~(%(.*?)%)~e', "${'this}->{'\\2'}", $template );
I can't get any variation on this theme to work. One of the messages I've been getting is: Can't convert object Model_User to string.
But of course, it's not my intention to convert the object represented by $this to a string... I want to grab the attribute of the object that matches the placeholder (without the percentage signs of course).
I think I'm on the right track with the /e modifier. But not entirely sure about this either. Maybe this can be achieved much more simple?
Any ideas about this? Thank you in advance.
Like I commented to Paul's answer: in the meanwhile I found the solution myself. The solution is much more simple than I thought. I shouldn't have used double quotes.
The solution is as simple as this:
$result = preg_replace( '~(%(.*?)%)~e', '$this->\\2', $template );
Hope this helps anyone else for future reference.
Cheers.
Check out preg_replace_callback - here's how you might use it.
class YourObject
{
...
//add a method like this to your class to act as a callback
//for preg_replace_callback...
function doReplace($matches)
{
return $this->{$matches[2]};
}
}
//here's how you might use it
$result = preg_replace_callback(
'~(%(.*?)%)~e',
array($yourObj, "doReplace"),
$template);
Alternatively, using the /e modifier, you could maybe try this. I think the only way to make it work for your case would be to put your object into global scope
$GLOBALS['yourObj']=$this;
$result = preg_replace( '~(%(.*?)%)~e', "\$GLOBALS['yourObj']->\\2", $template );