intra-mart Accel Platform / TERASOLUNA Global Framework Programming Guide

Version 2 2014-01-01

Contents

Internationalization Input Process Example

In this chapter, about internationalization input is described.

About Internationalization Information Input/Registration/Update

Internationalization information when you input, registrate, or update are described here.
This process example is also used in intra-mart Accel Platform.

About Input Item Display

  • In case [All mandatory] and [Small numbers of item]
  • Display all input item for the number of languages installed in the system.
  • Process such as hide languages other than default one from the beginning is never performed.
  • Display input items of tenant default language at the top.
  • Alignment sequence other than tenant default will be the sequence that SystemLocale#getLocaleInfos() returns.
  • If tenant default language is not set, it will be system default language.
  • Example: IM-Workflow
  • In case of [Not all necessary] or [Many items]
  • Display default locale only, and determine what other locales to be registerd by server side logic.
  • Example: IM-Master

Abuot Input and Registration/Update Process from a Screen

  • If internationalization item is mandatory input, the language displayed firts *1 will be mandatory input and other language will be optional input.

    |If nothing is input in optional input item, it is assumed as the same value with the item value of language displayed first is input. | | *1:Language displayed first will be either tenant default or system default language.

    • If option item is not input when registration and updating, contents of the item that corresponds to the tenant locale are copied.
    • Copy process is performed at server side.
    • You should not copy it inside the API.
  • Internationalization input item value supplement of optional input is processed at server side.

Validation Check

  • Items that changes dynamically such as internationalization input item should be uniquely checked at client side JavaScript.
    Other fixed items are checked by Java.

Implementation Example

  • Fixed input:Data ID (mandatory, maximum 64 characters)
  • Internationalization Input:Data name (mandatory, maximum 256 characters)

localeInfo.jsp (client side JavaScript)

  • Set validation rule, message loading and dynamic validation.
    If you send internationalization items to server side, the value to be the base (default language) and all languages are sent individually.
    By this, mandatory and the length of default language can be checked automatically.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="imui" uri="http://www.intra-mart.co.jp/taglib/imui" %>
<%@ taglib prefix="imart" uri="http://www.intra-mart.co.jp/taglib/core/standard" %>

<imui:head>

  <script src="ui/libs/jquery-validation-1.9.0/jquery.validate.js"></script>
  <script type="text/javascript">

    // It is described below localeInfo.jsp JavaScript part.

  </script>

</imui:head>

localeInfo.jsp JavaScript Part

  • Validation rule is described.
  • Internationalization input item is set to required: true.
jQuery(function() {
  // Validation rule
  var rules = {
      localeId: {
          required: true,
          maxlength: 64,
          id: true
      },
      displayName: {
          required: true,
          date: true
      }
  };

  // Validation message
  var messages = {
      localeId: {
          required: "ID is mandatory.",
          maxlength: "Please enter ID within 64 characters.",
          id: "ID should be ID format."
      },
      displayName: {
          required: "name is mandatory",
          maxlength: "Please enter name within 256 characters"
      }
  };

  var copyObject = function(obj) {
      var result = {};
      for (var k in obj) {
          result[k] = obj[k];
      }
      return result;
  };

  // Function to add validation rule dynamically
  var addValidateRules = function(rules, messages, src, dest) {
      rules[dest] = copyObject(rules[src]);
      messages[dest] = copyObject(messages[src]);
  };

  // Add validation rule dynamically
  var idx = 0;
  jQuery("#input_form [name=localeInfos] input").each(function() {
      var newName = "displayName_" + jQuery(this).attr("locale");
      jQuery(this).attr("name", newName);
      addValidateRules(rules, messages, "data_name", newName);

      if (idx++) {
          rules[newName].required = false;
      }
  });

  jQuery("#input_form [name=submit]").click(function() {
    if (imuiValidate("#input_form", rules, messages)) {
      var locales = {};
      jQuery("#input_form [name=localeInfos] input").each(function() {
        locales[$(this).attr("locale")] = $(this).val();
      });

      jQuery.ajax({
        type: "POST",
        url: "sample/localeInfo/validLocale",
        dataType: "json",
        data: {
          localeId: jQuery("#input_form [name=localeId]").val(),
          displayName: jQuery("#input_form [name=localeInfos] input:first").val(),
          localeInfos: ImJson.toJSONString(locales)
        },
        success: function(result) {
          if (result.error) {
            imuiShowErrorMessage(result.errorMessage, result.detailMessages);
            return;
          } else {
            imuiShowSuccessMessage(result.successMessage);
          }
        },
        error: function(request, textStatus, errorThrown) {
          imuiShowErrorMessage(request.statusText + "(" + request.status + ")", "");
        }
      });
    }
  });
});
  • Input item display
<form id="input_form" onsubmit="return false;">
  <table class="imui-form">
    <tbody>
      <tr>
        <th><label class="imui-required">ID</label></th>
        <td><imui:textbox name="localeId" style="ime-mode: disabled;" /></td>
      </tr>
      <tr>
        <th><label class="imui-required">name</label></th>
        <td>
          <table class="imui-table" name="localeInfos">
            <tbody>
              <c:forEach var="localeInfo" items="${localeInfoDtoList}">
                <tr>
                  <th><c:out value="${localeInfo.displayName}" /></th>
                  <td><input type="textbox" locale='<c:out value="${localeInfo.locale}"/>' style="width: 400px;" /></td>
                </tr>
              </c:forEach>
            </tbody>
          </table>
        </td>
      </tr>
    </tbody>
  </table>
  <imui:button id="submit-button" name="submit" value="send" class="imui-small-button" />
</form>

