3

Once again I have a question concerning WordPress and PHP. In fact, it is not completly WordPress related, but thinking of it in this context makes it more understandable.

I develop a framework that is supposed to make theme development as easy as possible. Therefore, I want to make a theme translatable automatically. The developer using the framework should not have to use __(...) every time he or she passes a string to somewhere.

The problem is that the PHP gettext functions can't handle variables by design. That really makes sense, since dynamic content cannot be translated and also would require the translation generater to not only parse, but also execute the script file - but it is really limiting in scenarios where your content is somehow fixed (at least as fixed as a string in PHP would be).

Here is an example of what the developer currently has to do:

new EaseWidgetArea(
    __('Notificationbar', 'themedomain'),
    "notificationbar",
    __("Top bar supposed to hold emergency information.", 'themedomain')
);

Here is what I aim for:

new EaseWidgetArea(
    "Notificationbar",
    "notificationbar",
    "Top bar supposed to hold emergency information."
);

In the class-method where the widget-area is finally registered:

register_sidebar(
    array(
        'name' => __($this->name),
        'id' => $this->id,
        'description' = __($this->description),
        'before_widget' => '<div class="widgetarea">',
        'after_widget' => '</div>'
    )
);

Do you know any clean and convenient way to accomplish this? Thanks in advance!

2 Answers 2

1

I see what you are trying to do, but you can't* solve your problem like this:

new EaseWidgetArea(
    "Notificationbar",
    "notificationbar",
    "Top bar supposed to hold emergency information."
);

What you can do however is this:

new EaseWidgetArea(
    __("Notificationbar"),
    __("notificationbar"),
    __("Top bar supposed to hold emergency information.")
);

To do this, you have to locate the __ function, this is an alias of``translate()`. Which is located in l10n.php.

In this, you could automate your translation however you want to, another upside of doing it like this, software from other devs that do use __() will still work.

  • Never say can't right? In this case however, it would mean that you have to run through every single function in Wordpress, this will take you at least 2225 years.
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your suggestion! I thought about this, but, as you noted yourself, this isn't really a solution but more like a compromise. As you said; never say can't! ;-)
1

There might be ways through which it might be possible. For instance extending a class method and then using fetch_all_args, and searching your .po files for that string, and then sending it back to the original method. But these should be avoided, as it might slow down your performance and adding a simple __() is worth it.

But there is one way which might be worth looking into. You might already be using grunt or gulp in your theme development workflow, so you can use it to automatically replace all strings with the localized version of that strings. As you are just using this for theme development so you can just add in the path for theme and make gulp watch for any changes made to any files. Now as soon as you make any changes to any file within that directory, it will automatically replace your string with the localized string. Or you can just do after the whole theme development is done. This way you wont have to do any extra work neither will your servers be doing any extra calculations and your themes will still be using the official functions.

EDIT: Another way would be create a must-have plugin and then use it to automatically check all the strings in the theme folder, then only replace the strings without the wrapper function __(). You can add some hooks to this plugin to automatically run on theme updates or activation. And once the strings are replaced it can also run plugins like LOCO, where the developers/users can translate the strings through the admin panel itself. This way there will be no performance drops and neither will they have to use grunt/gulp in their work flow.

2 Comments

Thank you for your answer, but I think it does not solve my original problem. I want to make development easier for framework-users, not for me. Therefore using grunt or gulp is not really an option. Your first solution might work, but is not the way to go since the huge drop in performance...
Well this is indeed getting interesting :) , Another thought just past my mind, it might not be what you are looking for but you can checkout my edit.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.