DevinVinson / WordPress-Plugin-Boilerplate

[WordPress] A foundation for WordPress Plugin Development that aims to provide a clear and consistent guide for building your plugins.
http://wppb.io
7.67k stars 2.25k forks source link

Plugin name as text domain in template? #277

Closed pnpetroff closed 8 years ago

pnpetroff commented 9 years ago

I'm creating a plugin that has a Template integrated in which I have some translatable strings. However, I can't see how can I pass the $plugin_name variable in the text domain as it's reachable only in a Class.

I can write it as a string, but that destroys the whole concept of the OOP.

What is the best way that I can achieve this?

DevinVinson commented 9 years ago

How are you integrating the template?

pnpetroff commented 9 years ago

I've created a template for that is used as a page for a custom post type.

In class-plugin-name-public.php

public function single_cpt_page() {

        global $post;

        if ($post->post_type == 'custom_post_type') {
            Plugin_Name_Helper::get_template_part('single-custom-post-type');
        }

    }

get_template_part is my custom funcion in public/partials/class-plugin-name-helper.php that is similar to the native Wordpress one.

And public/partials/templates/single-custom-post-type.php is the page that is used for the single custom post type page. In that file is the string that I want to pass the text domain to.

pnpetroff commented 9 years ago

No one? :(

DevinVinson commented 9 years ago

Do you have a repo I can look through? I'm not getting the whole picture.

In general the partials folder is for small html elements so I'm thinking maybe there is a disconnect somewhere on how things are being tied together.

cjbarnes commented 9 years ago

If you're planning to use the plugin name as the text domain for one of WordPress's translation functions, you should always write it as a string every time, never store it in a variable.

So don't do this:

<?php $translated_text = __( 'string', $plugin_name ); ?>

Do this instead:

<?php $translated_text = __( 'string', 'mytextdomain' ); ?>

The reason for this is because translation software has to parse your plugin's PHP code to look for translatable strings, and it can only understand string literals, it can't understand PHP variables. See 'Examples' heading on this WordPress Codex page: Function reference: __().

Sorry if I've misunderstood what you wanted to do! Not a direct answer to your question, but I hope it's helpful anyway.

slushman commented 9 years ago

Yikes, Chris! I've never run across that before, but reading Otto's article it makes sense. This so would require some major changes to the boilerplate since the translation string is used all over the place as a variable.

grappler commented 9 years ago

It was discussed before https://github.com/DevinVinson/WordPress-Plugin-Boilerplate/issues/59 and a variable as a text domain does not make any difference though I now think best for the text domain to be a string. It is not like the text domain is going to be changed constantly and length of the text domain and variable is not much different. By using a string one ends up with less code.