public static String detect(String text) throws LangDetectException { Detector detector = DetectorFactory.create(0.5); detector.append(text); return detector.detect(); } }
public String getLanguage(String text) throws LangDetectException { Detector detector = DetectorFactory.create(); detector.append(text); return detector.detect(); }
String lang = ""; try { lang = detector.detect(); } catch (Exception e) { e.printStackTrace();
/** * Returns the locale for the given text based on the language detection library.<p> * * The result will be <code>null</code> if the detection fails or the detected locale is not configured * in the 'opencms-system.xml' as available locale.<p> * * @param text the text to retrieve the locale for * * @return the detected locale for the given text */ public static Locale getLocaleForText(String text) { // try to detect locale by language detector if (isNotEmptyOrWhitespaceOnly(text)) { try { Detector detector = DetectorFactory.create(); detector.append(text); String lang = detector.detect(); Locale loc = new Locale(lang); if (OpenCms.getLocaleManager().getAvailableLocales().contains(loc)) { return loc; } } catch (LangDetectException e) { LOG.debug(e); } } return null; }