LocaleInfoAction.java (server side Java )

It is an action class of localeInfo.jsp.

  • It makes an adjustment to put tenant default language or system default language in a higher layer.
package sample.sastruts.action.localeinfo;

// import sentence is omitted.

public class LocaleInfoAction {

    public LocaleInfoDtoList localeInfoDtoList;

    @Execute(validator = false)
    public String index() {

        Locale defaultLocale = null;

        try {
            // Obtain locale from tenant information
            defaultLocale = new TenantInfoManager().getTenantInfo(true).getLocale();
        } catch (final AdminException ignore) {
            // Intentionally ignored.
        }

        if (null == defaultLocale) {
            // If the locale could not be obtained from tenant information, the locale will be obtained from system information
            defaultLocale = SystemLocale.getDefaultLocaleInfo().getLocale();
        }

        // Obtain all locale information from system information
        final LocaleInfo[] localeInfos = SystemLocale.getLocaleInfos();

        //
        // Generate lists such as default locale, other locale 1, or other locale 2...
        //
        localeInfoDtoList = new LocaleInfoDtoList();
        for (final LocaleInfo localeInfo : localeInfos) {
            // If the locale is matched, locale information will be stored
            if (defaultLocale.equals(localeInfo.getLocale())) {
                final LocaleInfoDto dto = new LocaleInfoDto();
                dto.locale = localeInfo.getLocale();
                dto.displayName = localeInfo.getDisplayName();
                localeInfoDtoList.add(dto);
            }
        }
        for (final LocaleInfo localeInfo : localeInfos) {
            // If the locale does not match, the locale information will be stored
            if (!defaultLocale.equals(localeInfo.getLocale())) {
                final LocaleInfoDto dto = new LocaleInfoDto();
                dto.locale = localeInfo.getLocale();
                dto.displayName = localeInfo.getDisplayName();
                localeInfoDtoList.add(dto);
            }
        }

        return "/sample/localeInfo.jsp";
    }
}

LocaleInfoDto.java (server side Java)

It is DTO class of LocaleInfoAction.

  • Define the information to be used in LocaleInfoAction.
package sample.sastruts.dto.localeInfo;

import java.util.Locale;

public class LocaleInfoDto {

    public Locale locale;

    public String displayName;
}

LocaleInfoAjaxResponse.java (server side Java)

It is DTO class for communication response.

  • Define the information that is returned at LocaleInfoAction#validateLocale.
package sample.sastruts.dto.localeInfo;

public class LocaleInfoAjaxResponse {
    public String result;
    public boolean error;
    public String errorMessage;
    public String successMessage;
    public String[] detailMessages;
    public Map<String, Object> parameter;
}

LocaleInfoAjaxAction.java (server side java)

Add action form and validateLocale method to LocaleInfoAction.
Return value of the method is null and it returns the character string in which
response object is converted to JSON format using ResponseUtil.write method.
public class LocaleInfoAjaxAction {

    @ActionForm
    @Resource
    protected LocaleInfoForm localeInfoForm;

    @Execute(validator = false)
    public String validateLocale() {
        LocaleInfoAjaxResponse response = new LocaleInfoAjaxResponse();

        Map localeInfoMap = JSON.decode(localeInfoForm.localeInfos);
        Set entrySet = localeInfoMap.entrySet();
        Iterator iterator = entrySet.iterator();
        while (iterator.hasNext()) {
            Map.Entry obj = (Map.Entry) iterator.next();

            String displayName = obj.getValue() == null ? "" : (String) obj.getValue();

            if (displayName.length() > 256) {

                // Validation error
                response.result = "error";
                response.error = true;
                response.errorMessage = "name exceeds 256 characters.";
                response.detailMessages = new String[] { "localeId = ".concat((String)obj.getKey()) };

                // Return response object by JSON character string
                ResponseUtil.write(JSON.encode(response), "application/json");
                return null;
            }

            // Supplement a part where internationalization item has not yet input
            if (displayName.isEmpty()) {
                String defaultValue = ""; // Specify the default value when it is uninput.
                localeInfoMap.put(obj.getKey(), defaultValue);
            }
        }

        response.result = "success";
        response.error = false;
        response.successMessage = "Succeeded.";

        // Return response object by JSON character string
        ResponseUtil.write(JSON.encode(response), "application/json");
        return null;
    }
}

About Resolution of Locale when displayed

  1. Name that corresponds to the locale stored in account context is displayed.
  2. If a name corresponding to the above locale cannot be obtained, a name corresponding to tenant locale will be displayed.
  3. If a name corresponding to the above locale cannot be obtained, and if there is any name which is associated with a locale other than described above, that name will be displayed.
  4. If none of above display item exists, character string meaning [Name Undefined] will be displayed.

About Expression of Mandatory Items

1. Input Form Policy – Mark the spot for item mandatory according to the notation system of mandatory input.
2. Location where you mark a spot for item mandatory
a. Item of mandatory item
Ⅰ. Example)Role name
b. Tenant locale language input item inside the mandatory items
Ⅰ. Whether the input item of tenant locale language is mandatory will be able to be judged from screen.
Ⅱ. Only the tenant default (mandatory) is displayed with asterisk as [Japanese``*``].
Ⅲ. Example) Role name┳Japanese
        ┣English
        ┗Chinese

Implementation Example

$(function() {
  var i = 0;
  $("#input_form input").each(function() {
    if (i == 0) {
      $(this).closest('tr').children('th').attr('class', 'imui-required'); // Mandatory class is given to th for the very first time here.
    }
    $(this).attr("name", "roleDisplayName_" + document.variableForm.localeId[i++].value);
  });
});

Contents