'poedit' translation activation - wordpress

I'm trying to make my wordpress site translation ready, using 'poedit'. I have problems with activating the translation. I want to translate the site only, not wordpress system. I have my .pot, .po and .mo files in a 'languages' folder in my theme directory, I have this code in my functions.php file:
load_theme_textdomain( 'example', get_template_directory() . '/languages' );
well, after everything is saved and translated in the poedit program, how do I activate it in my site? I am working with a localhost for now.

First, you need to connect the method to an action hook in your theme functions.php:
function load_mytheme_textdomain() {
load_theme_textdomain( 'example', get_template_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'load_mytheme_textdomain' );
Then you have to use translation instead of echo for output:
_e( 'My Text', 'example' );
Read more here: https://developer.wordpress.org/themes/functionality/internationalization/

Related

How to add custom javascript to Wordpress Admin using a plugin

I am setting up a new Wordpress plugin with a custom metabox. This box contains sets of fields that I store in an array. I want the user to be able to add a new set of fields by clicking a button on the admin-page.
I read different posts on how to accomplish this but I havent succeeded yet. For testing purposes I created a real simple setup in the metabox to change a text of a element. This is also not working so I think it is a problem with loading the scripts correctly.
So what I did so far:
- add action through admin_enqueue_scripts
- register the script using wp_register_script
- enqueue script using wp_enqueue_script
- setup the js file (choose for testpurpose to store it in same dir as the plugin
function amai_woordjes_scripts() {
wp_register_script( 'amai_woordjes_updaten', 'amai_woordjes_updaten.js', array( 'jquery' ), '1.0', true );
wp_enqueue_script( 'amai_woordjes_updaten' );
}
add_action( 'admin_enqueue_scripts', 'amai_woordjes_scripts' );
//HTML code I use in the function which is called by add_meta_box
echo '<p id="demo">Current text</p>';
echo '<button id="woordje_toevoegen" onclick="woordjesToevoegen();">Woorden toevoegen</button>';
//amai_woordjes_updaten.js
<script>
function woordjesToevoegen() {
document.getElementById("demo").innerHTML = "New texxt";
}
</script>
You need use function wp_enque_script(). You use incorrect script path.
plugin_dir_path( __DIR__ ) . path/to/script.js
First You have to set the file path
Create js folder in your plugin root directory and move the 'amai_woordjes_updaten.js' file in js folder
function amai_woordjes_scripts() {
wp_register_script( 'amai_woordjes_updaten', plugin_dir_url( __FILE__ ).'js/amai_woordjes_updaten.js', array( 'jquery' ), '1.0', true );
wp_enqueue_script( 'amai_woordjes_updaten' );
}
add_action( 'admin_enqueue_scripts', 'amai_woordjes_scripts' );

wordpress custom plugin translation file not working

I have a plugin with name "keydesign-addon" and I created file translation in my language and file loaded with below code:
add_action( 'plugins_loaded', 'keydesign_addon_load_textdomain' );
function keydesign_addon_load_textdomain() {
load_plugin_textdomain( 'keydesign', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
but plugin translation not working and it doesn't translate any words.
textdomain is "keydesign" and words are like this
esc_html__("App gallery", "keydesign")
I used "keydesign-addon-fa_IR.mo" and "fa_IR.mo" for file name but not working
also I installed "loco translate" to translate plugin but again not working

Applying translation to custom WordPress plugin

I'm trying to apply a translation to my cusom plugin. I've alredy created brau-ru_RU.mo and brau-ru_RU.po files. My translation domain is 'brau'.
What I've tried is to put files in wp-content/languages/plugins/ and execute this code in my plugin.
$domain = 'brau';
$mo_file = WP_LANG_DIR.'/plugins/'.$domain.'-'.get_locale(). '.mo';
var_dump(load_textdomain( $domain, $mo_file ));
var_dump(load_plugin_textdomain( $domain ));
var_dump(__('This is the test', 'brau'));
Result is:
bool(true) bool(true) string(16) "This is the test"
I also got this code in my config
define ('WPLANG', 'ru_RU');
The text shoud be translated from English to Russian, but it's not. What am I missing?
This is a link to test version of the plugin: https://github.com/Brezgalov/brezgalovauth
If you want to add language files to your plugin then you can create a folder called languages inside your plugin folder and in the plugin main file you can define like this
// Localiztion with language files
function custom_langs_i18n() {
load_plugin_textdomain( 'textdomain', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
add_action( 'init', 'custom_langs_i18n' );
Please remember to use textdomain where you are using string.

Wordpress - Childtheme language Translation

I'm really really stuck here so I'd appreciate some help from above...
I'm using the jkreativ theme and am currently trying to translate my child-theme.
I am using this code in my childthemes functions.php:
<?php
/**
* Setup My Child Theme's textdomain.
*
* Declare textdomain for this child theme.
* Translations can be filed in the /languages/ directory.
*/
function my_child_theme_setup() {
load_child_theme_textdomain( 'jkreativ-child', get_stylesheet_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'my_child_theme_setup' );
?>
In my Childthemes Folder I have a folder called "languages" that contains the
de_DE.mo
de_De.po
files.
I looked in the wordpress codex and to me this seems to be the proper way.
But no single line gets translated...
Any ideas anyone?
Thanks!
Paul
There is a post at wordpress.stackoverflow.com with this problem. Basically what you have to do is
Create a folder inside your languages one with the name of your parent theme;
Place there the .mo file that will overwrite the parent theme translations;
Change your code like this:
function my_child_theme_setup() {
load_theme_textdomain( 'jkreativ', get_stylesheet_directory() . '/languages/jkreativ' );
load_child_theme_textdomain( 'jkreativ-child', get_stylesheet_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'my_child_theme_setup' );
Calling load_theme_textdomain and passing the parent theme domain will do the trick.
Hope it helps!

Change language only on theme

I want to keep the admin in english but the theme in another language. By adding language files to the theme directory nothing happens, the option to switch language wont come up. Is this possible without adding languages to wp-content?
I found a solution using the locale filter and load theme textdomain
The solution looks like this:
define('LOCALE', 'en');
add_action( 'after_theme_setup', 'site_setup' );
function site_setup() {
//Make the theme available for translation
load_theme_textdomain( 'mytheme', get_template_directory() . '/languages' );
$locale = get_locale(); //Will get what you defined earlier
$locale_file = get_template_directory() . "/languages/$locale.php";
if ( is_readable( $locale_file ) ) require_once( $locale_file );
}

Resources