You can simply use the bindtextdomain
and textdomain
functions once again to set a new translations file as the default resource.
A good way to simplify it would be to create the utilitary function let's say selectTrFile($filename)
which would make it look like this :
function selectTrFile($trFile) {
bindtextdomain($trFile, TRANSLATION_BASE_DIR); // The constant being the path of you translations file directory
textdomain($trFile);
}
// Now set the first file
selectTrFile('default');
// Do your stuff with this translations file
selectTrFile('anotherTranslationsResource');
// Have fun with it
You can even make it quicker by wrapping the function above in another that switches automatically the file for you.
function _f($msgid, $trFile){ // called _f to make it very short to type
selectTrFile($trFile); // Switch the translations file
return _($msgid); // Return the translated string
}
// Then wrap your translations filenames in short variables
$f1 = 'lang';
$f2 = 'brand';
// now you can translate Strings easily from one file or another.
_f("to be translated in file 'lang'", $f1);
_f("to be translated in file 'brand'", $f2);
If what you do not know in which file the translation is, you can use this function instead of _f($msgid, $trFile);
function _f($msgid){
selectTrFile('lang'); // or set a variable containing the filename and use it
$msgstr = _($msgid);
if($msgstr == $msgid){ // if the Strings are equals it means that there was no match in the first file
selectTrFile('brand'); // or set a variable containing the second filename and use it
$msgstr = _$(msgid);
}
return $msgstr;
}
The problem with this last one is that it may slow down a lot the process, I don't know at what point. You could optimize it by not changing to 'lang'
every time, looking what is the actual one if the String was not translated or something like this.
Edit
I don't think you can have multiples translations resources loaded at once. It would be too dangerous for msgid's conflicts. You would be better of making one bigger .mo file than loading multiple ones.
This is the reason why msgfmt makes sure that there are no conflicting msgids at compile time. Do not be afraid of very large .po/.mo files. Mo is a compiled and very fast format.
The problem that could appear now is having a very clumsy .po file. To avoid it, the best way is to separate the file in sections.
There are two ways of separating the sections, either by file or subjet. If you plan on having the same translations in multiple files (which should happen very rarely if you use your includes properly), you can separate by subject.
The usual way is to separate by file and add in a comment where the string is from.
##
# File : /your/php/file.php
##
msgid "first String in file.php"
msgstr "first translation in file.php
.
.
.
##
# File : your/other/file.php
##
msgid "other translation"
msgstr "and then you go on"
So the point is that you should never need to access multiple .mo files from a single .php file if you follow properly the guidelines.
See here (The format of PO files) for more informations.