alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Play Framework/Scala example source code file (Messages.java)

This example Play Framework source code file (Messages.java) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Play Framework (and Scala) source code examples by using tags.

All credit for the original source code belongs to Play Framework; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Play Framework tags/keywords

api, boolean, buffer, collection, i18n, lang, list, locale, messages, mutable, object, play, play framework, string, t

The Messages.java Play Framework example source code

/*
 * Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
 */
package play.i18n;

import org.apache.commons.lang3.ArrayUtils;
import scala.collection.mutable.Buffer;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import play.api.i18n.Lang;

/**
 * High-level internationalisation API.
 */
public class Messages {

    private static Lang getLang(){
        Lang lang = null;
        if(play.mvc.Http.Context.current.get() != null) {
            lang = play.mvc.Http.Context.current().lang();
        } else {
            Locale defaultLocale = Locale.getDefault();
            lang = new Lang(defaultLocale.getLanguage(), defaultLocale.getCountry());
        }
        return lang;
    }

    /**
     * Converts the varargs to a scala buffer, 
     * takes care of wrapping varargs into a intermediate list if necessary
     * 
     * @param args the message arguments 
     * @return scala type for message processing
     */
    private static Buffer<Object> convertArgsToScalaBuffer(final Object... args) {
        return scala.collection.JavaConverters.asScalaBufferConverter(wrapArgsToListIfNeeded(args)).asScala();
    }

    /**
     * Wraps arguments passed into a list if necessary. 
     *
     * Returns the first value as is if it is the only argument and a subtype of `java.util.List` 
     * Otherwise, it calls Arrays.asList on args
     * @param args arguments as a List
     */
    static <T> List<T> wrapArgsToListIfNeeded(final T... args) {
        List<T> out = null;
        if (ArrayUtils.isNotEmpty(args)
            && args.length == 1
            && args[0] instanceof List){
            out = (List<T>) args[0];
        }else{
            out = Arrays.asList(args);
        }
        return out;
    }

    /**
    * Translates a message.
    *
    * Uses `java.text.MessageFormat` internally to format the message.
    *
    * @param lang the message lang
    * @param key the message key
    * @param args the message arguments
    * @return the formatted message or a default rendering if the key wasn't defined
    */
    public static String get(Lang lang, String key, Object... args) {
        Buffer<Object> scalaArgs = convertArgsToScalaBuffer(args);
        return play.api.i18n.Messages.apply(key, scalaArgs, lang);
    }

    /**
    * Translates the first defined message.
    *
    * Uses `java.text.MessageFormat` internally to format the message.
    *
    * @param lang the message lang
    * @param keys the messages keys
    * @param args the message arguments
    * @return the formatted message or a default rendering if the key wasn't defined
    */
    public static String get(Lang lang, List<String> keys, Object... args) {
        Buffer<String> keyArgs = scala.collection.JavaConverters.asScalaBufferConverter(keys).asScala();
        Buffer<Object> scalaArgs = convertArgsToScalaBuffer(args);
        return play.api.i18n.Messages.apply(keyArgs.toSeq(), scalaArgs, lang);
    }

    /**
    * Translates a message.
    *
    * Uses `java.text.MessageFormat` internally to format the message.
    *
    * @param key the message key
    * @param args the message arguments
    * @return the formatted message or a default rendering if the key wasn't defined
    */
    public static String get(String key, Object... args) {
        Buffer<Object> scalaArgs = convertArgsToScalaBuffer(args);
        return play.api.i18n.Messages.apply(key, scalaArgs, getLang());
    }

    /**
    * Translates the first defined message.
    *
    * Uses `java.text.MessageFormat` internally to format the message.
    *
    * @param keys the messages keys
    * @param args the message arguments
    * @return the formatted message or a default rendering if the key wasn't defined
    */
    public static String get(List<String> keys, Object... args) {
        Buffer<String> keyArgs = scala.collection.JavaConverters.asScalaBufferConverter(keys).asScala();
        Buffer<Object> scalaArgs = convertArgsToScalaBuffer(args);
        return play.api.i18n.Messages.apply(keyArgs.toSeq(), scalaArgs, getLang());
    }

    /**
    * Check if a message key is defined.
    * @param lang the message lang
    * @param key the message key
    * @return a Boolean
    */
    public static Boolean isDefined(Lang lang, String key) {
        return play.api.i18n.Messages.isDefinedAt(key, lang);
    }

    /**
    * Check if a message key is defined.
    * @param key the message key
    * @return a Boolean
    */
    public static Boolean isDefined(String key) {
        return play.api.i18n.Messages.isDefinedAt(key, getLang());
    }
}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework Messages.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.