Question
· Apr 27, 2018

Change language - Required

Hi, 

someone knows how to change the "required" to portuguese.

it's a property(Required) on persistence.

i want to change from "required" to "obrigatório"

Discussion (12)3
Log in or sign up to continue

This can be done with the requiredMessage property of %ZEN.Component.control. There are two ways to accomplish this:

1. Just add the requiredMessage attribute

Class DC.Demo.ZenLocalization Extends %ZEN.Component.page
{

XData Contents [ XMLNamespace = "http://www.intersystems.com/zen" ]
{
<page xmlns="http://www.intersystems.com/zen">
<form>
<text label="Descrição" required="true" requiredMessage="obrigatório." />
<submit caption="Enviar" />
</form>
</page>
}

}

Problem is, you'd need to do that in a lot of different places. Instead, you could...

2. Use custom components that subclass built-in control types.

Sample component:

Class DC.Demo.Component.text Extends %ZEN.Component.text [ System = 3 ]
{

/// Feel free to customize this.
Parameter NAMESPACE = "https://community.intersystems.com/post/change-language-required";

/// Value displayed in alert box by the form <method>validate</method>
/// method when this control is required and does not have a value.<br>
/// This is a localized value.
Property requiredMessage As %ZEN.Datatype.caption [ InitialExpression = "obrigatório." ];

}

Sample page using component:

Class DC.Demo.ZenLocalization Extends %ZEN.Component.page
{

XData Contents [ XMLNamespace = "http://www.intersystems.com/zen" ]
{
<page xmlns="http://www.intersystems.com/zen" xmlns:custom="https://community.intersystems.com/post/change-language-required">
<form>
<custom:text label="Descrição" required="true" />
<submit caption="Enviar" />
</form>
</page>
}

}

I was hoping there would be a way to use Zen's localization features to do this, but it seems like that isn't an option, unfortunately;"required." is hard-coded as the InitialExpression for requiredMessage in %ZEN.Component.control, and may only be localized within a page using the component if a non-default value is specified.

With a dynaForm, this would look like:

Class DC.Demo.ZenLocalizationModel Extends %ZEN.DataModel.ObjectDataModel
{

Property Descrição As %String(ZENATTRS = "requiredMessage:obrigatório.") [ Required ];

}

And:

Class DC.Demo.ZenLocalization Extends %ZEN.Component.page
{

XData Contents [ XMLNamespace = "http://www.intersystems.com/zen" ]
{
<page xmlns="http://www.intersystems.com/zen">
<dataController id="myController" modelClass="DC.Demo.ZenLocalizationModel" />
<dynaForm controllerId="myController" injectControls="before">
<submit caption="Enviar" />
</dynaForm>
</page>
}

}

For more information on how to customize data model behavior with property parameters, see the class reference for %ZEN.DataModel.objectModelParameters.

Localization in Caché DBMS

Simple example:

  1. Class dc.test Extends %ZEN.Component.page
    {
    
    Parameter DOMAIN = "DCTEST";
    
    XData Contents [ XMLNamespace "http://www.intersystems.com/zen" ]
    {
    <page xmlns="http://www.intersystems.com/zen">
      <radioSet id="lng"
        label="Текущий язык"
        layout="vertical"
        displayList="Португальский,Русский"
        valueList="pt-br,ru"
        value="ru"
        onchange="zenPage.changeLang(zenThis.value);"
      />
      <form>
        <text label="Описание" required="true" requiredMessage="обязательно." />
        <submit caption="Сохранить"/>
      </form>
    </page>
    }
    
    /// User clicked to change preferred language.
    ClientMethod changeLang(lng) [ Language = javascript ]
    {
      var ok this.SrvChangeLang(lng);
      self.document.location.reload();
    }
    
    /// Change preferred language for this session and page
    ClassMethod SrvChangeLang(lngAs %Boolean ZenMethod ]
    {
      %session.Language=lng
      %response.Language=lng
      q $$$YES
    }
    
    Method %OnAfterCreatePage() As %Status
    {
      ..%SetValueById("lng",%session.Language)
      Quit $$$OK
    }
    
    }
  2. USER>##class(%MessageDictionary).ExportDomainList("messages_ru.xml","DCTEST","ru")
    
    Result (messages_ru.xml):
    
    <?xml version="1.0" encoding="UTF-8"?>
    <MsgFile Language="ru">
       <MsgDomain Domain="DCTEST">
          <Message Id="358179803">Текущий язык</Message>
          <Message Id="1805419696">Португальский,Русский</Message>
          <Message Id="2153752096">Описание</Message>
          <Message Id="2835101332">обязательно.</Message>
          <Message Id="3683485237">Сохранить</Message>
       </MsgDomain>
    </MsgFile>
    
  3. messages_pt-br.xml:
    
    <?xml version="1.0" encoding="UTF-8"?>
    <MsgFile Language="pt-br">
       <MsgDomain Domain="DCTEST">
          <Message Id="358179803">Idioma atual</Message>
          <Message Id="1805419696">Português,Russo</Message>
          <Message Id="2153752096">Descrição</Message>
          <Message Id="2835101332">é obrigatório.</Message>
          <Message Id="3683485237">Salvar</Message>
       </MsgDomain>
    </MsgFile>
    
    USER>##class(%MessageDictionary).Import("messages_pt-br.xml")
  4. Profit!