Wordpress WPML not translating get_option - wordpress

i am trying to translate my plugin options through WPML but it is not working.
Here is how i have placed my string in the plugin file
get_option(_e('my_label','my-text-domain'));
I have already scan my plugin through the WPML and have done translation in "German" while default is English.
Can anyone help me.
Thanks

The issue is that _e outputs the translated text.
You want __ (double underscore), which will return the translated text.
I would explore other solutions to this problem. In general you shouldn't be using translation to determine what option to pull from the database. The functionality of your plugin should not hinge on whether or not the translated text is a valid option name.
An alternative approach may be to use get_locale to fetch the current locale and then use that to determine the option name:
$option_name = get_locale() . 'my_label';
$label = get_option( $option_name );
Now you can still get the localized version of your option, without depending on translators to input the correct option name.
If you have to use the translation approach, I would use _x to explain that the translated text should be an option key.
get_option( _x('my_label', 'Must be a valid option name', 'my-text-domain') );

Provided that you have multiple entries in your meta table per language, try the double underscore function?:
get_option( __( 'my_label','my-text-domain' ) );

Related

Get post ID of the original version in WPML

I have a website using WooCommerce in the Slovenian language that is being translated with WPML into multiple languages and English will become the default one.
Products were all set in Slovenian language and use a custom field (ACF) for an image. The products are translated but the images are duplicated which causes issues in one case. The image should be of the original product.
I have now fixed this by using this:
$preview_img_id = get_field( 'preview_image', apply_filters( 'wpml_object_id', get_the_ID(), 'product', FALSE, 'sl' ) );
But this is not a good solution in the long term because new products might be added in another language first.
What function can I use to get the ID of the original post/product?
I'm not sure I understand what "causes issues in one case", but I believe you can omit the $ulanguage_code and set $return_original_if_missing to TRUE. Documentation:
$ulanguage_code - (mixed) (Optional) If missing or NULL, it will use
the current language. If set to a language code, it will return a
translation for that language code or the original if the translation
is missing and $return_original_if_missing is set to TRUE. Default is
NULL
If you always want to get the ID of the original product, then maybe you could trick it into doing this by specifying a non-existent language code, for example:
apply_filters( 'wpml_object_id', get_the_ID(), 'product', TRUE, 'xx' )

WordPress SDL Translation for shortcodes

I have some sort of sort of shortcodes like [wp-text text="Lorem Impsum etc" button_text="Read More"][/wp-text].
I want to extract this shortcode from post content field and then send these two attributes: text and button_text for translation to SDL World Server.
After getting response from SDL, I want to replace that shortcode accordingly.
Any Suggestion or Help?
Here is a suggestion about your approach:
Parse the shortcode attributes
Having the content you can parse the variable and extract all short code attributes with the provided Wordpress function shortcode_parse_atts
For each parsed value make a API call to the provided SDL World Server REST API (Or for better performance you can group all the translations and translate them with one complex API call if supported from the API)
After getting the response replace the original strings with the translated strings.
Here is example pseudo code in PHP:
$shotcode_attributes = shortcode_parse_atts( $post->post_content );
foreach ( $shortcode_attributes as $attribute => $value ) {
// Make SDL World Server API call refer to the API documentation for the exact way
$translated_value = $sdlApiClient->translate( $value );
// Replace the translated value.
// You can be more precise here ensuring that the value is inside the shortcode
$post->post_content = str_replace(
"{$attribute}=\"{$value}\"",
"{$attribute}=\"{$translated_value}\"",
$post->post_content
);
}
You can also research and examine the provided SDL WordPress Connector

Wordpress - polylang in command launch post traduction

Today I need some help with Wordpress, as is is far from being my cup of tea.
Currently, I'm tring to create a plugin to export translation from post in a xliff file then reimport this xliff file to set the translation. This plugin we only be used If wordpress uses polylang for translations. For now I'm only exporting meta data.
I've managed to find how polylang is linking two post, saying this one is in english and this one is in french. So for now I have a command to export a post in a xliff file, and a command to import a traduction from an xliff file.
Everything work fine as long, as the post I'm trying to translate already has existing translation (basically I'm just updating meta data).
But my problem, is when I have a post in french, but I do not have a matching post in english.
What I wanted to do was to do, was to create the missing post and after overwrite the meta data.
But I do not know how to create the missing post as a copy of my post and saying that this one is in english. Does anyone know how I can do that from my command ?
Thanks
Actually I found what I wanted. Polylang has commande line for duplicating a post in an other language. And I just had to use \WP_CLI::runcommand
$options = array(
'return' => true,
'parse' => 'json',
'launch' => false,
'exit_error' => true,
);
$command = 'pll post duplicate ' . $idPost . ' ' . $destLocale;
\WP_CLI::runcommand($command, $options);

Saving an array to Advanced Custom Fields WordPress

