Difference between revisions of "Loading extra language files"

From Joomla! Documentation

m (Corrected default value for $base_dir)
m (removed Category:Needs review using HotCat)
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
==Overview==
+
<noinclude><languages /></noinclude>
 +
 
 +
<translate>
 +
==Overview== <!--T:1-->
 
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
 
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
 +
</translate>
 +
 +
<translate>==PHP Code== <!--T:2--></translate>
  
==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;
Line 12: Line 17:
 
</source>
 
</source>
  
 +
<translate>
 +
<!--T:3-->
 
Following is an explanation of the variables
 
Following is an explanation of the variables
  
 +
<!--T:4-->
 
1. $extension - This is the extension whose language file will be loaded
 
1. $extension - This is the extension whose language file will be loaded
  
 +
<!--T:5-->
 
2. $base_dir - Should be JPATH_SITE in case you have language files stored elsewhere. Defaults to JPATH_BASE.  [optional]
 
2. $base_dir - Should be JPATH_SITE in case you have language files stored elsewhere. Defaults to JPATH_BASE.  [optional]
  
 +
<!--T:9-->
 +
'''Note:''' Joomla will look in its /languages/ folder for a en-GB.com_helloworld.ini language file. If you put your component's language file in your own components language folder /components/com_helloworld/language/en-GB/en-GB.com_helloworld.ini, then you need to specify the path:
 +
<source lang="PHP">
 +
$base_dir = JPATH_SITE . '/components/com_helloworld'
 +
</source>
 +
 +
 +
<!--T:6-->
 
3. $language_tag - 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]
  
 +
<!--T:7-->
 
4. $reload - Flag that will force a language to be reloaded if set to true. [optional]
 
4. $reload - Flag that will force a language to be reloaded if set to true. [optional]
 +
</translate>
  
<noinclude>[[Category:Development]][[Category:Languages]]</noinclude>
+
<noinclude>
 +
<translate>
 +
<!--T:8-->
 +
[[Category:Development]]
 +
[[Category:Languages]]
 +
</translate>
 +
</noinclude>

Latest revision as of 17:28, 31 March 2020

Other languages:
Deutsch • ‎English • ‎Nederlands • ‎español • ‎français • ‎русский

Overview[edit]

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[edit]

$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]

Note: Joomla will look in its /languages/ folder for a en-GB.com_helloworld.ini language file. If you put your component's language file in your own components language folder /components/com_helloworld/language/en-GB/en-GB.com_helloworld.ini, then you need to specify the path:

$base_dir = JPATH_SITE . '/components/com_helloworld'


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]