• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Lack of interest Additional js phrases

xf_phantom

Well-known member
#2
A probably better alternative would be a way to fetch the phrases via ajax and add them to XenForo.phrases for later usage

e.g.
Code:
phrasesHelper : function($phrases){
            $phrasesToFetch = [];
            $.each($phrases, function(id,$name){
                if (!XenForo.phrases[$name]){
                    $phrasesToFetch.push($name);
                }
                XenForo.ajax('index.php?core/fetchPhrases',$phrasesToFetch,$.context(this, 'loadSuccess'));
            });
        },

then we would need to call only
Code:
var neededPhrases = ['save','fat_load_more','cancel'];

XenForo.phrasesHelper(neededPhrases);
inside our JS code and wouldn't need to change the templates
 

Chris D

XenForo developer
Staff member
#3
I'm not sure how necessary a new solution is.

You can just do this in any template:

Code:
<script>
    jQuery.extend(XenForo.phrases,
    {
        test_phrase: "TESTING PHRASE"
    });
</script>
upload_2014-2-18_13-40-26.png

You could also create an external JS file and require it in your template wherever you need it.
 

xf_phantom

Well-known member
#4
You could also create an external JS file and require it in your template wherever you need it.
This way i can't use the xenforo phrases system the customize the text and it wouldn't work for boards with more languages (and i'm not interessted in creating and rebuilding the file)

Code:
<script>
    jQuery.extend(XenForo.phrases,
    {
        test_phrase: "TESTING PHRASE"
    });
</script>
requires, that you know exactly, what phrases you'll need in your template(if you want to avoid to load them global) and it makes asynch. javascript loading imposible (and it also makes development easier, because you don't need to maintain the templates:D )
 

Jeremy

Well-known member
#7
The JS runs on specific pages, yes? They have templates, right? Chris' method works perfectly fine and can be utilized dynamically by inserting them via code.
 

xf_phantom

Well-known member
#9
XenForo is doing something similar, e.g.

Code:
  var processing = XenForo.phrases['processing'] || 'Processing',
         cancel = XenForo.phrases['cancel'] || 'Cancel',
         cancelling = XenForo.phrases['cancelling'] || 'Cancelling';
but i have to avoid hardcoded phrase in the file because of
This way i can't use the xenforo phrases system the customize the text and it wouldn't work for boards with more languages (and i'm not interessted in creating and rebuilding the file)
 

Jeremy

Well-known member
#10
I know what asynchronous code is. I wrote it for a living. If you don't want to use the solution Chris and I have provided, that is up to you. You can wait and see if this suggestion is implemented.
 

cclaerhout

Well-known member
#11
I may be wrong, but I don't see any asynch js code here.

From where are your calling the JS that will have the code below ?
Code:
this.$link = $('<a href="javascript:" />').text(XenForo.phrases.fat_load_more).addClass('button').click($.context(this, 'click')).xfInsert('appendTo', this.$container);
Extend phrases
  • If the trigger is inside a template, you don't need it, you use directly the XenForo phrase tag.
  • If the trigger is an element in the page that calls a ajax request, then your code has a destination. If this destination is a XenForo url, then it will have a "view", if it has a view, you can use the "renderJson" function to add any variable to the json answer. You will be able to get that element in the "ajaxData" argument from the ajax success function.

    Code:
    Element on the page => ajax request => create/extend view of the request destination => add phrases => ajax request success => find your phrase inside the ajax data
If you don't want to load gobally a phrase that will only be use in some sections of the website, include at least a template in these sections. You can then create a new JS object with the phrases you need or, if you only need 1 phrase, then put it in a "data" parameter of an element:
HTML:
<div data-myPhrase="{xen:phrase myPhrase}"></div>

About your code
Code:
phrasesHelper : function($phrases){
$phrasesToFetch = [];
$.each($phrases, function(id,$name){
if (!XenForo.phrases[$name]){
$phrasesToFetch.push($name);
}
XenForo.ajax('index.php?core/fetchPhrases',$phrasesToFetch,$.context(this, 'loadSuccess'));
});
},
  • If this function is used several times, then the ajax request will be triggered several times too
  • The XenForo ajax should not be inside the loop
  • Advice: try to only use the '$' character with jQuery object. For normal js variable, strip it, you're not in php. It will be easier and faster to re-read your code.