1

I use the following code to get image sizes:

public function getImageSizes(){
        global $_wp_additional_image_sizes;
        $sizes = array();
        foreach(get_intermediate_image_sizes() as $s){
            $sizes[$s] = array(0, 0);
            if( in_array( $s, array( 'thumbnail', 'medium', 'large' ) ) ){
                $sizes[$s][0] = get_option( $s . '_size_w' );
                $sizes[$s][1] = get_option( $s . '_size_h' );
            } else {
                if ( isset( $_wp_additional_image_sizes ) && isset( $_wp_additional_image_sizes[ $s ] ) )
                    $sizes[$s] = array( $_wp_additional_image_sizes[$s]['width'], $_wp_additional_image_sizes[$s]['height'] );
            }
        }
        return $sizes;
    }

The result is a select with image sizes, something like this:

<option value="thumbnail">Thumbnail</option>
<option value="medium">Medium</option>
<option value="large">Large</option>
<option value="tribe_image_widget_custom" selected='selected'>Custom</option>

The question is how to translate sizes (Thumbnail, Medium, Large and Custom)? By default the sizes are in English, I need them to be another language (I use CodeStyling Localization plugin for it).

Thank you!

P.S.: to biuld a select I use this code:

<select class="widefat" id="<?php echo $this->get_field_id('size'); ?>" name="<?php echo $this->get_field_name('size'); ?>">
    <?php foreach ($this->getImageSizes() as $name => $atts): ?>
        <option value="<?php echo $name ?>"<?php echo ($name==$size?' selected':'') ?>><?php echo ucwords($name) . ' ('. implode( 'x', $atts ). ')' ?></option>
    <?php endforeach ?>
</select>
4
  • $sizes is an array containing the image sizes; how do you build the select options?
    – Sven
    Commented Jul 18, 2014 at 15:35
  • Sven, thank you for your help. I use the following code to build a select:
    – Ksenia
    Commented Jul 18, 2014 at 21:46
  • <select class="widefat" id="<?php echo $this->get_field_id('size'); ?>" name="<?php echo $this->get_field_name('size'); ?>"> <?php foreach ($this->getImageSizes() as $name => $atts): ?> <option value="<?php echo $name ?>"<?php echo ($name==$size?' selected':'') ?>><?php echo ucwords($name) . ' ('. implode( 'x', $atts ). ')' ?></option> <?php endforeach ?> </select>
    – Ksenia
    Commented Jul 18, 2014 at 21:48
  • 1
    I also posted this code above (in the question) with better formatting.
    – Ksenia
    Commented Jul 18, 2014 at 21:53

1 Answer 1

0

You can use __(), _e(), _x() or _ex() and your textdomain to make your theme or plugin translatable.

I stripped your code snippet to the basics, so you will get the idea more easily:

<select class="widefat">
  <?php foreach ( $this->getImageSizes() as $name => $atts ) : ?>
    <option value="<?php echo $name; ?>" ?>>
      <?php echo ucwords( __( $name, 'textdomain' ) ); ?>
    </option>
  <?php endforeach; ?>
</select>

Unfortunately internationalization is not possible with variables like $name so you will have to provide the default image size names to make them accessible:

$sizes = array(
  __( 'thumbnail', 'textdomain' ),
  __( 'medium', 'textdomain' ),
  __( 'large', 'textdomain' )
);

If you're adding the image sizes yourself you can avoid the hack above if adding them like this:

add_image_size( __( 'custom-image-size', 'textdomain' ), $width, $height, $crop );
3
  • 1
    Oh, now I see! Sven, thank you a lot for your kind help!
    – Ksenia
    Commented Jul 19, 2014 at 10:11
  • @Ksenia You're welcome! Please accept the answer to mark this question as solved :)
    – Sven
    Commented Jul 19, 2014 at 10:25
  • 1
    Done! Sorry, this is my first question on Stack Exchange :)
    – Ksenia
    Commented Jul 19, 2014 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.