Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+7 votes
316 views
in Q2A Core by
I am using qa_lang() to avoid hardcoding language into the plugins. However some of my javascript files produce errors or messages for the user - how do I avoid hardcoding language in these scripts?
Q2A version: 2.7.4

1 Answer

+5 votes
by
selected by
 
Best answer

In that case, I will create script files dynamically.

Creation timing

  • When plugin options are saved
  • When my head_script() layer function is called from core

Example

my-layer.php:

require_once QA_PLUGIN_DIR.'my-plugin/my-class.php';
class qa_html_theme_layer extends qa_html_theme_base {
    public function head_script() {
        $this->content['script'][] = '<script src="'.MyClass::get_js_path().'?'.QA_VERSION.'"></script>';
        qa_html_theme_base::head_script();
    }
}

my-class.php:

class MyClass {
    public static function get_js_path() {
        $relativepath = 'qa-plugin/my-plugin/js/myplugin.js';
        self::create_js($relativepath);
        return qa_path($relativepath, null, null, QA_URL_FORMAT_NEAT);
    }
    public static function create_js($relativepath, $force=false) {
        $absolutepath = QA_BASE_DIR.$relativepath;
        if($force || !file_exists($absolutepath)) {
            $js = '';
            $js .= 'var lang = "'.qa_lang('main/1_answer').'";'."\n";
            file_put_contents($absolutepath, $js);
        }
    }
}

...