Language strings are not overridden in Joomla - overriding

I am programming a custom template in Joomla 3.3. I need to override some language strings, so I have created a folder called language/templates/mytemplate/language/es-ES/ and inside it, I placed a file called es-ES.custom.ini.
In template index.php file I placed:
<?php
$language =& JFactory::getLanguage();
$result = $language->load('custom' , dirname(__FILE__), $language->getTag(), true);
echo $result;
?>
$result variable is true indicating the file is correctly loaded, but the strings are not actually overridden.
Any help will be appreciated.

If you enable Language Debugging in your admin area:
you'll notice that even if your custom language file is loaded, other language files are loaded later, overriding your strings.
A different approach would be to create a file called JROOT/templates/YOURTEMPLATE/language/overrides/es-ES.override.ini. Then add the following to your templateDetails.xml:
<languages folder="language">
...
<language tag="overrides">overrides/es-ES.override.ini</language>
</languages>
This is described more in detail here.

Related

Single Post Template Override for Custom Post Type

I've got kind of a unique scenario that I'm trying to nail down. I'm working on a new template for a custom post type that already exists. Basically, we're replacing the single-customposttype.php file with a new one. All of that is going swimmingly, except one thing - they have one post in that custom post type that they want to keep the OLD template on.
So there's a NEW single-customposttype.php file that will work as the default single template for that CPT.
But I need ID #93 to use the OLD single-customposttype.php template. I hoped just doing single-customposttype-93.php might do the trick, but it doesn't. What's the best way to apply the other template to only one post id?
Thanks in advance!
I deal with custom template loading all the time, it's really pretty simple! Really, all you need to do is hook into the template_include hook, and override the template based on whatever conditions you want.
That hook takes a single argument, the $template file to load. You can then use any conditionals you want and force a separate file to load instead.
add_filter( 'template_include', 'custom_template_include', 99 );
function custom_template_include( $template ){
// For ID 93, load in file by using it's PATH (not URL)
if( get_the_ID() === 93 ){
// Note the file name can be ANYTHING, the WP auto-template names don't matter here
$file = get_stylesheet_directory() . '/post-id-93-custom-template.php';
// It's generally good to see if the file exists before overriding the default
if( file_exists( $file ) )
$template = $file;
}
// ALWAYS return the $template, or *everything* will be blank.
return $template;
}
It's really that simple! Inside the custom PHP file, you have access to all of the WordPress functions and such as you would with a default template.
Generally you'll want to use the file_exists() function on the template, just to make sure it's found, otherwise you'll be passing along a file that doesn't exist, and that page will not load. By checking if it exists, it will still fall back to the old template if it's not found (deleted/renamed/moved, etc)
Also, you always need to have return $template at the end, otherwise anything that uses WordPress' template system will break.
I made a quick example on a demo site:
https://xhynk.com/content-mask/policies/cookie-policy/
https://xhynk.com/content-mask/policies/use-another-template/
The policies are a custom post type, and the cookie policy loads normally. The other one is modified with the same code as above (with the name/ID changed to match), and it's loading in a simple .php file with that content in it.

Drupal basic page doesn’t seem to use page.tpl.php

Title says it really. Basic pages created in Drupal don’t seem to use the page.tpl.php file as a template.
If I edit the html.tpl.php file, those changes apply to every page, and it causes errors when I load a basic page.
I have also tried to copy the page.tpl.php file and name it page—basic-page.tpl.php to no avail.
Any idea what’s going on?
Also, how do I go about changing the page_top variable to include more content?
Lastly, the default page.tpl.php file has $page variables and things like $page_top and the like.
How would I call the title from the page only and the body text of a page only?
I’m using Drupal 7 and a custom sub theme.
The aforementioned files are in the template folder of the theme and I did clear caches when I added them.
Add $conf['theme_debug'] = TRUE; in settings.php and clear cache, reload page and check view source.
It will display the template file suggestions.
page.tpl.php file common for all pages. Just print anything to the tpl and run any node of basic page as well as other content type page and check if its working or not. If page.tpl.php not working for basic page only, then check your template.php file.
For print a page title just need to use following code:
<?php print $title; ?>
For print body text you need to use following:
<?php print render($page['content']); ?>
This may depend on the theme you are using. But I guess you are actually meaning page--page.tpl.php (double dashes). Which will be taken into account after you added the following snippet to your theme's template.php. Replace MYTHEME with your theme's machine name.
function MYTHEME_preprocess_page(&$variables) {
if (isset($variables['node']->type)) {
// If the content type's machine name is "my_machine_name" the file
// name will be "page--my-machine-name.tpl.php".
$variables['theme_hook_suggestions'][] = 'page__' . $variables['node']->type; // (double underscores)
}
}
See Template (theme hook) suggestions, where I also got above snippet from.
active theme debug for inspecting the template source and you get a different suggestions to user it (just avoid using node/nid).
commend drush to enable theme debug drush vset theme_debug 1

Wordpress Language file is not being loaded

I seem to have a problem loading Wordpress language files into my custom theme.
In functions.php I have the following code in my setup:
load_theme_textdomain( 'theme_textdomain', get_template_directory() . '/langs' );
In my stylesheet I have the textdomein defined:
Text Domain: theme_textdomain
In my theme folder I have a folder /langs with 2 different file types:
en_GB.mo
nl_NL.mo
Default language of my theme is nl_NL.
In one of my templates I use:
<?= __('Zoeken'); ?>
Just to test I added a translation of this in both language files:
For en_GB = search, for nl_NL = zoeken2. However, both nl_NL and en_GB are not being loaded by the theme. What I'm I doing wrong?
I think you need to specify your theme domain in your call to the __() function. I don't think it's picked up automatically from your stylesheet header. So rather than
<?= __('Zoeken'); ?>
try
<?= __('Zoeken', 'theme_textdomain'); ?>

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]

Wordpress: how to create a template that is still available when you switch theme?

I have a custom template for my portfolio page. However, after switching theme, it's gone because it is in my previous theme's folder.
Technically, I can copy that file into my new theme folder. However, I plan to change theme every two weeks and this becomes non-trivial. Is there a way to always have a bunch of common template files available to me no matter when and how often I switch theme? (In other words, I want to create template files that are not dependent on themes.)
Thanks!
There is, using template_redirect, which you would put in the functions.php file.
function uniquename_default_template() {
global $wpdb;
if(get_post_type() == 'posttype') : /* You could use is_single() instead of get_post_type() == '' or any type of conditional tag) */
include(TEMPLATEDIR . 'path/to/theme/file.php'); /* You could use TEMPLATEDIR to get a file from a template folder, or PLUGINDIR to get a file from the plugins directory - doesn't support HTTP requests */
exit; endif;
}
add_action('template_redirect', 'uniquename_default_template');
Hope it helps.

Resources