Closed onnimonni closed 8 years ago
I imagine you can just use WordPress' default i18n functions: __('text to translate', 'text-domain') and _e('text to translate', 'text-domain')
I meant loading the .mo
and .po
files in the standard WordPress way felt really ugly compared how well you had designed herbert framework otherwise.
And it seems that I can't use
__('text to translate', 'text-domain')
in the twig
templates?
Not sure if this is the "right" way to do it (I'm not the framework author - just have been playing around with it a bit), but I've been passing the translated strings to the view like so:
(in the controller method)
return view( '@Plugin/view.twig',
[
'title' => __( 'Some Settings', 'plugin-slug' ),
'formAction' => $form_action,
'settingsTitle' => __( 'Some Other Settings Title', 'plugin-slug' )
]
);
Hello Guys, Just checked this and look like not working. Did you use a special "include" or "use" at the top of the controller? Did you simply add the .po and .mo file in the /languages folder in the root of the plugin? Thanks for your help
I added translations like this in one project.
plugin.php
:
<?php
/**
* @wordpress-plugin
* Plugin Name: Example plugin
* Plugin URI: http://plugin-name.com/
* Description: Plugin description
* Version: 1.0
* Author: Me
* Author URI: http://author.com/
* License: MIT
* Domain Path: /languages
*/
/*
* Translations
*/
add_action( 'plugins_loaded', 'my_herbert_plugin_load_textdomain' );
function my_herbert_plugin_load_textdomain() {
load_plugin_textdomain( 'my-text-domain', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/vendor/getherbert/framework/bootstrap/autoload.php';
and put *.mo
and *.po
files in languages
folder in your project.
We haven't yet discussed the 'herbert way' to add translations but for now this works. And after this you can use __('text','domain');
functions. Please share your experience, for example I didn't need to use translations in twig templates at all, so I have no experience about that.
Thank you so much it's working!
In case anyone is still experiencing issues with translating Herbert, there is a way to implement the native WordPress i18 locale translation so you can use translation plugins.
Since you cannot access php functions defined out of Twig, you need to create the function and make it accessible in Twig environment.
Add the following in your plugin.php
<?php
/**
* @wordpress-plugin
* Plugin Name: Example plugin
* Plugin URI: http://plugin-name.com/
* Description: Plugin description
* Version: 1.0
* Author: Me
* Author URI: http://author.com/
* License: MIT
* Domain Path: /languages
*/
/*
* Translations
*/
add_action( 'plugins_loaded', 'my_herbert_plugin_load_textdomain' );
function my_herbert_plugin_load_textdomain() {
load_plugin_textdomain( 'my-text-domain', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/vendor/getherbert/framework/bootstrap/autoload.php';
// Get the booted Twig
$twig = herbert()->twig;
// Create function _e that calls the native WordPress _e when used
$function = new Twig_SimpleFunction('_e', function($string, $text_domain) {
return _e($string, $text_domain);
});
// Add the created function to the Twig Environment
$twig->addFunction($function);
Now you will be able to use it in your .twig views.
<div>
<h2>{{ _e('Your string.', 'your-textdomain') }}</h2>
</div>
Hey!
How do you use translations for the plugins made with herbert?