Skip to content
This repository

Integrating C10N with Play Framework 2.0

A example of integrating c10n with Play Framework 2.0 with support for dynamically changing locale.

Note The example below will only work for Java projects.

Step 1: Add c10n dependency to Build.scala

object ApplicationBuild extends Build {
    // ...

    val appDependencies = Seq(
      // Add c10n dependency
      "c10n" % "c10n-core" % "1.0"
    )

    val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA).settings(
      // Add c10n repository
      resolvers += "C10N" at "https://raw.github.com/rodionmoiseev/c10n/master/c10n-mvn-repo/releases"
      // ...
    )
}

Step 2: Configure c10n at Application Startup

In your application global settings initialize c10n. If you don't have global settings object set one up first.

import c10n.*;

public class Global extends GlobalSettings {
     //...

    /**
     * {@inheritDoc}
     */
    @Override
    public void onStart(Application app) {
        //...
        initializeC10N();
    }

    /**
     * Set up c10n
     */
    public static void initializeC10N() {
        C10N.configure(new C10NConfigBase() {
            @Override
            protected void configure() {
                //... your custom c10n configuration ...

                //Tell Play to use our custom LocaleProvider
                setLocaleProvider(new PlayLocaleProvider());
            }
        });
    }
}

Step 3: Create Your Locale Provider

An example of how you can dynamically switch to the best possible language, using the LocaleProvider.

For illustration purposes, the example assumes that your application contains a language selection box, which, on change, will set the "C10NLocale" session attribute to one of the languages supported by Locale object (see Locale(String lang)).

import java.util.Locale;
import play.i18n.Lang;
import c10n.LocaleProvider;
import controllers.*;

public class PlayLocaleProvider implements LocaleProvider {
    @Override
    public Locale getLocale() {
        String language = null;

        try {
            //Retrieve the language set in the GUI
            language = MyAppController.session().get("C10NLocale");
        } catch (RuntimeException e) {
            //Handle [RuntimeException: There is no HTTP Context available from here.]
            //Since there is no context available, 
            //try to use http header to choose best locale
            Locale best = Lang.preferred(Lang.availables()).toLocale();
            if (best != null) {
                return best;
            }
        }
        if (language == null) {
            //If no preferred language could be determined.
            //Just fallback to some default locale of your choice
            return Locale.ENGLISH;
        }
        return new Locale(language)
    }
}

Step 4: Use C10N in Controllers and Views

Using c10n interfaces from controllers works as one would expect, for instance:

public class MyController extends Controller {
  private static final MyMessages msg = C10N.get(MyMessages.class);

  public static Result c10nExample() {
    return ok(msg.sayHello());
  }
}

To use c10n interfaces from view, you can pass the c10n interface to the view as an argument:

public class MyController extends Controller {
  public static Result c10nExample() {
    return ok(index.render(C10N.get(MyMessages.class)));
  }
}  

... and then reference it from the view:

@(msg: MyMessages)

<h1>
 @msg.sayHello
</h1>

Or create and use the c10n interface instance directly in the template:

@import c10n._

<h1>
 @C10N.get(classOf[MyMessages]).sayHello
</h1>

See also: Scala Integration

Something went wrong with that request. Please try again.