<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to get the translation of a message from an operation in Nuxeo Forum</title>
    <link>https://connect.hyland.com/t5/nuxeo-forum/how-to-get-the-translation-of-a-message-from-an-operation/m-p/318957#M5958</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;You could try the following inside your custom operations (extracted from some Nuxeo operations):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;import org.apache.commons.lang.LocaleUtils;
import org.nuxeo.common.utils.i18n.I18NUtils;

    protected Locale getLocale() {
        if (lang == null) {
            lang = (String) ctx.get("lang");
        }
        if (lang == null) {
            lang = "en";
        }
        return LocaleUtils.toLocale(lang);
    }

    protected String translate(String key) {
        if (key == null) {
            return "";
        }
        return I18NUtils.getMessageString("messages", key, new Object[0], getLocale());
    }

&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 27 Feb 2017 18:14:49 GMT</pubDate>
    <dc:creator>Anahide_Tchertc</dc:creator>
    <dc:date>2017-02-27T18:14:49Z</dc:date>
    <item>
      <title>How to get the translation of a message from an operation</title>
      <link>https://connect.hyland.com/t5/nuxeo-forum/how-to-get-the-translation-of-a-message-from-an-operation/m-p/318956#M5957</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I have an operation that returns some data of a document. I call this operation through Rest API to use it in another application.
I want the operation to return the document type, but translated to the language of the user (File -&amp;gt; Fitxategia (in Basque) -&amp;gt; Archivo (in Spanish), etc.).
I've tried many different ways to translate the messages but I'm not able to make it work, and I don't see any example of translations in operations (except AddMessage, that doesn't do what i want, and I guess it won't work if you don't use it from the web interface).&lt;/P&gt;
&lt;P&gt;Can anyone guide me to the correct answer, please?&lt;/P&gt;
&lt;P&gt;Thank you so much,&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2017 11:49:22 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/nuxeo-forum/how-to-get-the-translation-of-a-message-from-an-operation/m-p/318956#M5957</guid>
      <dc:creator>ioihanguren_</dc:creator>
      <dc:date>2017-02-24T11:49:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the translation of a message from an operation</title>
      <link>https://connect.hyland.com/t5/nuxeo-forum/how-to-get-the-translation-of-a-message-from-an-operation/m-p/318957#M5958</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;You could try the following inside your custom operations (extracted from some Nuxeo operations):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;import org.apache.commons.lang.LocaleUtils;
import org.nuxeo.common.utils.i18n.I18NUtils;

    protected Locale getLocale() {
        if (lang == null) {
            lang = (String) ctx.get("lang");
        }
        if (lang == null) {
            lang = "en";
        }
        return LocaleUtils.toLocale(lang);
    }

    protected String translate(String key) {
        if (key == null) {
            return "";
        }
        return I18NUtils.getMessageString("messages", key, new Object[0], getLocale());
    }

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 27 Feb 2017 18:14:49 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/nuxeo-forum/how-to-get-the-translation-of-a-message-from-an-operation/m-p/318957#M5958</guid>
      <dc:creator>Anahide_Tchertc</dc:creator>
      <dc:date>2017-02-27T18:14:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the translation of a message from an operation</title>
      <link>https://connect.hyland.com/t5/nuxeo-forum/how-to-get-the-translation-of-a-message-from-an-operation/m-p/318958#M5959</link>
      <description>&lt;P&gt;Hi Anahide,&lt;/P&gt;
&lt;P&gt;thank you for your reply.&lt;/P&gt;
&lt;P&gt;I couldn't obtain the locale using &lt;CODE&gt;ctx.get("lang")&lt;/CODE&gt; as you proposed, but I got what I wanted with this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;protected Locale getLocale() {
        String lang = null;

        NuxeoPrincipal principal = (NuxeoPrincipal) session.getPrincipal();
        UserProfileService userProfileService = Framework.getLocalService(
                UserProfileService.class);
        DocumentModel userProfileDoc = userProfileService.getUserProfileDocument(
                principal.getName(), session);

        if (userProfileDoc.getPropertyValue(
                UserProfileConstants.USER_PROFILE_LOCALE) != null) {
            lang = (String) userProfileDoc.getPropertyValue(
                    UserProfileConstants.USER_PROFILE_LOCALE);
        }

        if (lang == null) {
            lang = "en";
        }
        return LocaleUtils.toLocale(lang);
}
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I guess this won't be the best way to do it, but I will stick to this until I get another way. Please anyone tell me if you see anything very very wrong &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Thank you very much for showing me the way!&lt;/P&gt;</description>
      <pubDate>Fri, 03 Mar 2017 11:36:35 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/nuxeo-forum/how-to-get-the-translation-of-a-message-from-an-operation/m-p/318958#M5959</guid>
      <dc:creator>ioihanguren_</dc:creator>
      <dc:date>2017-03-03T11:36:35Z</dc:date>
    </item>
  </channel>
</rss>

