Gettext requires fixed, static strings so you cannot dynamically generate a select list. You’ll need to define each option one at a time. For example:
echo '<select name="function_camere">';
$i = __('one','sacconicase');
echo '<option value="one" '. selected( $saved, 'one', false ) .' >'.$i.'</option>';
$i = __('two','sacconicase');
echo '<option value="two" '. selected( $saved, 'two', false ) .' >'.$i.'</option>';
//etc....
In this example, the saved value is always in English, but the displayed options are in the current language. This approach has a lot of redundant code, there are likely ways to streamline it, but the option label displayed does need to be done one at a time.
I have a problem because in italian you use the apostrophe and I suppose this is interpreted as a single quotation mark from the system. This is what I wrote:
function introduzione_meta_box_render( $post ){
// Get the number and display it in a numeric field
$introduzione = get_post_meta( $post->ID, "function_camere", true );
echo '<div><select name="function_camere">';
$i = __('L'appartamento si compone di:','sacconicase');
echo '<option value="L'appartamento si compone di:" '. selected( $saved, 'L'appartamento si compone di:', false ) .' >'.$i.'</option>';
$i = __('La casa si compone di:','sacconicase');
echo '<option value="La casa si compone di:" '. selected( $saved, 'La casa si compone di:', false ) .' >'.$i.'</option>';
}
echo '</select></div>';
I cant avoid writing “L’appartamento”
Only the “straight”(') apostrophe is seen as a quote. You can use the “curly” kind (’) and it will not be seen as a quote. Or use the equivalent entity code & rsquo; (remove the space after the & that I had to insert to prevent the forum’s parser from converting it into a ’)
Another way to use a straight apostrophe in code is to use double quotes:
echo "L'appartamento";
If you need a double quote to be seen as a character in a double quoted string, it can be escaped:
echo "He replied \"Nuts\".";
If I do
function introduzione_meta_box_render( $post ){
// Get the number and display it in a numeric field
$introduzione = get_post_meta( $post->ID, "function_camere", true );
echo '<div><select name="function_camere">';
$i = __(''L'appartamento si compone di:'','sacconicase');
echo '<option value="L'appartamento si compone di:" '. selected( $saved, 'L'appartamento si compone di:', false ) .' >'.$i.'</option>';
$i = __('La casa si compone di:','sacconicase');
echo '<option value="La casa si compone di:" '. selected( $saved, 'La casa si compone di:', false ) .' >'.$i.'</option>';
}
echo '</select></div>';
with
$i = __(''L'appartamento si compone di:'','sacconicase');
PhpChecker tells me it’s an error
Use one double quote character, not two single quote characters.
$i = __("L'appartamento si compone di:",'sacconicase');
I still have a problem with
$i = __("L'appartamento si compone di:",'sacconicase');
inside
function introduzione_meta_box_render( $post ){
// Get the number and display it in a numeric field
$introduzione = get_post_meta( $post->ID, "function_camere", true );
echo '<div><select name="function_camere">';
$i = __("L'appartamento si compone di:",'sacconicase');
echo '<option value="L'appartamento si compone di:" '. selected( $saved, "L'appartamento si compone di:", false ) .' >'.$i.'</option>';
$i = __("La casa si compone di:",'sacconicase');
echo '<option value="La casa si compone di:" '. selected( $saved, 'La casa si compone di:', false ) .' >'.$i.'</option>';
}
echo '</select></div>';
Your syntax is breaking on the echo line because you are not using quotes properly. You need to escape quotes when you have nested quotes:
function introduzione_meta_box_render($post)
{
// Get the number and display it in a numeric field
$introduzione = get_post_meta($post->ID, 'function_camere', true);
echo '<div><select name="function_camere">';
$i = __("L'appartamento si compone di:",'sacconicase');
echo '<option value="L\'appartamento si compone di:" ' . selected($saved, "L'appartamento si compone di:", false) . ' >' . $i . '</option>';
$i = __('La casa si compone di:', 'sacconicase');
echo '<option value="La casa si compone di:" ' . selected($saved, 'La casa si compone di:', false) . ' >' . $i . '</option>';
echo '</select></div>';
}
Or store the value in a variable and use that variable if you are getting confused:
function introduzione_meta_box_render($post)
{
// Get the number and display it in a numeric field
$introduzione = get_post_meta($post->ID, 'function_camere', true);
$original_text = "L\'appartamento si compone di:";
echo '<div><select name="function_camere">';
$translated_text = __($original_text, 'sacconicase');
echo '<option value="' . $original_text . '" ' . selected($saved, $original_text, false) . ' >' . $translated_text . '</option>';
$original_text = 'La casa si compone di:';
$translated_text = __($original_text, 'sacconicase');
echo '<option value="' . $original_text . '" ' . selected($saved, $original_text, false) . ' >' . $translated_text . '</option>';
echo '</select></div>';
}