I'm using the ACF plugin for WordPress. I'm posting to a custom post type programmatically via my theme. Now, I thought I would be able to save an array of items to a custom field quite easily, but whenever I try to, nothing gets input. I simply get this error when I check the post screen in the admin area:
Warning: htmlspecialchars() expects parameter 1 to be string, array given in /home/lukeseag/public_html/spidr-wp/wp-content/plugins/advanced-custom-fields/core/fields/text.php on line 127
Now, I'm kind of throwing this out there for suggestions as to how I can go about solving this problem.
To give some context, I'm programmatically saving data input by a user, and creating a post with it. Much of the data is simple strings and numbers that can each have their own custom field.
But I have an unknown number of text strings and URL's (with an id number for each) coming through this page too, that need to be linked with the same post. I need to be able to output each set of text string and URL into their own div on the single.php post page. Now, ideally these text/url pairs would be saved in a single array so I can just loop through the array and output the data, but it looks like ACF doesn't want to let this happen?
Any suggestions as to how I can save this data and then output it easily on the single.php page? Each set of results will have an ID (number), text string and url.
Thanks!
This is exactly why all those "frameworks" are usually more pain than gain. they are designed to look flexible to the lazy but then they always prove useless on a real case scenario that is a bit more complex than anything that would actually be easily achieved without them.
Anyhow, as for your question.
The problem is that you are passing array instead of a string .
I am not going to go into debugging the plugin, and anyhow , more code and info is needed , so i will just give you ONE possible and easy solution, and that is to compose a pseudo-array string like so :
'ID|URL|STRING'
note that the delimiter pipe (|) is just an example, you could use ( , ) ( : ) ( # ) or whatever.
in other words :
$url = 'http://myurl.url/something';
$id = 35;
$string = 'my string';
$pseudo_r = $id . '|' . $url . '|' . $string . '|'; // results in 'ID|URL|STRING';
or you can use the implode() function
$pseudo_r = array(
"url" => $url,
"id" => $id ,
"string" => $string
);
$pseudo_r = implode("|", $pseudo_r); // gives a string
and pass it as a string that later on you can decompose it with explode() like so :
$pseudo_r = explode( '|', $pseudo_r ); // gives back the original array
( Please note that now the array is NON ASSOCIATIVE anymore . )
now, since you mentioned that each one of those is a set, you can compose the same pseudo array from the sets, using a different delimiter.
This might work for you , but essentially it is wrong .
why is it wrong ?
Because this is a generic and somewhat hackish solution that actually ignores your data type .
You are mixing 3 types of data types , URL , ID and a STRING .
All 3 ( should ) have potentially different validation schemes , and I suspect that the htmlspecialchars() part belong to the URL.
Another solution is just to find the right data field in the ACF settings or use a different field for each and then just DISPLAY it together , also using the implode() and explode() functions.
Yo can save the array using json_encode($your_array) and json_decode() to recover it. This works also for associative arrays.

qTranslate do not detect properly when called from inside a plugin through ajax

I got two problems.
Problem 1:
I have translated mailchimp's pot file into two languages. The .mo has been tested to be working.
But WP fails to show the ajax returns for other languages for the mailchimp form and/or widget for mailchimp list subscribe form.
I got around this by modifying the mailchimp.php like following:
$textdomain = 'mailchimp_i18n'; #line 57 to start
if (defined('WPLANG'))
$lang=WPLANG;
$locale = apply_filters( 'plugin_locale', $lang, $textdomain);
How did I figure out?
In place of this $lang variable, default mailchimp.php has get_locale() defined in l10n.php of wp-includes.
When I placed a print_r($locale) before modifying as above. It shows the correct language set at wp-config's WPLANG. But when the mailchimp form is submitted it gets just default en_US!
*Where does it get the en_US, when I haven't set it?*
I found it very weird, line 48-49 of l10n.php for definition of get_locale() contains:
if ( empty( $locale ) )
$locale = 'en_US';
Which, I think gets $locale as empty while in my case was called from the form. Which is very unlikely! But it happens in four instances of my testing.
What my modification at mailchimp.php gets me to?
I can now get the other language (ie Arabic) strings to my mailchimp form responses. But as its detected from WPLANG, I cannot switch to English in site's English mode.
Problem 2:
To switch languages on the fly. I used qtrans_getLanguage() in place of get_locale() in the mailchimp.php's code part. Thus, it becomes:
$locale = apply_filters( 'plugin_locale', qtrans_getLanguage(), $textdomain);
If I print_r($locale) after this line. It shows current language. But not when the form is submitted. The form's response always shows it as (en)!.
Now, problem1 is very unlikely to happen and is unusual. But for problem2 I definitely think there is a smart work around. Why would the qTranslate report wrong while called from the Ajax of that form?
Also, I do not want to modify core plugin files. Currently, I had to modify mailchimp.js to manually match and replace common strings(And I hate it!).
Solved it by following this and this
But as mailchip.js has this 'ajax_url' I searched in the mailchimp plugin where this URL is generated from. Its generated at line number 96 in mailchimp.php
'ajax_url' => trailingslashit(home_url().'/'. qtrans_getLanguage()),
I just added the '/'. qtrans_getLanguage() part.

Resources