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

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.

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

Translatable strings from child theme not included in translation files

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.

How do I check for child theme files first using include, before going to parent theme?

I'm creating a WordPress child theme based on an existing parent theme, and I'd like to have any same-named file I put in my child theme directory take priority over the file in the parent theme directory. I thought this was how parent/child theming was set up in WP but I have hit a bump.
According to the WordPress codex on Child Themes, it says:
Template Files
If you want to change more than just the stylesheet,
your child theme can override any file in the parent theme: simply
include a file of the same name in the child theme directory, and it
will override the equivalent file in the parent theme directory when
your site loads.
In one of my files (header.php), there is an include that looks like this:
include get_parent_theme_file_path("folder/file.php")
Even though I have a duplicate-named-but-modified version of that file.php in my child theme, it still uses the version in my parent theme. According to the same codex, their recommendation for targeting a child theme file specifically is to use get_stylesheet_directory(), so it would look like this:
include (get_stylesheet_directory()."/folder/file.php");
I understand that the purpose of a function called "get_parent_theme_file_path()" is to ignore the parent/child relationship and just get the parent theme version, so without replacing that with a function that explicitly gets a file in my child theme (ie. get_stylesheet_directory), is there a way I can have some sort of universal get_path() function that checks for child first, if it doesn't exist, get parent version?
By the way, I read this Q&A on "get_parent_theme_file_path vs. get_template_directory", but their solution was to use parent_theme_file filters, but that isn't dynamic, and would require me to write a filter function for every child file I want it to use.
Thanks for your help.
Have you tried something like this:
add_filter( 'parent_theme_file_path', function( $path, $file ) {
return get_stylesheet_directory() . '/' . $file;
}, 10, 2 );
This should override the parent theme path without the need to write a function for every file.
If you want to make sure get_parent_theme_file_path() still works for parent theme files that are not overriden in your child theme, you could do a simple check:
add_filter( 'parent_theme_file_path', function( $path, $file ) {
if ( !file_exists( get_stylesheet_directory() . '/' . $file ) ) {
return $path;
}
return get_stylesheet_directory() . '/' . $file;
}, 10, 2 );
Someone on wordpress.stackexchange pointed out the use of locate_template, which retrieves the name of the highest priority template file that exists.
Searches in the STYLESHEETPATH before TEMPLATEPATH and wp-includes/theme-compat so that themes which inherit from a parent theme can just overload one file.

Language file not loading - Wordpress Child Theme

I'm trying to add a Dutch translation to a Wordpress theme.
I have edited .pot file (inside the theme) with POedit, and saved nl_NL .po and .mo (without any issues).
Naturally, I've made languages directory in my childtheme (called charityhub-child).
I have changed my code in the most basic form (taken from Wordpress Code reference).
My functions.php looks like this:
function wpdocs_child_theme_setup() {
load_child_theme_textdomain( 'charityhub', get_stylesheet_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'wpdocs_child_theme_setup' );
Parent theme name = charityhub.
What seems to be the problem here ? Language files are not loading...

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