Home arrow Forum

Remository Forum

 


admin

Karma: 101  
Language files - 2005/08/28 20:15 Many components (also sometimes modules or mambots) have language files. This is a good principle, as it allows for text that is displayed to be kept separate from the code, so that versions in different languages are easily substituted.

There is another useful feature of language files, and that is that users can easily alter the text themselves. However, this use can create a problem when the user is faced with the task of doing the edits all over again when a new version of the software is released with a different language file.

My recommendation is that all language files avoid the simple use of define, instead first checking whether the definition is there already. So a typical line of a language file should be:

if (!defined('NAME')) define ('NAME','VALUE');

This way, the user can use simple defines in a block prior to the standard definitions. Any item that has been defined by the user will not get its standard definition. The user defines are all together and easily moved to a future version.
Martin Brampton aka Counterpoint
http://aliro.org
http://black-sheep-research.com
  | | Sorry, you do not currently have permission to write here.
Nicolas Moyroud

 
Re:Language files - 2005/12/01 15:27 Hello,

Very good idea Martin, thanks! I think I will use that kind of language file for my future developments. I like the idea to allow the users to keep their changes even when a new version is available.
But it's a little boring to have to put
if(!defined('NAME')) define...
at each line of your language file.
To alter the text, users have to edit this file and can make some mistakes when adding their own lines.
I think about it for a while and I have an improvement to suggest.
So I propose to use two separated ini files for one language and take the benefit of using the parse_ini_file PHP function. I give an example :

1) file english.ini (provided with the component)
_GLOSSARY_TERM = Term
_GLOSSARY_TERMS = Terms
_GLOSSARY_AUTHOR = Author
_GLOSSARY_GLOSSARY = Glossary
...

2) file english_custom.ini (created & edited by user)
_GLOSSARY_AUTHOR = Creator
...

3) file language.php
function loadLanguage($lang) {
if (file_exists($lang.'_custom.ini')) {
$tab2 = parse_ini_file($lang.'_custom.ini');
fillConstants($tab2);
}

if (!file_exists($lang.'.ini')) {
$lang = 'english';
}
$tab = parse_ini_file($lang.'.ini');
fillConstants($tab);
}

function fillConstants($array) {
foreach ($array as $key=>$val) {
if(!defined($key)) {
define($key,$val);
}
}
}

4) At the top of a file where your want to use your language file (for example glossary.php), just add :
require_once 'language.php';
loadLanguage($lang); // $mosConfig_lang instead of $lang for Mambo-Joomla

Then users only have to edit english_custom.ini, french_custom.ini, etc... which are very simple file with no php code in them. When a new version of the component is available, users will keep unchanged their custom.ini files.
  | | Sorry, you do not currently have permission to write here.
admin

Karma: 101  
Re:Language files - 2005/12/02 03:54 There is no need for user modifications to use the conditional define. Only the standard language file entries.

The drawback to ini files using the PHP parser is that the parser simply stops if an error is found in the ini file. So far as I know, that leaves an unpredictable outcome with no helpful diagnostics. For that reason, I'm reluctant to go down the ini route.
Martin Brampton aka Counterpoint
http://aliro.org
http://black-sheep-research.com
  | | Sorry, you do not currently have permission to write here.

Save FM Radio

Login

Subscribe to Premium Support

Get priority support for Remository and Glossary, sign up now for a Premium Support monthly subscription:

Your Remository user name

Or purchase a year's support:

Your Remository user name

Recommended SEF

SEF Advance

Who is Online

Remository welcomes guests and visitors

We have 7 guest online