We build. You grow.

Get best community software here

Start a social network, a fan-site, an education project with oxwall - free opensource community software

OW.getLanguageText | Forum

Marcus
Marcus Sep 7 '19

Why this function doesn't work.


var result = confirm(OW.getLanguageText('plugin', 'test'));


Shows as plugin+test. The key is not empty!

dave Leader
dave Sep 17 '19
Because you have the code wrong.. 


First in your php gave you need to get a copy of the language instance if you want to its not necessary just less typing 


Its not OW.getLanguage  its 


$language = OW::getLanguage();       you forgot the ::


Then text is an object... we know this by the ->   remember an old php rule.... you shoot objects with arrows, thats how i always kept it straight between objects and arrays. 


so that has an arrow  ->   so its an object. 


you forgot to tell it its an object like so 


OW::getLanguage()->text('plugin', 'keyname'));


Use the keyname, if your going to just use plane text then just put text you dont need getlanguage at all. 


I would do it this way since i have the language instance. 


 $language->text('plugin', 'keyname'));  which is the same as 


OW::getLanguage()->text('plugin', 'keyname'));


i am confused about the var result =.. are you trying to use that in php or html or js.  Remember smarty does it differently if you want to use that in html using js


in smarty html you would do it like so.. 


this would be a delete link



<a href="{$item.deleteurl}" onclick="return confirm('{text key='base+are_you_sure'}');">{text key='pluginname+keyname'}</a>


$item.deleteurl is assigned by the data array in the php file as the url with id and uses the php function that deletes the file


hope that helps... 

Marcus
Marcus Sep 18 '19
Great Dave. Well it appears to be working just fine for newsfeed and photo plugins. You can locate this line of code in .js files. I'm trying to call language key from Javascript just like in ow.js
dave Leader
dave Sep 18 '19
if you want to use a lang key in js youll have to do this..  


on the html side you can pass that value to js like so 


{literal}

<script type="text/javascript">

"use strict";

var text = {/literal} '{text key="pluginname+key"}'{literal};


</script>

{/literal}


any value that you have passed from php to smarty can also be passed to js in the html file in the same way.



The Forum post is edited by dave Sep 18 '19
AppXprt
AppXprt Sep 18 '19
Nice, I didn't know about this Dave, thanks for the info!
dave Leader
dave Sep 18 '19
YW, if your passing a smarty variable you do so like this 


var value  = {/literal} '{$value}' {literal};

Marcus
Marcus Sep 18 '19
Thanks Dave. Take a look at this code from photo.js

$.ajax({
url: $(this).attr('url'),
cache: false,
type: 'POST',
dataType: 'json',
success: function( data )
{
// TODO show message
OW.info(OW.getLanguageText('photo', 'approve_success_message'));
$('#photo-approve').parent().remove();
}
})
}); How foes it work here

The Forum post is edited by Marcus Sep 18 '19
Marcus
Marcus Sep 18 '19
P. S. Ow.info work just fine why not OW.getLanguageText wierd
The Forum post is edited by Marcus Sep 18 '19
dave Leader
dave Sep 18 '19

Quote from Marcus Thanks Dave. Take a look at this code from photo.js
$.ajax({url: $(this).attr('url'),cache: false,type: 'POST',dataType: 'json',success: function( data ){// TODO show messageOW.info(OW.getLanguageText('photo', 'approve_success_message'));$('#photo-approve').parent().remove();}})}); How foes it work here

Ajax is how you process data in the background without loading a new page.  Its normally used to run a php file for example and return the results with no new page load. It is written in a js file. 


url is the url of the php file you want to run.  This is assigned as a dynamic value passed to the html file and then passed to js, or a static value. 


Cache false means that you dont want the requested page cached which is for security.  Also it will allow the request to be called over and over again by other users. 


Type is the type of reqest,  a POST request. 


Datatype   json  is a secured way to save the data returned from the request


Then you check for success and run everything in the success section if returns data. 


If not success then it will skip the success section and fall down to a error section which you dont have in your sample. 




You also have not assigned the data which would go after datatype


example  (this is what is passed to the php file for processing)


data: { 

            id: parseInt(id),

             url: url,             

            ajax: true

           },



You set ajax true so that the php file can check to see if ajax is set to true, if not then the php file will exit. 


after the sucess closes you would add something like this for error 


error: function(errorThrown){
         console.log('There is an error with AJAX! - code 5');
        }


this will show up in the console log so you know where to look in the code for the error,.

 


hope that helps. 

The Forum post is edited by dave Sep 18 '19
Marcus
Marcus Sep 19 '19
Thanks Dave for refreshing my memory. But I was actually pointing this line out......

OW
.info(OW.getLanguageText('photo''approve_success_message'));
The Forum post is edited by Marcus Sep 19 '19
dave Leader
dave Sep 19 '19

Ill have a look.. 

The Forum post is edited by dave Sep 19 '19
dave Leader
dave Sep 19 '19

I am not a js pro but 



ow_static/plugins/base/js/ow.js line 138   is where the js function is assigned


this.getLanguageText = function(prefix, key, assignedVars)


so it should work if you call it with OW.  as you did...mmmmmmm 


im really not sure  why it does not work for you....  OW should be global im thinking... 


what about assigning it to a var.. 


var value = OW. blah blah blah 


console.log('value');



The Forum post is edited by dave Sep 19 '19
dave Leader
dave Sep 19 '19

Quote from Marcus

Why this function doesn't work.


var result = confirm(OW.getLanguageText('plugin', 'test'));


Shows as plugin+test. The key is not empty!

So you actually have a plugin called plugin?  and a key named test?


or was that just an example for here?


I have had lang keys that got stuck before meaning that they show up under language but not under the parsed, i had to export the lang for the plugin and overwrite the one in the root of the plugin and then clear cache several times and then it finally showed up.    Not sure why it does that sometimes. 

The Forum post is edited by dave Sep 19 '19
Marcus
Marcus Sep 19 '19
Thanks again I have tried with many keys including of my plugin no luck returns and empty key like so base+whatever. Could you test it please and les us know how did it go
dave Leader
dave Sep 19 '19
Sure ill have some time today to test it.  May i ask is there a reason why you have to get the text value in that way and not just grab the text with php?
Marcus
Marcus Sep 19 '19
Am playing around with dinamic content. You know Ajax jquery Javascript. I could prefech Lang keys but it bugs me why it's not working. Someone took time to develop that function I'd like to take advantage of it. 
Senior Developer Leader
Senior Developer Sep 19 '19
All language text is not available in JavaScript, you need make it available using this PHP code inside your controller or component:


        $language = OW::getLanguage();        

        $language->addKeyForJs('yourplugin', 'yourlangkey');


Now you can get this text from javascript in any place using this JS code:


OW.getLanguageText('yourplugin', 'yourlangkey');



Senior Developer.

The Forum post is edited by Senior Developer Sep 19 '19
dave Leader
dave Sep 20 '19
Great SD thanks for helping out :)  I have seen the addKeyForJS but never knew what it was for.  :)
Marcus
Marcus Sep 20 '19
Thanks guys. Really appreciate this one. 
dave Leader
dave Sep 20 '19
Marcus i forgot yesterday dont forget before the success check if data not === 'undefined'  i always toss that in to help qualify the success in case there is a conflict and comes back undefined it will fall to your error process. 


Yeah SD is the js man, he runs circles around me in js, he is very good at it. 

The Forum post is edited by dave Sep 20 '19
Pages: 1 2 »