Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+4 votes
1.2k views
in Q2A Core by
I want to change language by front-end(user) operation. qa-lang/en-GB and qa-lang/ja etc... Is there any idea?

Related question: http://www.question2answer.org/qa/2258
Operation sample: http://qa.modxclub.net/  >> テーマセレクト at top-right

Thanks in advance.

1 Answer

+1 vote
by
edited by
Below code is language + theme changer for V1.2.
Demo: http://qa.modxclub.net/
See Selection box at Top-right.

---------------------------------------------------------------
【URL parameter read】
Source: qa-include/qa-base.php
find:
        "$languagecode=qa_get_option($qa_db, 'site_language');"
add after:
        $qa_lang=qa_get('qa_lang'); // get from URL parameter if possible
        if (isset($qa_lang)) // otherwise get option
            $languagecode=$qa_lang;
---
Source: qa-include/qa-page.php
find:
    $themeclass=qa_load_theme_class(qa_get_option($qa_db, 'site_theme'), $qa_template, $qa_content, $qa_request);
replace:
    $qa_theme=qa_get('qa_theme'); // get from URL parameter if possible
    if (!isset($qa_theme)) // otherwise get option
        $qa_theme=qa_get_option($qa_db, 'site_theme');
    $themeclass=qa_load_theme_class($qa_theme, $qa_template, $qa_content, $qa_request);
---------------------------------------------------------------
【URL parameter restore】
Source: qa-include/qa-base.php
find:
        global $qa_db, $qa_root_url_relative;
add after:
        $qa_lang=qa_get('qa_lang');
        if (isset($qa_lang))
            @$params['qa_lang']=$qa_lang;
        $qa_theme=qa_get('qa_theme');
        if (isset($qa_theme))
            @$params['qa_theme']=$qa_theme;
---------------------------------------------------------------
【Display HTML】
---
Source: qa-include/qa-page.php
find:
    $themeclass->output('>');
add after:
    $themeclass->changer();
---
Source: qa-include/qa-theme-base.php
add function:
        function changer()
        {
            require_once QA_INCLUDE_DIR.'qa-app-admin.php';

            // Lang: Get and Set URL parameter OR System settings
            $qa_lang=qa_get('qa_lang');
            if (!isset($qa_lang))
                $qa_lang=qa_get_option($qa_db, 'site_language');
            $langParam='&qa_lang='.$qa_lang;
            $langOptions = qa_admin_language_options();
            foreach($langOptions as $key => $val)
                $langArray[] = array('name'=>$val, 'value'=>$key, 'selected'=>'');
            $found = false;
            $index = 0;
            foreach($langArray as $langItem){
                foreach($langItem as $key => $val){
                    if ($key=='value' && $val==$qa_lang){
                        $langArray[$index]['selected']='selected="selected"';
                        $found = true;
                        break 2;
                    }
                }
                $index++;
            }
            if(!$found) $langArray[0]['selected']='selected="selected"';

            // Theme: Get and Set URL parameter OR System settings
            $qa_theme=qa_get('qa_theme');
            if (!isset($qa_theme))
                $qa_theme=qa_get_option($qa_db, 'site_theme');
            $themeParam='&qa_theme='.$qa_theme;
            $themeOptions = qa_admin_theme_options();
            foreach($themeOptions as $key => $val)
                $themeArray[] = array('name'=>$val, 'value'=>$key, 'selected'=>'');
            $found = false;
            $index = 0;
            foreach($themeArray as $themeItem){
                foreach($themeItem as $key => $val){
                    if ($key=='value' && $val==$qa_theme){
                        $themeArray[$index]['selected']='selected="selected"';
                        $found = true;
                        break 2;
                    }
                }
                $index++;
            }
            if(!$found) $themeArray[0]['selected']='selected="selected"';

            // Output option bar
            $this->output('<DIV class="qa-changer" style="padding:5px 0;margin:0 auto;text-align:right;width:980px;">');
            $this->output('<DIV class="qa-changer-block" style="display:inline;">
            <SPAN class="qa-changer-title" style="">Language:</SPAN>
            <SELECT class="qa-changer-selector" style="" name="qa-changer-select-lang" onchange="location.href=\'?&amp;qa_lang=\'+this.options[this.selectedIndex].value+\''.$themeParam.'\'">');
            foreach($langArray as $langItem)
                $this->output('<OPTION class="qa-changer-selector-option" value="'. $langItem['value'] .'" '. $langItem['selected'] .'>'.$langItem['name'].'</OPTION>');
            $this->output('</SELECT>');
            $this->output('</DIV>');
            $this->output('<DIV class="qa-changer-block" style="display:inline;">
            <SPAN class="qa-changer-title" style="">Theme:</SPAN>
            <SELECT class="qa-changer-selector" style="" name="qa-ts-selector-theme" onchange="location.href=\'?'.$langParam.'&amp;qa_theme=\'+this.options[this.selectedIndex].value">');
            foreach($themeArray as $themeItem)
                $this->output('<OPTION class="qa-changer-selector-option" value="'. $themeItem['value'] .'" '. $themeItem['selected'] .'>'.$themeItem['name'].'</OPTION>');
            $this->output('</SELECT>');
            $this->output('</DIV>');
            $this->output('</DIV>');
        }
---
by
...