Translatable strings from child theme not included in translation files - wordpress

So I think that I have all the configuration for translation set, but my translatable strings are not loaded to .po or .pot.
In functions.php:
function opportune_child_setup() {
load_child_theme_textdomain( 'opportune', get_stylesheet_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'opportune_child_setup' );
In style.css:
/*
[...]
Text Domain: opportune
*/
Translatable string (example):
<label for="custom_field"><?php _e( "Company Tax ID", 'opportune' ) ?></label>
Translation files directory:
themes/opportune-child/languages/opportune.pot
themes/opportune-child/languages/pt_PT.mo
themes/opportune-child/languages/pt_PT.po
The .po and .mo files were created with Poedit based on opportune.pot in the parent theme (which is located in equivalent directory:)
themes/opportune/languages/opportune.pot
I've even hardcoded in my wp-config.php (although already set in WP Admin):
define('WP_LANG', 'pt_PT');
What I do to see if the string has been loaded to either .po or .pot is: I go to a page with translatable strings, make hard refresh (deleting cache), download .po and .pot and then search for the string. None of the strings have ever been loaded.
I've used Loco Translate plugin, I think with the right configurations, and still no result.
What am I missing? Thank you so much!

Firstly, child themes should not use the same text domain as the parent theme.
If you're only using strings from the parent theme, then just translate the parent. But if you're adding new strings into your own PHP files, then use a separate text domain.
So if "Company Tax ID" is a string you've added and it isn't in the parent, ensure it's in a text domain matching the child theme folder, i.e. "opportune-child".
Secondly, strings aren't automatically "loaded into po files" as you say. You create your .pot file by extracting strings from your PHP files. Then you base your .po files from that. Read about WordPress internationalization to understand this process.
Don't create files "based on opportune.pot" because the parent theme is a separate entity. You only need to extract and load strings you've defined for your child. Let the parent take care of itself.
There is a guide for translating child themes here:
https://localise.biz/wordpress/plugin/child-themes
Full disclosure: I am the author.

Related

Wordpress child theme translation

I'd like to translate a child theme of wordpress twentythirteen theme.
Following the documentation, I've created and located fr_FR.po and fr_FR.mo files in a languages subdirectory of the child theme. The translation files contain only the child theme's specific translations. Then I've added the following in functions.php:
function theme_vja_setup() {
load_child_theme_textdomain('twentythirteen',
get_stylesheet_directory()."/languages");
}
add_action('after_setup_theme','theme_vja_setup');
I can't get what I'm doing wrong but I can't get the child theme translated
Looking at wordpress trac:
load_child_theme _text_domain verifies that $path is not empty and then call load_theme_text_domain.
load_theme_text_domain in turn tries to load the parent localization file wp-content/languages/themes/twentythirteen-fr_FR.mo which exists --> the function load_text_domain returns true and the loading stops
Any direction would be appreciated, I'm going crazy.
You don’t need this code in functions.php. Copy the .po and .mo files in the wp-content/languages/themes folder.
Name them:
[yourchildthemename]-fr_FR.po
and
[yourchildthemename]-fr_FR.mo.
(replace [yourchildthemename] by the slug of your child theme)
Regards Tom

Wordpress: add new strings (and translate them) on child theme

I have a parent theme and I did create a child theme.
I did put a folder inside child theme /lang that overwrite parent and all went done. I did have my theme translate in italian and english.
Now I need to add a field to the registration form, so I use this code:
function tre_custom_woocommerce_register_form () {
?>
<p><label class="woocommerce-form__label woocommerce-form__label-for-checkbox inline">
<input class="woocommerce-form__input woocommerce-form__input-checkbox" name="privacy_mandatary" type="checkbox" id="privacy_mandatary" /> <span><?php _e('Autorizzo il trattamento dei miei dati, come riportato alla lett. A punti 1, 2 e 3 della policy privacy', 'mychildtheme-child'); ?></span>
</label></p>
So basically I did "create" another language domain (mychildtheme-child) different from parent.
In functions.php I did use this snippet:
function mychild_load_languages() {
load_child_theme_textdomain( 'mychildtheme-child', get_stylesheet_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'mychild_load_languages' );
And in the new folder called /languages/ (different from the other) I did put my .pot and the translations. But translates are not called, I have the theme only italian.
So, summarizing:
I have the parent theme with folder lang (it works)
I have the child with folder lang (it works). When I swith to English, I got all text translated (but not the new)
I have the child with folder languages (together with previous point) doesn't work.
According to the documentation of load_child_theme_textdomain, you need to also make sure you have a .mo file in your child themes root directory as well. Then you also need to ensure you have .po and .pot language files present in your language folder.
Also, you need to use the same text domain as the parent theme and not create a new text domain for your child theme. According to this comment in the documentation, whilst the translations can be in your child theme, it needs to imitate the parent theme as it's treated as one theme.
Excerpt from that comment:
function wpdocs_child_theme_setup() {
load_child_theme_textdomain( 'my_parent_theme', get_stylesheet_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'wpdocs_child_theme_setup' );
‘my_parent_theme’ = The name of the Main theme
The .mo files must use language-only filenames, like languages/de_DE.mo in your child theme directory.
For compatibility, make sure the languages folder is called the same as the parent themes language folder (/lang in your case) and the .mo, .po and .pot files are in a valid format.

how do i change the woocommerce_sale_flash part on a multilanguage site?

I'm building a multilingual site with WP using Woocommerce.
The client asked me to change the Sale Flash text, after some research i found this code
add_filter('woocommerce_sale_flash', 'avia_change_sale_content', 10, 3);
function avia_change_sale_content($content, $post, $product){
$content = '<span class="onsale">'.__( 'Sale custom text!', 'woocommerce' ).'</span>';
return $content;
}
It works just fine, but it also changes the german and spanish text - replacing it with the english one.
I checked at the WPML strings (hoping to translate it through that system), but couldn't find it there.
Does anyone know what filter i would have to set to create custom flash sale text in english, spanish and german
Thanks
You are replacing the text correctly, however the issue is that the new string is not found in the language files provided by WooCommerce. The plugin will load a domain called "woocommerce" which is then used to locate strings and their translations if necessary. You are passing the text into __() (more info on __()', '_e(), _x(), _n()), but because your text is new, it isn't found in the dictionary provided by WooCommerce so it defaults to English.
// the __() function can't find 'Sale custom text' in the 'woocommerce' text domain,
// so it does not translate.
__( 'Sale custom text!', 'woocommerce' )
To have this text translate, you will need to load your own language files and text domain and pass the text through __() using your custom domain.
The short version of how to do this:
Use a custom text domain
// load text from 'my_text_domain'
__( 'Sale custom text!', 'my_text_domain' )
Download and install PoEdit to create *.po language files. It has options to scan PHP files for translations entries and will add them to your dictionary. There are tutorials on this on the Internet.
Create one translation file per language that you want to support
Create a folder called languages under your theme directory (this should be a Child Theme so that updates don't overwrite your work), or plugin directory if you are doing your customizations that way.
Update your functions.php (or other plugin, etc) to load your custom text domain. Make sure to use the appropriate path, this assumes it's in the theme.
function load_textdomain_my_text_domain() {
$language_dir = get_template_directory() . '/languages/';
load_plugin_textdomain( 'my_text_domain', false, $language_dir );
}
add_action( 'plugins_loaded', 'load_textdomain_my_text_domain' );
An example of doing this in a plugin using one I wrote:
Spanish language files create by PoEdit under the /languages directory.
Loading the language files under a custom text domain.
Text being passed through __() using the custom text domain.

Trouble Translating a WordPress theme

I've created an Arabic website and I am using the zippy theme from MageeWP the things is that I am trying to translate some strings in the theme, but I don't seem to get it working well.
In the theme functions file, there is a specification for the folder location for .PO and .MO files
I created a file ar.mo and put it there, but it still not working, can anybody guide to a clue?
I checked in wp_config.php and indeed the WPLAND is set to ar
I also used the CodeStyling plugin to make the translation, and it didn't work, although the plugin does not problem a .po or .mo files for ONLY ar.
Additionally
the source code of the zippy theme is here: http://themes.svn.wordpress.org/zippy/1.0.9/
Looks like the theme is doing it wrong (simplified code):
define( 'ZIPPY_THEME_BASE_URL', get_template_directory_uri() );
$lang = ZIPPY_THEME_BASE_URL. '/lang';
load_theme_textdomain( 'zippy', $lang );
It's passing an URL and it should be a path, change the load_theme_textdomain line to:
load_theme_textdomain( 'zippy', get_template_directory() . '/lang');
I had a look at the Zippy theme. There are a template called en_US.po inside the lang folder. I had a look at that as well.
If you don't have poedit installed, download poedit now and install it on your computer. Now, make a copy of en_US.po and rename it ar.po. Open ar.po with poedit. Now you can do all your translations in this template. When you're done, just click save in poedit. Poedit will automatically create a ar.mo template when your ar.po is saved.
Note, this is just a quick way of doing it, as there are already a language file available that isn't yet translated.
Hope this helps

Wordpress gettext bilingual language switching for a theme

When using gettext in Wordpress to create a bilingual theme, do we have to create one .php file for every page twice (English, French)? For example, header.php, header-fr.php, sidebar.php, sidebar-fr.php, taxonomy-types.php, taxonomy-types-fr.php, etc. Sidebar-fr.php would then use _e('Text to translate', 'domain'). Is there a way to simply keep one copy of all php files and to switch the locale?
Thank you.
Create languages folder inside the theme directory, that's where you save language files, e.g. ar.po (for Arabic).
When writing strings in the code use the following (I usually use English text as the default text, so I won't need to create extra language file, and for each other language create files):
<?php echo __('This is a test','my_theme_name'); ?>
<?php _e('This is a test','my_theme_name'); ?>
in functions.php in the theme folder add the following code to load the language file (please note my_theme_name as given above):
add_action( 'after_setup_theme', 'my_theme_setup' );
function my_theme_setup(){
load_theme_textdomain('my_theme_name', get_template_directory() . '/languages');
}
This way you create only one file(header.php, index.php, etc), where strings are easy to translate.
[Note: this answer assumes that you have knowledge in how to create gettext translation files]

Resources