1

我正在尝试做一个 Prestashop 模块,但是当尝试在配置表单中设置一个可翻译的表单字段时,它失败了。

这是我通过 JS 控制台得到的错误:

[14:33:39.915] ReferenceError: defaultLanguage is not defined @ http://localhost:8888/js/admin.js:173

我认为我已经很好地配置了后台的语言,所以我不确定为什么会发生这种情况。

这就是我尝试创建表单的方式:

public function displayForm()
{
    // Get default Language
    $default_lang = (int)Configuration::get('PS_LANG_DEFAULT');

    // Init Fields form array
    $fields_form[0]['form'] = array(
            'legend' => array(
                    'title' => $this->l('Settings'),
            ),
            'input' => array(
                    array(
                            'type' => 'text',
                            'label' => $this->l('Título de la noticia'),
                            'name' => 'NOTICIA_TIT',
                            'size' => 30,
                            'required' => true,
                            'lang' => true
                    ),
                    array(
                            'type' => 'text',
                            'label' => $this->l('Imagen de la noticia'),
                            'name' => 'NOTICIA_IMG',
                            'size' => 30,
                            'required' => true,
                            'enabled' => false
                    ),
                    array( 
                            'type' => 'file',
                            'label' => $this->l('Subir nueva imagen'),
                            'name' => 'NOTICIA_IMG_FILE',
                            'size' => 30        
                    ),
                    array(
                            'type' => 'textarea',
                            'label' => $this->l('Texto de la noticia'),
                            'name' => 'NOTICIA_TXT',
                            'required' => true,
                            'cols' => 30,
                            'rows' => 4,
                            'lang' => true
                    )
            ),
            'submit' => array(
                    'title' => $this->l('Save'),
                    'class' => 'button'
            )
    );

    $helper = new HelperForm();

    // Module, token and currentIndex
    $helper->module = $this;
    $helper->name_controller = $this->name;
    $helper->token = Tools::getAdminTokenLite('AdminModules');
    $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;

    // Language
    $helper->default_form_language = $default_lang;
    $helper->allow_employee_form_lang = $default_lang;

    // Title and toolbar
    $helper->title = $this->displayName;
    $helper->show_toolbar = true;        // false -> remove toolbar
    $helper->toolbar_scroll = true;      // yes - > Toolbar is always visible on the top of the screen.
    $helper->submit_action = 'submit'.$this->name;
    $helper->toolbar_btn = array(
            'save' =>
            array(
                    'desc' => $this->l('Save'),
                    'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
                    '&token='.Tools::getAdminTokenLite('AdminModules'),
            ),
            'back' => array(
                    'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
                    'desc' => $this->l('Back to list')
            )
    );

    // Load current value
    $helper->fields_value['NOTICIA_TXT'] = Configuration::get('NOTICIA_TXT');
    $helper->fields_value['NOTICIA_TIT'] = Configuration::get('NOTICIA_TIT');
    $helper->fields_value['NOTICIA_IMG'] = Configuration::get('NOTICIA_IMG');

    return $helper->generateForm($fields_form);
}

编辑:我在代码中看到了这一点。

<script type="text/javascript">
var module_dir = '/modules/';
    var id_language = 1;
    var languages = new Array();
    var vat_number = 1;
    // Multilang field setup must happen before document is ready so that calls to displayFlags() to avoid
    // precedence conflicts with other document.ready() blocks
            // we need allowEmployeeFormLang var in ajax request
    allowEmployeeFormLang = 1;
    displayFlags(languages, id_language, allowEmployeeFormLang);

    $(document).ready(function() {

        if ($(".datepicker").length > 0)
            $(".datepicker").datepicker({
                prevText: '',
                nextText: '',
                dateFormat: 'yy-mm-dd'
            });

    });

</script>

languajes 变量被创建为一个空数组。但是,这是函数 displayFlags:

function displayFlags(languages, defaultLanguageID, employee_cookie)
{
if ($('.translatable'))
{
    $('.translatable').each(function() {
        if (!$(this).find('.displayed_flag').length > 0) {
            $.each(languages, function(key, language) {
                if (language['id_lang'] == defaultLanguageID)
                {
                    defaultLanguage = language;
                    return false;
                }
            });
            var displayFlags = $('<div></div>')
                .addClass('displayed_flag')
                .append($('<img>')
                    .addClass('language_current')
                    .addClass('pointer')
                    .attr('src', '../img/l/' + defaultLanguage['id_lang'] + '.jpg')
                    .attr('alt', defaultLanguage['name'])
                    .click(function() {
                        toggleLanguageFlags(this);
                    })
                );
            var languagesFlags = $('<div></div>')
                .addClass('language_flags')
                .html('Choose language:<br /><br />');
            $.each(languages, function(key, language) {
                var img = $('<img>')
                    .addClass('pointer')
                    .css('margin', '0 2px')
                    .attr('src', '../img/l/' + language['id_lang'] + '.jpg')
                    .attr('alt', language['name'])
                    .click(function() {
                        changeFormLanguage(language['id_lang'], language['iso_code'], employee_cookie);
                    });
                languagesFlags.append(img);
            });
            if ($(this).find('p:last-child').hasClass('clear'))
                $(this).find('p:last-child').before(displayFlags).before(languagesFlags);
            else
                $(this).append(displayFlags).append(languagesFlags);
        }
    });
}
}
4

2 回答 2

2

我修复了您遇到的相同错误,但我遇到了另一个错误。

为了定义 defaultLanguage,您应该填写 helperform 的语言属性。你可以这样做:

$languages = Language::getLanguages(true);
$helper->languages = $languages;

我不确定您是否应该为 getLanguages 设置 true 或 false ...我都厌倦了,但我仍然收到此错误:

Uncaught SyntaxError: Unexpected token ILLEGAL 

它发生在这里:

languages[0] = {
id_lang: 1,
iso_code: 'en',
name: 'English',
is_default: '<br />

所以现在,为了定义属性 is_default 可能还有更多工作要做......你找到解决问题的方法了吗?

编辑:通过自己设置 is_default ,它可以工作。但是很丑...

// Languages
$languages = Language::getLanguages(true);
    for($i=0; $i<count($languages); $i++){
        if($languages[$i]['id_lang'] == $default_lang){
            $languages[$i]['is_default'] = 1;
        }else{
            $languages[$i]['is_default'] = 0;
        }
    }
$helper->languages = $languages;
于 2014-02-10T15:47:30.643 回答
2

从模块控制器获取语言:

$languages = $this->context->controller->getLanguages();
于 2014-12-13T16:59:59.800 回答