use of org.haiku.haikudepotserver.dataobjects.NaturalLanguage in project haikudepotserver by haiku.
the class NaturalLanguageChooserTag method doStartTagInternal.
@Override
protected int doStartTagInternal() throws Exception {
ObjectContext context = getObjectContext();
TagWriter tagWriter = new TagWriter(pageContext.getOut());
List<NaturalLanguage> naturalLanguages = new ArrayList<>(NaturalLanguage.getAllPopular(context));
NaturalLanguage currentNaturalLanguage = NaturalLanguageWebHelper.deriveNaturalLanguage(context, (HttpServletRequest) pageContext.getRequest());
Collections.sort(naturalLanguages, Comparator.comparing(_NaturalLanguage::getCode));
tagWriter.startTag("span");
tagWriter.writeAttribute("class", "multipage-natural-language-chooser");
for (int i = 0; i < naturalLanguages.size(); i++) {
NaturalLanguage naturalLanguage = naturalLanguages.get(i);
if (0 != i) {
tagWriter.appendValue(" ");
}
if (currentNaturalLanguage != naturalLanguage) {
String path = (String) pageContext.getRequest().getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);
UriComponentsBuilder builder = ServletUriComponentsBuilder.newInstance().path(path);
builder.replaceQueryParam(WebConstants.KEY_NATURALLANGUAGECODE, naturalLanguage.getCode());
tagWriter.startTag("a");
tagWriter.writeAttribute("href", builder.build().toString());
tagWriter.appendValue(naturalLanguage.getCode());
// a
tagWriter.endTag();
} else {
tagWriter.startTag("strong");
tagWriter.writeAttribute("class", "banner-actions-text");
tagWriter.appendValue(naturalLanguage.getCode());
// strong
tagWriter.endTag();
}
}
// span
tagWriter.endTag();
return SKIP_BODY;
}
use of org.haiku.haikudepotserver.dataobjects.NaturalLanguage in project haikudepotserver by haiku.
the class NaturalLanguageWebHelper method deriveNaturalLanguage.
/**
* <p>This will look at parameters on the supplied request and will return a natural language. It will
* resort to English language if no other language is able to be derived.</p>
*/
public static NaturalLanguage deriveNaturalLanguage(ObjectContext context, HttpServletRequest request) {
Preconditions.checkNotNull(context);
if (null != request) {
String naturalLanguageCode = request.getParameter(WebConstants.KEY_NATURALLANGUAGECODE);
if (!Strings.isNullOrEmpty(naturalLanguageCode)) {
Optional<NaturalLanguage> naturalLanguageOptional = NaturalLanguage.tryGetByCode(context, naturalLanguageCode);
if (naturalLanguageOptional.isPresent()) {
return naturalLanguageOptional.get();
} else {
LOGGER.info("the natural language '{}' was specified, but was not able to be found", naturalLanguageCode);
}
}
// see if we can deduce it from the locale.
Locale locale = request.getLocale();
if (null != locale) {
Iterator<String> langI = Splitter.on(Pattern.compile("[-_]")).split(locale.toLanguageTag()).iterator();
if (langI.hasNext()) {
Optional<NaturalLanguage> naturalLanguageOptional = NaturalLanguage.tryGetByCode(context, langI.next());
if (naturalLanguageOptional.isPresent() && naturalLanguageOptional.get().getIsPopular()) {
return naturalLanguageOptional.get();
}
}
}
}
return NaturalLanguage.tryGetByCode(context, NaturalLanguage.CODE_ENGLISH).get();
}
Aggregations