Forum Discussion

rwm's avatar
rwm
Advisor
12 years ago

REST API for text editor keys?

Is there a REST API that will fetch a given text key as defined in the Studio ext editor section?

 

Now that we are implementing new languages (the single-instance method), I would like to access text keys from a Javascript library to handle some translations, similar to how one uses the text.format() function in Freemarker.

 

I've looked through the REST API docs and community messages but don't find anything.   Is there any kind of API to read text editor strings from Javascript?

 

Otherwise, I may have to implement my own translation mechanism and then somehow determine what language I am using for a given page.

 

Any help or ideas would be appreciated, thanks.

 

6 Replies

  • AdamN's avatar
    AdamN
    Khoros Oracle
    12 years ago

    I don't believe there's a REST API method for that, but you should be able to do something like that using a Studio Endpoint. If you're not familiar with endpoints already, I'd suggest starting here:

    http://lithosphere.lithium.com/t5/developers-knowledge-base/Working-with-Lithium-Studio-Endpoints/ta-p/60240

     

    Your endpoint code might look something like this:

    <#assign textKey = http.request.parameters.name.get("textkey", "") />
    <#if textKey?? && textKey?has_content >
         ${text.format(textKey)}
    </#if>
    

    To get the text string value, you would make a request to the endpoint and specify the text key name as the value of the "textkey" query string parameter. Please note that this is going to give you the text value in the language of the user accessing the endpoint. It sounds like you're planning on some kind of client-side approach anyway, so that may work fine for your purposes.

     

    I hope this helps!

     

    -Adam

  • rwm's avatar
    rwm
    Advisor
    12 years ago

    Thanks for the reply, Adam.   Based on what you said I think this could be a solution to my problem.   However, I don't see the Endpoints tab in my Lithium Studio.   I guess I'll need to ask support about that...

     

     

     

  • AdamN's avatar
    AdamN
    Khoros Oracle
    12 years ago

    That's correct. If you don't have Endpoints enabled in your stage environment, Support should be able to get you taken care of!

  • rwm's avatar
    rwm
    Advisor
    12 years ago

    So to follow-up, I got support to enable Endpoints in Studio.    I created a basic endpoint using the sample you gave me almost exactly as-is, then tested it and it works great.   Then I created the ajax call in my javascript to use the endpoint to fetch a text key, and this is working.

     

    Your original reply said it would return the text value in the language of the user accessing the endpoint, which I have confirmed.   However, in our single-instance language community structure, all our users have their language set to English, and instead we have created categories pages for each language community and assigned the language pack to that category page, which Lithium then properly recognizes down the hierarchy for the most part.

     

    However, because of this way we are setup I want the endpoint to recognize the language of the PAGE it is being called from, NOT the user that is calling it, in order to get my properly translated text.

     

    Is this possible?

     

  • AdamN's avatar
    AdamN
    Khoros Oracle
    12 years ago

    Hmmm... so users will never set their own language preference? How do you plan to handle community-level pages such as My Settings, Search, Profile, etc?

     

    There might be some possible work arounds, but I think it would be a lot cleaner if you could reliably key off the user's language preference.

  • rwm's avatar
    rwm
    Advisor
    12 years ago

    That's a good question...we're still trying to work out those details.   This is the way Lithium has recommended we go.   We've seen the community-level pages handled on other Lithium-based sites that apparently have the same single-instance structure we do, so hopefully we can also solve that problem.

     

    So it sounds like I'm going to need to get the language from the page, perhaps using CommunityJsonObject since I see that has node IDs which could identify the language for me thanks to our naming convention.  I was just hoping for a cleaner solution.

     

    Thanks.