Created
December 10, 2012 15:42
-
-
Save biokys/4251326 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers; | |
import models.Language; | |
import play.Logger; | |
import play.Play; | |
import play.cache.Cache; | |
import play.i18n.Lang; | |
import play.mvc.Before; | |
import play.mvc.Controller; | |
import play.mvc.Http; | |
import play.mvc.Http.Cookie; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Trida slouzi jako interceptor pro jine controlery | |
* Spravuje jazykove cookies | |
* Pokud neni vracen pres url zadny jazyk (/en, /cz, atd.), pak se zkontroluje, zda neni nejaky jazyk | |
* jiz ulozen v cookies. Pokud neni, pak se urci defaultni jazyk podle browseru a ulozi do cookies. Pokud je, | |
* pak se pouzije hodnota z tohoto cookie. Pokud je vracen pres url jazyk, pak se updatuje cookie. | |
* | |
* Dale nastavime v aplikaci tento jazyk. Pokud neni jazyk podporovany, pak vezmemem implicitni jazyk podle browseru. | |
* | |
* Pote nastavime promennou pro reverzni routing. Pri vyhodnocovani routovacich adres, pote aplikace zohlednuje | |
* tento pridany jazyk | |
* | |
* Nakonec se nactou z application.conf property, ktere definuji jednotlive jazyky + jejich lokalni nazvy. Toto nacitani je | |
* urychleno pomoci cache. Proto dokud je cache nastavena, pak se nesaha do property filu. Tyto definice se posilaji do kazdeho | |
* view renderu | |
* | |
* @author biokys | |
* | |
*/ | |
public class LanguageResolver extends Controller { | |
public static final String LANG_COOKIE_NAME = "wallet-lang"; | |
@Before | |
static void setLang(String lang) { | |
String language = ""; | |
// pokud neni predan zadny jazyk, tak se podivame do cookies, jestli mame neco ulozeneho | |
if (lang == null) { | |
// objekt obsahujici cookie pro jazyky | |
Cookie cookie = Http.Request.current().cookies.get(LANG_COOKIE_NAME); | |
// pokud neexistuje cookie | |
if ( cookie == null) { | |
// nacteme defaultni jazyk | |
language = Lang.get(); | |
// a ulozime do cookies | |
response.setCookie(LANG_COOKIE_NAME, Lang.get()); | |
} else { | |
// pokud mame ulozeno v cookies info o jazyku | |
// pak jej nacteme | |
language = cookie.value; | |
} | |
} else { | |
// pokud je predan jazyk metode | |
// pak nastavime tento jazyk | |
language = lang; | |
// a take updatujem jazyk v cookies | |
response.setCookie(LANG_COOKIE_NAME, lang); | |
} | |
// nastavime jazyk a pokud zjistime ze neni podporovany | |
if (!Lang.set(language)) { | |
// nastavime defaultni | |
language = Lang.get(); | |
} | |
// nastavime jazyk pro reverzni routing | |
routeArgs.put("lang", language); | |
// posleme do view seznam podporovanych jazyku | |
renderArgs.put("languages",getLanguages()); | |
} | |
public static List<Language> getLanguages() { | |
List<Language> languages; | |
// pokusime se nacist definice jazyku z cache | |
if (Cache.get("languages_def") != null) { | |
Logger.debug("Taking language definitions from cache"); | |
// nalezli jsme | |
languages = (List<Language>)Cache.get("languages_def"); | |
} else { | |
// pokud jsme definice nenalezli | |
languages = new ArrayList<Language>(); | |
int loop = 1; | |
// pak prochazime pres konstanty v configu | |
// do chvile, nez zadnou nenajdeme | |
while (true) { | |
String text = Play.configuration.getProperty("language.definition." + loop); | |
if (text == null) { | |
break; | |
} | |
Language lng = new Language(text); | |
Logger.info("Found language definition: " + lng); | |
languages.add(lng); | |
// zvysime o 1 | |
loop++; | |
} | |
Logger.info("Inserting " + languages.size() + " language definition(s) to cache"); | |
// na konci vlozime do cache | |
Cache.add("languages_def", languages, "1h"); | |
} | |
return languages; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment