Hi, today we will be working on custom strings localization. You will be able to localize your links, image urls, widget titles or another text information on your site.

Localization of custom strings

Use stella_translate_string function to localize strings on the site depending on language.
NOTE: requires Stella 1.3

Usage

stella_translate_string( $filter_name, $original_string, $translations_array );

Parameters

$filter_name
Name of filter, which will be localized.

$original_string
String Value.

$translations_array
Array with translations. In keys – language code ( en, de, ru, etc ). In values – corresponding translation.

Examples

First example will be about image source. Imagine you have some picture in your theme. You may have something like this:

 <?php $template_uri = get_template_directory_uri();
       echo "<img src='$template_uri/images/flower.png'/>";
 ?> 

 

Ok, you want another picture, for instance, in Russian and German languages. First, add filter to picture filename:

 <?php $template_uri = get_template_directory_uri();
       $picture_filename = apply_filters( 'picture_filename', 'flower.png' );
       echo "<img src='$template_uri/images/$picture_filename'/>";
 ?> 

Second step: use stella_translate_string function.
Add this code to functions.php in your theme:

add_action( 'stella_init', 'stella_functions' );
function stella_functions(){
    stella_translate_string( 'picture_filename', 'flower.png', array( 'ru'=>'flower-ru.png', 'de'=>'flower-de.png', ) );
} 

That’s all! You got localized picture.

Now let’s look at another example.
In Stella 1.3 we have added filters to language names, so you can localize them. Imagine you have two languages: English and Russian. Lets make Stella language switcher widget more friendly. Add this code to functions.php in your theme:

add_action( 'stella_init', 'stella_functions' );
function stella_functions(){
    // Here we tell stella to localize language name 'English'
     stella_translate_string( 'stella_lang_name', 'English', array( 'en'=>'English', 'ru'=>'Английский', ) );
    // .. and here to localize language name 'Russian'
     stella_translate_string( 'stella_lang_name', 'Russian', array( 'en'=>'Russian', 'ru'=>'Русский', ) );
    // ... and widget title
     stella_translate_string( 'stella_language_widget_title', 'Language', array( 'en'=>'Select language','ru'=>'Выберите язык', ) );
} 

That’s how the switcher looks in two languages:
language switcher on different languages

NOTICE: You can use stella_translate_filter ( do not need to pass $original_string value ) instead stella_translate_string since 1.3.48!

I hope you will find this tool very useful. Please send us your suggestions. Thanks ;)