List of usage examples for java.util Locale Locale
public Locale(String language)
From source file:org.joinfaces.mock.JsfMock.java
public void init(ApplicationContext applicationContext) { this.mockFacesContext = FacesContextMocker.mockFacesContext(); this.mockApplication = Mockito.mock(Application.class); this.mockViewRoot = Mockito.mock(UIViewRoot.class); this.mockExternalContext = Mockito.mock(ExternalContext.class); this.mockHttpServletRequest = Mockito.mock(HttpServletRequest.class); this.mockHttpServletResponse = Mockito.mock(HttpServletResponse.class); this.mockHttpSession = Mockito.mock(HttpSession.class); this.mockTagConfig = Mockito.mock(TagConfig.class); this.mockFaceletHandler = new MockFaceletHandler(); this.mockTagAttributes = new MockTagAttributes(); this.mockTag = new Tag(null, null, null, null, this.mockTagAttributes); this.mockFaceletContext = new MockFaceletContext(this.mockFacesContext); this.mockViewMap = new HashMap<String, Object>(); this.mockServletContext = new MockServletContext(); if (applicationContext != null) { this.mockServletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext);//from w ww. j a v a 2s . co m } Mockito.when(this.mockTagConfig.getNextHandler()).thenReturn(this.mockFaceletHandler); Mockito.when(this.mockTagConfig.getTag()).thenReturn(this.mockTag); Mockito.when(this.mockFacesContext.getApplication()).thenReturn(this.mockApplication); Mockito.when(this.mockApplication.getSupportedLocales()).thenReturn(createLocales().iterator()); Mockito.when(this.mockFacesContext.getViewRoot()).thenReturn(this.mockViewRoot); Mockito.when(this.mockViewRoot.getLocale()).thenReturn(new Locale("en")); Mockito.when(this.mockViewRoot.getViewMap()).thenReturn(this.mockViewMap); Mockito.when(this.mockFacesContext.getExternalContext()).thenReturn(this.mockExternalContext); Mockito.when(this.mockExternalContext.getRequest()).thenReturn(this.mockHttpServletRequest); Mockito.when(this.mockHttpServletRequest.getSession()).thenReturn(this.mockHttpSession); Mockito.when(this.mockExternalContext.getResponse()).thenReturn(this.mockHttpServletResponse); Mockito.when(this.mockExternalContext.getContext()).thenReturn(this.mockServletContext); Map<String, String> requestMap = new HashMap<String, String>(); Mockito.when(this.mockExternalContext.getRequestParameterMap()).thenReturn(requestMap); }
From source file:com.github.persapiens.jsfboot.mock.JsfMock.java
public void init(ApplicationContext applicationContext) { this.mockFacesContext = FacesContextMocker.mockFacesContext(); this.mockApplication = Mockito.mock(Application.class); this.mockViewRoot = Mockito.mock(UIViewRoot.class); this.mockExternalContext = Mockito.mock(ExternalContext.class); this.mockHttpServletRequest = Mockito.mock(HttpServletRequest.class); this.mockHttpServletResponse = Mockito.mock(HttpServletResponse.class); this.mockHttpSession = Mockito.mock(HttpSession.class); this.mockTagConfig = Mockito.mock(TagConfig.class); this.mockFaceletHandler = new MockFaceletHandler(); this.mockTagAttributes = new MockTagAttributes(); this.mockTag = new Tag(null, null, null, null, this.mockTagAttributes); this.mockFaceletContext = new MockFaceletContext(this.mockFacesContext); this.mockViewMap = new HashMap<>(); this.mockServletContext = new MockServletContext(); if (applicationContext != null) { this.mockServletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext);/*w w w. jav a 2 s. c o m*/ } Mockito.when(this.mockTagConfig.getNextHandler()).thenReturn(this.mockFaceletHandler); Mockito.when(this.mockTagConfig.getTag()).thenReturn(this.mockTag); Mockito.when(this.mockFacesContext.getApplication()).thenReturn(this.mockApplication); Mockito.when(this.mockApplication.getSupportedLocales()).thenReturn(createLocales().iterator()); Mockito.when(this.mockFacesContext.getViewRoot()).thenReturn(this.mockViewRoot); Mockito.when(this.mockViewRoot.getLocale()).thenReturn(new Locale("en")); Mockito.when(this.mockViewRoot.getViewMap()).thenReturn(this.mockViewMap); Mockito.when(this.mockFacesContext.getExternalContext()).thenReturn(this.mockExternalContext); Mockito.when(this.mockExternalContext.getRequest()).thenReturn(this.mockHttpServletRequest); Mockito.when(this.mockHttpServletRequest.getSession()).thenReturn(this.mockHttpSession); Mockito.when(this.mockExternalContext.getResponse()).thenReturn(this.mockHttpServletResponse); Mockito.when(this.mockExternalContext.getContext()).thenReturn(this.mockServletContext); Map<String, String> requestMap = new HashMap<>(); Mockito.when(this.mockExternalContext.getRequestParameterMap()).thenReturn(requestMap); }
From source file:no.dusken.barweb.view.InvoiceView.java
private Document generateXml(Map<BarPerson, Integer> persons, Gjeng gjeng, Invoice invoice) { Document dom = null;/*from w ww.j av a 2 s . co m*/ //get an instance of factory DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { //get an instance of builder DocumentBuilder db = dbf.newDocumentBuilder(); //create an instance of DOM dom = db.newDocument(); } catch (ParserConfigurationException pce) { log.error("Error when generating invoice"); } Element root = createInvoiceElement(invoice, gjeng, dom); SimpleDateFormat dateformat = new SimpleDateFormat("dd. MMMMMMMMM yyyy - HH:mm", new Locale("no")); root.setAttribute("generated", dateformat.format((new GregorianCalendar()).getTime())); root.setAttribute("magicNumber", String.valueOf(((new Random()).nextDouble() * 1000))); dom.appendChild(root); Element personsEle = dom.createElement("persons"); for (Map.Entry<BarPerson, Integer> p : persons.entrySet()) { Element personEle = createPersonElement(p.getKey(), p.getValue(), dom); personsEle.appendChild(personEle); } root.appendChild(personsEle); return dom; }
From source file:com.inkubator.sms.gateway.web.LoginController.java
public String doLogin() { System.out.println(" do login"); ExternalContext context = FacesUtil.getExternalContext(); RequestDispatcher dispatcher = ((ServletRequest) context.getRequest()) .getRequestDispatcher("/" + SMSGATEWAY.SPRING_SECURITY_CHECK); try {//from ww w.j ava 2 s . com dispatcher.forward((ServletRequest) context.getRequest(), (ServletResponse) context.getResponse()); } catch (ServletException | IOException ex) { LOGGER.error("Error", ex); } FacesUtil.setSessionAttribute(SMSGATEWAY.LOGIN_DATE, dateFormatter.getDateFullAsStringsWithActiveLocale(new Date(), new Locale(selectedLanguage))); FacesUtil.getFacesContext().responseComplete(); return null; }
From source file:net.felsing.client_cert.GetCountries.java
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { final Locale defaultLanguage = Locale.ENGLISH; String bestLanguage = null;/*from w w w . ja v a 2 s . c om*/ resp.setContentType("application/json; charset=UTF-8"); resp.setCharacterEncoding("UTF-8"); final String acceptLanguage = StringEscapeUtils.escapeJava(req.getHeader("Accept-Language")); List<Locale.LanguageRange> languageRanges = Locale.LanguageRange.parse(acceptLanguage); for (Locale.LanguageRange l : languageRanges) { try { if (bestLanguage == null) { Locale locale = new Locale(l.getRange().split("-")[0]); bestLanguage = locale.getISO3Language(); logger.debug("bestLanguage: " + bestLanguage); usedLocale = locale; } } catch (Exception e) { // do nothing } } if (bestLanguage == null) { logger.debug("No sufficient language found for " + acceptLanguage); bestLanguage = defaultLanguage.getISO3Language(); usedLocale = Locale.ENGLISH; } logger.debug("usedLocale: " + usedLocale.getISO3Language()); PrintWriter pw = resp.getWriter(); loadFromJson(context); List<Country> countriesList = new ArrayList<>(); for (JsonElement jsonElement : countries) { String cca2 = jsonElement.getAsJsonObject().get("cca2").toString().replaceAll("\\\"", ""); cca2 = cca2.replaceAll("\\\\", ""); Country countryItem = new Country(); try { countryItem.name = jsonElement.getAsJsonObject().get("name").getAsJsonObject().get(bestLanguage) .getAsJsonObject().get("common").getAsString(); } catch (NullPointerException e) { countryItem.name = jsonElement.getAsJsonObject().get("name_").getAsString(); } countryItem.cca2 = cca2; countriesList.add(countryItem); } Collator coll = Collator.getInstance(usedLocale); coll.setStrength(Collator.PRIMARY); Collections.sort(countriesList); pw.print(new Gson().toJson(countriesList)); pw.flush(); }
From source file:cn.quickj.Setting.java
/** * setting.xml???/*from ww w .j a v a 2s. c o m*/ * * @param rootPath * @throws Exception * */ public static void load(String rootPath) throws Exception { if (initFinished == true) return; XMLConfiguration config; String settingPath = ""; if (rootPath == null) { // WebApplication Setting.xml String classPath = Thread.currentThread().getContextClassLoader().getResource("").getPath(); Pattern p = Pattern.compile("([\\S]+/)WEB-INF[\\S]+"); Matcher m = p.matcher(classPath); if (m.find()) webRoot = m.group(1); } else webRoot = rootPath; try { webRoot = URLDecoder.decode(webRoot, "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if (webRoot.endsWith(".xml")) { settingPath = webRoot; int lastPos = webRoot.lastIndexOf('\\'); if (lastPos == -1) lastPos = webRoot.lastIndexOf('/'); webRoot = webRoot.substring(0, lastPos); } else { if (!webRoot.endsWith("/") && !webRoot.endsWith("\\")) webRoot = webRoot + "/"; settingPath = webRoot + "WEB-INF/setting.xml"; } log.info("Loading setting file:" + settingPath); config = new XMLConfiguration(); // ?????? config.setDelimiterParsingDisabled(true); config.load(settingPath); /* * @Deprecated * ????unittest?runserver? * war?XML? String strRunMode = * config.getString("runmode", "development"); if * (strRunMode.equals("production")) runMode = PROD_MODE; else if * (strRunMode.equals("test")) runMode = TEST_MODE; else runMode = * DEV_MODE; */ String strRunMode = getRunMode(); packageRoot = config.getString("package", packageRoot); fieldBySetter = config.getBoolean("web.fieldBySetter", false); DEFAULT_CHARSET = config.getString("web.charset", "utf-8"); longDateFormat = config.getString("web.long-dateformat", "yyyy-MM-dd HH:mm:ss"); license = config.getString("license"); shortDateFormat = config.getString("web.short-dateformat", "yyyy-MM-dd"); String strLocale = config.getString("web.locale", Locale.getDefault().getDisplayName()); theme = config.getString("web.theme"); locale = new Locale(strLocale); sessionClass = config.getString("web.session.class", "cn.quickj.session.MemHttpSession"); sessionDomain = config.getString("web.session.domain", null); sessionTimeOut = config.getInt("web.session.timeout", 30 * 60); defaultUri = config.getString("web.defaultUri"); uploadDir = config.getString("web.upload.directory", System.getProperty("java.io.tmpdir")); uploadMaxSize = config.getInt("web.upload.max-size", 4096); jdbcDriver = config.getString("database." + strRunMode + ".driver", ""); usedb = (jdbcDriver.length() != 0); jdbcUser = config.getString("database." + strRunMode + ".user", "root"); jdbcUrl = config.getString("database." + strRunMode + ".url", ""); jdbcPassword = config.getString("database." + strRunMode + ".password", ""); maxActive = config.getInt("database." + strRunMode + ".pool.maxActive", 10); initActive = config.getInt("database." + strRunMode + ".pool.initActive", 2); maxIdle = config.getInt("database." + strRunMode + ".pool.maxIdle", 1800); dialect = config.getString("database." + strRunMode + ".dialect", null); tablePrefix = config.getString("database.prefix", ""); cacheClass = config.getString("cache.class", "cn.quickj.cache.SimpleCache"); cacheParam = config.getString("cache.param", "capacity=50000"); // ?? queueEnabled = "true".equalsIgnoreCase(config.getString("queue.enable", "false")); queueParam = config.getString("queue.param", "capacity=50000"); loadPlugin(config); loadApplicationConfig(); initFinished = true; }
From source file:de.cosmocode.commons.converter.LocaleLanguageIsoConverter.java
@Override public Locale decode(String input) { return new Locale(toTwoLetter(input)); }
From source file:sk.openhouse.web.recaptcha.LocaleReCaptchaImpl.java
@Override public String createRecaptchaHtml(String errorMessage, Properties options) { String errorPart = ""; if (errorMessage != null) { String encoding = "UTF-8"; try {// ww w .ja v a 2s. co m errorPart = "&error=" + URLEncoder.encode(errorMessage, encoding); } catch (UnsupportedEncodingException ex) { logger.fatal(encoding + " encoding is not supported", ex); } } if (options == null) { options = new Properties(); } String prefix = "reCaptcha."; /* message key with default value */ Map<String, String> keys = new HashMap<String, String>(); keys.put("visual_challenge", "Get a visual challenge"); keys.put("audio_challenge", "Get an audio challenge"); keys.put("refresh_btn", "Get a new challenge"); keys.put("instructions_visual", "Type the two words:"); keys.put("instructions_context", "Type the words in the boxes:"); keys.put("instructions_audio", "Type what you hear:"); keys.put("help_btn", "Help"); keys.put("play_again", "Play sound again"); keys.put("cant_hear_this", "Download sound as MP3"); keys.put("incorrect_try_again", "Incorrect. Try again."); /* get locale from properties if set */ Locale locale = null; String langKey = "lang"; if (options.containsKey(langKey)) { List<String> langs = Arrays.asList(Locale.getISOLanguages()); String lang = options.getProperty(langKey); if (langs.contains(lang)) { locale = new Locale(lang); } } /* if locale is not set, get it from context */ if (locale == null) { locale = LocaleContextHolder.getLocale(); } /* load messages for a given locale, if it fails, default to EN */ StringBuilder sb = new StringBuilder("{"); try { for (Map.Entry<String, String> entry : keys.entrySet()) { String key = entry.getKey(); sb.append(key); sb.append(":\""); sb.append(messages.getMessage(prefix + key, locale)); sb.append("\","); } } catch (NoSuchMessageException ex) { locale = Locale.ENGLISH; for (Map.Entry<String, String> entry : keys.entrySet()) { String key = entry.getKey(); sb.append(key); sb.append(":\""); sb.append(messages.getMessage(prefix + key, entry.getValue(), locale)); sb.append("\","); } } sb.deleteCharAt(sb.length() - 1); sb.append("}"); options.put("custom_translations", sb.toString()); options.put("lang", locale.getLanguage()); String message = fetchJSOptions(options); message += "<script type=\"text/javascript\" src=\"" + recaptchaServer + "/challenge?k=" + publicKey + errorPart + "\"></script>\r\n"; if (includeNoscript) { String noscript = "<noscript>\r\n" + " <iframe src=\"" + recaptchaServer + "/noscript?k=" + publicKey + errorPart + "\" height=\"300\" width=\"500\" frameborder=\"0\"></iframe><br>\r\n" + " <textarea name=\"recaptcha_challenge_field\" rows=\"3\" cols=\"40\"></textarea>\r\n" + " <input type=\"hidden\" name=\"recaptcha_response_field\" value=\"manual_challenge\">\r\n" + "</noscript>"; message += noscript; } return message; }
From source file:com.isalnikov.config.web.WebConfig.java
@Bean public LocaleResolver localeResolver() { CookieLocaleResolver localeResolver = new CookieLocaleResolver(); localeResolver.setDefaultLocale(new Locale("ru")); // change this Locale.ENGLISH) localeResolver.setCookieName("terminal_cookie"); localeResolver.setCookieMaxAge(60 * 30); return localeResolver; }
From source file:org.netxilia.api.impl.value.NumberParser.java
@Override public void afterPropertiesSet() throws Exception { this.locale = localeId != null ? new Locale(localeId) : Locale.getDefault(); this.formats = getNumberFormats(); // Use the appropriate currency symbol if our locale has a country, otherwise try the dollar sign! if (locale.getCountry() != null && !"".equals(locale.getCountry())) { this.currencySymbol = Currency.getInstance(locale).getSymbol(locale); } else {//w ww . jav a 2s.c o m this.currencySymbol = "$"; } }