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

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

This example Play Framework source code file (Lang.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

exception, i18n, lang, lib, library, list, override, play, play framework, string

The Lang.java Play Framework example source code

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

import java.util.*;
import play.libs.*;

/**
 * A Lang supported by the application.
 */
public class Lang extends play.api.i18n.Lang {
    
    public final play.api.i18n.Lang underlyingLang;
    
    public Lang(play.api.i18n.Lang underlyingLang) {
        super(underlyingLang.language(), underlyingLang.country());
        this.underlyingLang = underlyingLang;
    }
    
    /**
     * A valid ISO Language Code.
     */
    public String language() {
        return underlyingLang.language();
    }
    
    /**
     * A valid ISO Country Code.
     */
    public String country() {
        return underlyingLang.country();
    }
    
    /**
     * The Lang code (such as fr or en-US).
     */
    public String code() {
        return underlyingLang.code();
    }
    
    /**
     * Convert to a Java Locale value.
     */
    public java.util.Locale toLocale() {
        return underlyingLang.toLocale();
    }

    @Override
    public boolean equals(Object other) {
        return underlyingLang.equals(other);
    }

    @Override
    public int hashCode() {
        return underlyingLang.hashCode();
    }
    
    /**
     * Create a Lang value from a code (such as fr or en-US).
     */
    public static Lang forCode(String code) {
        try {
            return new Lang(play.api.i18n.Lang.apply(code));
        } catch (Exception e) {
            return null;
        }
    }
    
    /**
     * Retrieve Lang availables from the application configuration.
     */
    public static List<Lang> availables() {
        List<play.api.i18n.Lang> langs = Scala.asJava(play.api.i18n.Lang.availables(play.api.Play.current()));
        List<play.i18n.Lang> result = new ArrayList<play.i18n.Lang>();
        for(play.api.i18n.Lang lang: langs) {
            result.add(new Lang(lang));
        }
        return result;
    }
    
    /**
     * Guess the preferred lang in the langs set passed as argument.
     * The first Lang that matches an available Lang wins, otherwise returns the first Lang available in this application.
     */
    public static Lang preferred(List<Lang> langs) {
        List<play.api.i18n.Lang> result = new ArrayList<play.api.i18n.Lang>();
        for(play.i18n.Lang lang: langs) {
            result.add(lang.underlyingLang);
        }
        return new Lang(play.api.i18n.Lang.preferred(Scala.toSeq(result), play.api.Play.current()));
    }

}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework Lang.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.