Support » Plugin: Admin Columns » Translate labels

Viewing 15 replies - 1 through 15 (of 16 total)
  • Plugin Author Stefan van den Dungen Gronovius

    (@dungengronovius)

    Label should be translatable in combination with the WPML plugin but it probably does not work with the qtranslate-x plugin.

    We’ve got a build-in filter which you could use to translate the labels in your own way:

    public function cpac_translate_label_example( $label, $column_name, $column_options, $storage_model ) {
    
      $label = __( $label, 'custom-text-domain-with-variables');
      return $label;
    }
    add_filter( 'cac/headings/label', 'cpac_translate_label_example', 10, 4 );

    In this example I use a gettext function but you cannot parse this of course so you have to manually manage te pot/mo files. You could also write your own function or try to see if qtranslate-x has a function you can use.

    Thread Starter lingenChan

    (@lingenchan)

    Thanks,

    i’m trying but with no luck, because it changes all column labels with the same translation. How can i change labels separately?
    Here’s my code:

    function cpac_translate_label_trans() {
    if(qtrans_getLanguage()==’es’) {
    $column_name = ‘title’;
    $label = __( Título, ‘codepress-admin-columns’);
    }
    return $label;
    }
    add_filter( ‘cac/headings/label’, ‘cpac_translate_label_trans’, 10, 4 );

    Plugin Author Stefan van den Dungen Gronovius

    (@dungengronovius)

    In your code you always translate the same label.
    Please do not use the ‘codepress-admin-columns’ textdomain because this is reserved for strings in our plugin only. Use you own text domain and use the variable $label for the translation just as in my example.

    __( $label, 'your-own-textdomain' );

    The problem is that a parser like poedit will not find any translation because it’s a variable.
    You have to keep track of your own strings/variables and put them in a pot file yourself and translate them. Or maybe qtranslate has a replacement for __() that you can use. Otherwise I don’t know a way on keeping track on your variables.

    Thread Starter lingenChan

    (@lingenchan)

    Hi again,

    thanks. I think the code is working because i can translate the label in the languages i have in my site. The problem is what you said, its only translating the same label. My question now is: how can i decide the label to translate.

    I don’t think i need to use Poedit because as i sadi the translation is working but for all the labels,i only need to know how to apply it to each label.

    Regards,

    Plugin Author Stefan van den Dungen Gronovius

    (@dungengronovius)

    Please look at my first example.
    The first parameter ($label) is the original value that goes into this function. The filter is called for each label. Just put the the same variable in the translation string or check to alter.

    Instead of using:
    $label = __( Título, 'codepress-admin-columns');
    use:
    $label = __( $label , 'codepress-admin-columns');

    And be sure that you put the parameters in your function just as I did in my example. You have 4 parameters ($label, $column_name, $column_options, $storage_model)

    Thread Starter lingenChan

    (@lingenchan)

    ok, i understand you meanto alter the code but I am looking for a filter to put in function.php. I don’t wanna alter the code of the plugin if possible.

    Could you please help me?

    Thread Starter lingenChan

    (@lingenchan)

    No other ways of doing that? maybe just altering the HTML output?

    simple fiter to paste to functions.php..

    Regards,

    Plugin Author Stefan van den Dungen Gronovius

    (@dungengronovius)

    You can literally paste my first example to your own functions.php

    function cpac_translate_label_example( $label, $column_name, $column_options, $storage_model ) {
    
      $label = __( $label, 'custom-text-domain-with-variables');
      return $label;
    }
    add_filter( 'cac/headings/label', 'cpac_translate_label_example', 10, 4 );

    Only change the textdomain to your theme textdomain and be sure that the variable that goes into the gettext function is somewhere in your po file or is translated somewhere in your theme. If not, than you have to add that manually.

    Example: If you have added a columns with label set to ‘My Custom Label’, it goes to your filter as
    __( 'My Custom Label', 'your-theme-text-domain' );

    If this string is in your translation file, it shows the translated text, otherwise it shows the untranslated label.

    Thread Starter lingenChan

    (@lingenchan)

    Ok i will do exactly what you suggested but how can i add strings to translation files? i think this is not easy to do…

    Plugin Author Stefan van den Dungen Gronovius

    (@dungengronovius)

    Does your template have a translation file and are you keeping this file up to date with a tool like poedit?

    If yes, you can add some placeholders in your functions.php just so your parser will recognize the strings that has to be translated, like this:

    // Some placeholder functions so you can parse this to po files
    __( 'placeholder_a', 'your-text-domain' );
    __( 'placeholder_b', 'your-text-domain' );
    __( 'placeholder_c', 'your-text-domain' );

    Of course you’ll have to use the same labels als in your theme. (I know this is not optimal, but I don’t another way to do so).

    If you do not scan your code for translation string in your theme, you can add the strings yourself with a tool like poedit. Just add the label manually to the po file and translate it.

    Thread Starter lingenChan

    (@lingenchan)

    Hi again,

    i’m trying to make this part of the WP translatable (the file is here: File: wp-admin/includes/class-wp-posts-list-table.php

    /* translators: manage posts column name */
    505	        $posts_columns['title'] = _x( 'Title', 'column name' );
    506

    That’s the part corresponding to the admin column type: title, name: title.

    Could you please tell me what can i do exactly to translate this label?

    I tired many combinations and i spent several hours with no luck.

    Regards,

    Plugin Author Stefan van den Dungen Gronovius

    (@dungengronovius)

    _x( ‘Title’, ‘column name’ )
    means ‘Title’ is translated in the WordPress base file with context ‘column name’.
    The translation is in the admin-{lang_code}.po file.
    Depending on your language set in WordPress, the correct language file should be already in your /wp-content/languages/ folder and should be already translated.

    So for example if you use a Dutch language in admin (nl_NL) the translation file should be

    /wp-content/language/admin-nl_NL.po

    You can open it in poEdit and save the file to create the mo file. But this is a default WordPress translation file that comes automatically to your install so I think it will be overwrited when you update WordPress or change a language.

    Thread Starter lingenChan

    (@lingenchan)

    Hi again,

    i have 2 languags in my site (es_ES and ca_ES) and i need to have the column name to be translated not only in es_ES but also in ca_CA that’s why i’m trying to filter in some way the strings of the column titles. Now i’m trying with this function but still no luck:

    function admin_column_trad( $translations, $text, $context, $domain ) {
    	if ( qtrans_getLanguage()=='ca' ) {
    			if ( $translations == 'Título' && $text == 'Title' && $context == 'column name' && $domain = 'admin' ) {
    				$translations = 'Títol';
    			}
    		}
    		return $translations;
    }
    add_filter( 'gettext_with_context', 'admin_column_trad', 20, 4 );

    Plugin Author Stefan van den Dungen Gronovius

    (@dungengronovius)

    Hi,

    Sorry but without any context if can’t help you.
    When I look at your function and filter this seems the correct way to do it.
    But you have to debug it yourself.

    1) Does your qtrans_getLanguage() function work?
    2) Are you sure your check for your translation is correct?
    3) Test your return with some extra content so you are sure this filter runs correctly. e.g. return $translations . ‘Passed’;

    Other that this I think this becomes a little off topic from this plugin.

    Thread Starter lingenChan

    (@lingenchan)

    Hi,

    thanks for your support, i finally did it:

    function admin_column_headings( $label ) {
    	if ( qtrans_getLanguage()=='ca' ) {
    		if ( $label == 'Título' ) {
    			$label = 'Títol';
    		}
    		if ( $label == 'Imagen Destacada' ) {
    			$label = 'Imatge Destacada';
    		}
    		if ( $label == 'Categorías' ) {
    			$label = 'Categories';
    		}
    		if ( $label == 'Fecha' ) {
    			$label = 'Data';
    		}
    	}
    	return $label;
    }
    add_filter( 'cac/headings/label', 'admin_column_headings', 20, 1 );

    Best regards,

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Translate labels’ is closed to new replies.