I18nModuleBuilder extends JavaScriptModuleBuilder { List<String> parseAcceptLanguageHeader(HttpServletRequest request) { String header = request.getHeader("Accept-Language"); List<String> result = Collections.emptyList(); if (header != null) { PriorityQueue<Prioritized<String>> queue = new PriorityQueue<Prioritized<String>>(10, Prioritized.comparator); String[] toks = header.split(","); for (String tok : toks) { int idx = tok.indexOf(";"); String locale = tok; double priority = 1.0; if (idx != -1) { locale = tok.substring(0, idx); String q = tok.substring(idx+1); if (q.startsWith("q=")) { try { priority = Double.parseDouble(q.substring(2)); } catch (NumberFormatException e) { continue; } } else { continue; } } queue.offer(new Prioritized<String>(locale, priority)); } result = new LinkedList<String>(); while (!queue.isEmpty()) { result.add(queue.poll().value); } Collections.reverse(result); } return result; }  @Override ModuleBuild build(String mid, IResource resource,
			HttpServletRequest request, List<ICacheKeyGenerator> keyGens); @Override String layerBeginEndNotifier(EventType type, HttpServletRequest request, List<IModule> modules, Set<String> dependentFeatures); @Override List<ICacheKeyGenerator> getCacheKeyGenerators(IAggregator aggregator); @Override boolean handles(String mid, IResource resource); static String OPTION_DISABLE_LOCALE_EXPANSION; }
@Test public void testParseAcceptLanguageHeader() { HttpServletRequest mockRequest = EasyMock.createMock(HttpServletRequest.class); EasyMock.expect(mockRequest.getHeader("Accept-Language")).andReturn("p05;q=0.5,p01;q=0.1,p1").anyTimes(); EasyMock.replay(mockRequest); I18nModuleBuilder builder = new I18nModuleBuilder(); List<String> result = builder.parseAcceptLanguageHeader(mockRequest); Assert.assertEquals(Arrays.asList(new String[]{"p1", "p05", "p01"}), result); EasyMock.reset(mockRequest); EasyMock.expect(mockRequest.getHeader("Accept-Language")).andReturn("p05;q=0.5,p1;q=1,p01;q=0.1").anyTimes(); EasyMock.replay(mockRequest); result = builder.parseAcceptLanguageHeader(mockRequest); Assert.assertEquals(Arrays.asList(new String[]{"p1", "p05", "p01"}), result); EasyMock.reset(mockRequest); EasyMock.expect(mockRequest.getHeader("Accept-Language")).andReturn("p05;q=0.5,p1;q=1,pX;q=foo,p01;q=0.1").anyTimes(); EasyMock.replay(mockRequest); result = builder.parseAcceptLanguageHeader(mockRequest); Assert.assertEquals(Arrays.asList(new String[]{"p1", "p05", "p01"}), result); EasyMock.reset(mockRequest); EasyMock.expect(mockRequest.getHeader("Accept-Language")).andReturn("p05;q=0.5,p1;q=1,pX;blah,p01;q=0.1").anyTimes(); EasyMock.replay(mockRequest); result = builder.parseAcceptLanguageHeader(mockRequest); Assert.assertEquals(Arrays.asList(new String[]{"p1", "p05", "p01"}), result); }