XF 2.1 Is it possible to use Xenforo's phrase system inside javascript?

asprin

Active member
Let's say I've this code:

JavaScript:
var Asprin = window.Asprin || {};

/** @param {jQuery} $ */
!(function($, window, document, _undefined) {
  'use strict';

  Asprin.Foo = XF.Event.newHandler({
    eventType: 'click',
    eventNameSpace: 'Foobar',

    init: function() {
    },

    click: function(event) { 
        // call \XF::phrase() here?
    },
  });

  XF.Event.register('click', 'blahblah', 'Asprin.Foo');
})(jQuery, window, document);

One alternative I can think of is fetching the phrase inside a template and storing it on a HTML element via the data-foobar attribute and then pulling the content in javascript via element.getAttribute('data-foobar')

But wanted to check if all this can be avoided.
 
Solution
Check the helper_js_global template. It ships phrases that are frequently required by js to the end user. You won't be able to do it otherwise, since the user does not have access to the entire phase library of the site.

Lukas W.

Well-known member
Check the helper_js_global template. It ships phrases that are frequently required by js to the end user. You won't be able to do it otherwise, since the user does not have access to the entire phase library of the site.
 
Solution

asprin

Active member
Check the helper_js_global template. It ships phrases that are frequently required by js to the end user. You won't be able to do it otherwise, since the user does not have access to the entire phase library of the site.
So my understanding is that even though XF allows it, it should be used judiciously/as less as possible?
 

robdog

Well-known member
If you want to output a phrase you simply use:
JavaScript:
XF.phrase('PHRASE_TO_USE')

However, if you want to make phrases available for that page, you will need to add them to the global object on the page:
JavaScript:
jQuery.extend(XF.phrases, {
    new_phrase_a: "New Phrase A",
    new_phrase_b: "New Phrase B",
    new_phrase_c: "New Phrase C"
});

XF.phrase('new_phrase_a');
 
Top