Load language file during joomla (2.5) system plugin installation

荒凉一梦 提交于 2019-12-24 02:34:09

问题


I'm having a real hard time showing a localized string during the installation of a system plugin (in Joomla 2.5). The "normal" way with localized strings in the xml file doesn't seem to work, (see this other question: Language based installation description).

I now tried the way proposed there, to show the description via the install scripts. This kind of works (I can echo text successfully), however, I also can't localize there - when debugging the language it shows that the plugin.sys.ini is not loaded yet; I tried to manually load the file, but had no success with loading any of my plugin language files. This is what I got so far (in a file named setupscripts.php):

<?php // no direct access
defined('_JEXEC') or die('Restricted access');

class plgsystemmyplgnameInstallerScript {

    static function loadLanguage() {
        $lang =& JFactory::getLanguage();
        $lang->load('plg_system_myname', JPATH_ADMINISTRATOR);
    }

    function install($parent)
    {
        self::loadLanguage();
        echo JTEXT::_("PLG_MYNAME_TEST_TEXT");
    }
    function uninstall($parent)
    {
        self::loadLanguage();
        echo JText::_('PLG_MYNAME_UNINSTALL_TEXT');
    }
    function update($parent)
    {
        self::loadLanguage();
        echo JText::_('PLG_MYNAME_UPDATE_TEXT');
    }
    function preflight($type, $parent) {}
    function postflight($type, $parent) {
        self::loadLanguage();
        echo JText::_('PLG_MYNAME_INSTALL_TEXT');
    }
}

But I only get ??PLG_MYNAME_TEST_TEXT?? ??PLG_MYNAME_INSTALL_TEXT?? (language debugging is turned on) during installation... weirdly enough, the language debug feature at the bottom of the page under "untranslated strings" shows "None" (where do the question marks then come from if not from a tried but failed translation???).

Tried some variations of it (with .sys at the end of the plugin name, since I actually think the setup strings should be in the .sys.ini file, without the second parameter (leaving it default), but no luck - no error, nothing in the log (in fact my log file isn't existing, probably there was no entry yet? can one set the log level with Joomla?). But never is there any file loaded (nothing changes under "loaded language files".

Anybody got an idea how to load the language properly?

Is there something special to consider when loading languages during setup? Why is there no error message if loading the languages fails? Do I maybe have to install the language files to a special location to get them recognized during installation? My current xml looks like this:

<extension version="2.5" type="plugin" group="system" method="upgrade">
<name>PLG_MYNAME</name>
<!-- ... author, copyright, version, .. --> 
<scriptfile>setupscripts.php</scriptfile>
<files>
    <filename plugin="myname">myname.php</filename>
    <filename>setupscripts.php</filename>
    <filename>index.html</filename>
    <folder>sql</folder>
</files>
<!-- ... install->sql->file ... -->
<!-- ... uninstall->sql->file ... -->
<!-- ... update->schemas->schemapath ... -->
<languages [folder="admin"]>
   <language tag="en-GB">en-GB.plg_system_myname.ini</language>
   <language tag="en-GB">en-GB.plg_system_myname.sys.ini</language>
   <!-- ... other languages ... -->
</languages>
<!-- ... config->fields->fieldset->field ... -->
</extension>

(the square brackes around folder="admin" are supposed to indicate that I tried both with and without this attribute. It doesn't change anything).


回答1:


It only works on installation if you also copy the files to the admin language folder. If you look at the core extensions you will see that they do both. It's really a bug but that's the work around.




回答2:


Finally I found out how to really do it. A thorough search in the google Joomla dev group brought up this very similar question.

Basically, the language files need to reside in a separate language folder it seems, and the files section also needs to reference them. My xml now looks like this:

<!-- ... everything else stayed the same, except: -->
<files>
    <filename plugin="myplg">myplg.php</filename>
    <filename>index.html</filename>
    <folder>language</folder>
    <folder>sql</folder>
</files>
<languages folder="language">
        <language tag="en-GB">en-GB/en-GB.plg_system_myplg.ini</language>
        <language tag="en-GB">en-GB/en-GB.plg_system_myplg.sys.ini</language>
        <!-- .. other languages ... ->
</languages>
<!-- ... rest of the xml file ... -->

The language files are now all in separate subfolders... they get copied to the exact same location as before (administrator/language//...), but now the description from the XML is also localized!

I find it very weird that there are so many ways to specify language files, which all basically work except for the one corner case of the installation...

Hope this will help other people struggling with this!



来源:https://stackoverflow.com/questions/14186611/load-language-file-during-joomla-2-5-system-plugin-installation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!