Actions

Difference between revisions of "Loading extra language files"

From Joomla! Documentation

(New page: ==Overview== Many times, you need to load extra language files in your code. An example is loading your component's language file into a module for that component. Following is the code th...)
 
(JFactory uses singleton pattern so pass per reference operator "&" is not anymore needed)
 
(2 intermediate revisions by one user not shown)
Line 1: Line 1:
 
==Overview==
 
==Overview==
Many times, you need to load extra language files in your code. An example is loading your component's language file into a module for that component. Following is the code that helps you do this
+
Many times, you need to load extra language files in your code. An example is loading your component's language file into a module for that component. This example loads a single language file, and appends the results to the existing language strings. Following is the code that helps you do this
  
 
==PHP Code==
 
==PHP Code==
 
<source lang="PHP">
 
<source lang="PHP">
$lang =& JFactory::getLanguage();
+
$lang = JFactory::getLanguage();
 
$extension = 'com_helloworld';
 
$extension = 'com_helloworld';
 
$base_dir = JPATH_SITE;
 
$base_dir = JPATH_SITE;
 
$language_tag = 'en-GB';
 
$language_tag = 'en-GB';
$lang->load($extension, $base_dir, $language_tag, true);
+
$reload = true;
 +
$lang->load($extension, $base_dir, $language_tag, $reload);
 
</source>
 
</source>
  
Line 15: Line 16:
 
1. $extension - This is the extension whose language file will be loaded
 
1. $extension - This is the extension whose language file will be loaded
  
2. $base_dir - Should be JPATH_SITE in case you have language files stored elsewhere. Defaults to JPATH_SITE [optional]
+
2. $base_dir - Should be JPATH_SITE in case you have language files stored elsewhere. Defaults to JPATH_BASE.  [optional]
  
3. $language - This is the locale string. Language files for this locale will be loaded. Defaults to the one set in backend. [optional]
+
3. $language_tag - This is the locale string. Language files for this locale will be loaded. Defaults to the one set in backend. [optional]
 +
 
 +
4. $reload - Flag that will force a language to be reloaded if set to true. [optional]
  
 
<noinclude>[[Category:Development]][[Category:Languages]]</noinclude>
 
<noinclude>[[Category:Development]][[Category:Languages]]</noinclude>

Latest revision as of 13:10, 10 August 2013

Overview

Many times, you need to load extra language files in your code. An example is loading your component's language file into a module for that component. This example loads a single language file, and appends the results to the existing language strings. Following is the code that helps you do this

PHP Code

$lang = JFactory::getLanguage();
$extension = 'com_helloworld';
$base_dir = JPATH_SITE;
$language_tag = 'en-GB';
$reload = true;
$lang->load($extension, $base_dir, $language_tag, $reload);

Following is an explanation of the variables

1. $extension - This is the extension whose language file will be loaded

2. $base_dir - Should be JPATH_SITE in case you have language files stored elsewhere. Defaults to JPATH_BASE. [optional]

3. $language_tag - This is the locale string. Language files for this locale will be loaded. Defaults to the one set in backend. [optional]

4. $reload - Flag that will force a language to be reloaded if set to true. [optional]