@BeforeClass public static void setUpClass() throws Exception { _defaultLocale = LocaleUtil.getDefault(); LocaleUtil.setDefault( LocaleUtil.US.getLanguage(), LocaleUtil.US.getCountry(), LocaleUtil.US.getVariant()); }
@Test public void testFriendlyURLCollision() throws Exception { String defaultLanguageId = LocaleUtil.toLanguageId(LocaleUtil.getDefault()); Layout layoutA = LayoutTestUtil.addLayout(group.getGroupId(), "layoutA"); layoutA = LayoutLocalServiceUtil.updateFriendlyURL(layoutA.getPlid(), "/layoutA-de", "de"); Layout layoutB = LayoutTestUtil.addLayout(group.getGroupId(), "layoutB"); layoutB = LayoutLocalServiceUtil.updateFriendlyURL(layoutB.getPlid(), "/layoutB-de", "de"); long[] layoutIds = {layoutA.getLayoutId(), layoutB.getLayoutId()}; exportImportLayouts(layoutIds, getImportParameterMap()); layoutA = LayoutLocalServiceUtil.updateFriendlyURL(layoutA.getPlid(), "/temp", defaultLanguageId); layoutA = LayoutLocalServiceUtil.updateFriendlyURL(layoutA.getPlid(), "/temp-de", "de"); layoutB = LayoutLocalServiceUtil.updateFriendlyURL(layoutB.getPlid(), "/layoutA", defaultLanguageId); LayoutLocalServiceUtil.updateFriendlyURL(layoutB.getPlid(), "/layoutA-de", "de"); layoutA = LayoutLocalServiceUtil.updateFriendlyURL(layoutA.getPlid(), "/layoutB", defaultLanguageId); LayoutLocalServiceUtil.updateFriendlyURL(layoutA.getPlid(), "/layoutB-de", "de"); exportImportLayouts(layoutIds, getImportParameterMap()); }
protected boolean validateUniqueFieldNames(ActionRequest actionRequest) { Locale defaultLocale = LocaleUtil.getDefault(); Set<String> localizedUniqueFieldNames = new HashSet<String>(); int[] formFieldsIndexes = StringUtil.split(ParamUtil.getString(actionRequest, "formFieldsIndexes"), 0); for (int formFieldsIndex : formFieldsIndexes) { Map<Locale, String> fieldLabelMap = LocalizationUtil.getLocalizationMap(actionRequest, "fieldLabel" + formFieldsIndex); if (Validator.isNull(fieldLabelMap.get(defaultLocale))) { continue; } for (Locale locale : fieldLabelMap.keySet()) { String fieldLabelValue = fieldLabelMap.get(locale); if (Validator.isNull(fieldLabelValue)) { continue; } String languageId = LocaleUtil.toLanguageId(locale); if (!localizedUniqueFieldNames.add(languageId + "_" + fieldLabelValue)) { return false; } } } return true; }
public String[] getAvailableLanguageIds() { Set<String> availableLanguageIds = new TreeSet<String>(); Map<Locale, String> nameMap = getNameMap(); for (Map.Entry<Locale, String> entry : nameMap.entrySet()) { Locale locale = entry.getKey(); String value = entry.getValue(); if (Validator.isNotNull(value)) { availableLanguageIds.add(LocaleUtil.toLanguageId(locale)); } } Map<Locale, String> descriptionMap = getDescriptionMap(); for (Map.Entry<Locale, String> entry : descriptionMap.entrySet()) { Locale locale = entry.getKey(); String value = entry.getValue(); if (Validator.isNotNull(value)) { availableLanguageIds.add(LocaleUtil.toLanguageId(locale)); } } return availableLanguageIds.toArray(new String[availableLanguageIds.size()]); }
protected Field createField( DDMStructure ddmStructure, String fieldName, List<Serializable> fieldValues, ServiceContext serviceContext) { Field field = new Field(); field.setDDMStructureId(ddmStructure.getStructureId()); String languageId = GetterUtil.getString( serviceContext.getAttribute("languageId"), serviceContext.getLanguageId()); Locale locale = LocaleUtil.fromLanguageId(languageId); String defaultLanguageId = GetterUtil.getString(serviceContext.getAttribute("defaultLanguageId")); Locale defaultLocale = LocaleUtil.fromLanguageId(defaultLanguageId); if (fieldName.startsWith(StringPool.UNDERLINE)) { locale = LocaleUtil.getSiteDefault(); defaultLocale = LocaleUtil.getSiteDefault(); } field.setDefaultLocale(defaultLocale); field.setName(fieldName); field.setValues(locale, fieldValues); return field; }
/** * Returns the language ID of the locale of this service context's current user. * * @return the language ID */ public String getLanguageId() { if (_languageId != null) { return _languageId; } return LocaleUtil.toLanguageId(LocaleUtil.getMostRelevantLocale()); }
@Test public void testGetPreferencesValue() throws Exception { String key = RandomTestUtil.randomString(); LocaleUtil.setDefault( LocaleUtil.US.getLanguage(), LocaleUtil.US.getCountry(), LocaleUtil.US.getVariant()); PortletPreferences preferences = new PortletPreferencesImpl(); LocalizationUtil.setPreferencesValue(preferences, key, _ENGLISH_LANGUAGE_ID, "A"); LocalizationUtil.setPreferencesValue(preferences, key, _GERMAN_LANGUAGE_ID, "B"); Assert.assertEquals( "A", LocalizationUtil.getPreferencesValue(preferences, key, _ENGLISH_LANGUAGE_ID)); Assert.assertEquals( "B", LocalizationUtil.getPreferencesValue(preferences, key, _GERMAN_LANGUAGE_ID)); Assert.assertEquals( "A", LocalizationUtil.getPreferencesValue(preferences, key, _SPANISH_LANGUAGE_ID)); LocaleUtil.setDefault( LocaleUtil.GERMANY.getLanguage(), LocaleUtil.GERMANY.getCountry(), LocaleUtil.GERMANY.getVariant()); Assert.assertEquals( "A", LocalizationUtil.getPreferencesValue(preferences, key, _ENGLISH_LANGUAGE_ID)); Assert.assertEquals( "B", LocalizationUtil.getPreferencesValue(preferences, key, _GERMAN_LANGUAGE_ID)); Assert.assertEquals( "B", LocalizationUtil.getPreferencesValue(preferences, key, _SPANISH_LANGUAGE_ID)); }
public void setUp() { _english = new Locale("en", "US"); _englishId = LocaleUtil.toLanguageId(_english); _german = new Locale("de", "DE"); _germanId = LocaleUtil.toLanguageId(_german); }
protected PollsQuestion updatePollsQuestion( PollsQuestion pollsQuestion, String title, String description) throws Exception { Map<Locale, String> titleMap = new HashMap<Locale, String>(); Map<Locale, String> descriptionMap = new HashMap<Locale, String>(); for (Locale locale : _locales) { titleMap.put(locale, title.concat(LocaleUtil.toLanguageId(locale))); descriptionMap.put(locale, description.concat(LocaleUtil.toLanguageId(locale))); } return PollsQuestionLocalServiceUtil.updateQuestion( pollsQuestion.getUserId(), pollsQuestion.getQuestionId(), titleMap, descriptionMap, 0, 0, 0, 0, 0, true, pollsQuestion.getChoices(), ServiceTestUtil.getServiceContext()); }
@Test public void testUpdateLocalization() { Map<Locale, String> localizationMap = new HashMap<>(); localizationMap.put(LocaleUtil.US, _ENGLISH_HELLO); StringBundler sb = new StringBundler(); sb.append("<?xml version='1.0' encoding='UTF-8'?>"); sb.append("<root available-locales=\"en_US,de_DE\" "); sb.append("default-locale=\"en_US\">"); sb.append("<greeting language-id=\"de_DE\">"); sb.append("<![CDATA[Beispiel auf Deutsch]]>"); sb.append("</greeting>"); sb.append("<greeting language-id=\"en_US\">"); sb.append("<![CDATA[Example in English]]>"); sb.append("</greeting>"); sb.append("</root>"); String xml = LocalizationUtil.updateLocalization( localizationMap, sb.toString(), "greeting", LocaleUtil.toLanguageId(LocaleUtil.getDefault())); Assert.assertEquals( _ENGLISH_HELLO, LocalizationUtil.getLocalization(xml, _ENGLISH_LANGUAGE_ID, false)); Assert.assertEquals( StringPool.BLANK, LocalizationUtil.getLocalization(xml, _GERMAN_LANGUAGE_ID, false)); }
protected PollsQuestion addPollsQuestion(long groupId, String title, String description) throws Exception { Map<Locale, String> titleMap = new HashMap<Locale, String>(); Map<Locale, String> descriptionMap = new HashMap<Locale, String>(); for (Locale locale : _locales) { titleMap.put(locale, title.concat(LocaleUtil.toLanguageId(locale))); descriptionMap.put(locale, description.concat(LocaleUtil.toLanguageId(locale))); } List<PollsChoice> pollsChoices = new ArrayList<PollsChoice>(); pollsChoices.add(addPollsChoice("optionA", "descriptionA")); pollsChoices.add(addPollsChoice("optionB", "descriptionB")); ServiceContext serviceContext = ServiceTestUtil.getServiceContext(); serviceContext.setScopeGroupId(groupId); return PollsQuestionLocalServiceUtil.addQuestion( TestPropsValues.getUserId(), titleMap, descriptionMap, 0, 0, 0, 0, 0, true, pollsChoices, serviceContext); }
protected static String getOrderByCol( String sortField, String fieldType, int sortType, Locale locale) { if (sortField.startsWith(DDMStructureManager.STRUCTURE_INDEXER_FIELD_PREFIX)) { sortField = sortField.concat(StringPool.UNDERLINE).concat(LocaleUtil.toLanguageId(locale)); if (!fieldType.equals("ddm-date") && ((sortType == Sort.DOUBLE_TYPE) || (sortType == Sort.FLOAT_TYPE) || (sortType == Sort.INT_TYPE) || (sortType == Sort.LONG_TYPE))) { sortField = sortField.concat(StringPool.UNDERLINE).concat("Number"); } sortField = DocumentImpl.getSortableFieldName(sortField); } else if (sortField.equals("modifiedDate")) { sortField = Field.MODIFIED_DATE; } else if (sortField.equals("title")) { sortField = DocumentImpl.getSortableFieldName( "localized_title_".concat(LocaleUtil.toLanguageId(locale))); } return sortField; }
@BeforeClass public static void setUpClass() { _CLASS_NAME_ID = PortalUtil.getClassNameId(StringUtil.randomString()); _enLocale = LocaleUtil.fromLanguageId("en_US"); _ptLocale = LocaleUtil.fromLanguageId("pt_BR"); _jsonStorageAdapter = StorageAdapterRegistryUtil.getStorageAdapter(StorageType.JSON.toString()); }
protected Locale getDefaultLocale(HttpServletRequest httpServletRequest) { String defaultLanguageId = ParamUtil.getString(httpServletRequest, "defaultLanguageId"); if (Validator.isNull(defaultLanguageId)) { return LocaleUtil.getSiteDefault(); } return LocaleUtil.fromLanguageId(defaultLanguageId); }
@BeforeClass public static void setUpClass() { _CLASS_NAME_ID = PortalUtil.getClassNameId("com.liferay.dynamic.data.lists.model.DDLRecordSet"); _enLocale = LocaleUtil.fromLanguageId("en_US"); _ptLocale = LocaleUtil.fromLanguageId("pt_BR"); _jsonStorageAdapter = StorageAdapterRegistryUtil.getStorageAdapter(StorageType.JSON.toString()); }
@Override public String getFriendlyURLsXML() throws SystemException { Map<Locale, String> friendlyURLMap = getFriendlyURLMap(); return LocalizationUtil.updateLocalization( friendlyURLMap, StringPool.BLANK, "FriendlyURL", LocaleUtil.toLanguageId(LocaleUtil.getDefault())); }
protected void setUpLocaleUtil() { mockStatic(LocaleUtil.class); when(LocaleUtil.fromLanguageId("en_US")).thenReturn(LocaleUtil.US); when(LocaleUtil.fromLanguageId("pt_BR")).thenReturn(LocaleUtil.BRAZIL); when(LocaleUtil.toLanguageId(LocaleUtil.US)).thenReturn("en_US"); when(LocaleUtil.toLanguageId(LocaleUtil.BRAZIL)).thenReturn("pt_BR"); }
protected I18nData getI18nData(HttpServletRequest request) { String path = GetterUtil.getString(request.getPathInfo()); if (Validator.isNull(path)) { return null; } String i18nLanguageId = request.getServletPath(); int pos = i18nLanguageId.lastIndexOf(CharPool.SLASH); i18nLanguageId = i18nLanguageId.substring(pos + 1); if (_log.isDebugEnabled()) { _log.debug("Language ID " + i18nLanguageId); } if (Validator.isNull(i18nLanguageId)) { return null; } String i18nPath = StringPool.SLASH + i18nLanguageId; Locale locale = LocaleUtil.fromLanguageId(i18nLanguageId, true, false); String i18nLanguageCode = i18nLanguageId; if ((locale == null) || Validator.isNull(locale.getCountry())) { // Locales must contain the country code locale = LanguageUtil.getLocale(i18nLanguageCode); } if (locale != null) { i18nLanguageId = LocaleUtil.toLanguageId(locale); i18nLanguageCode = locale.getLanguage(); } if (!PropsValues.LOCALE_USE_DEFAULT_IF_NOT_AVAILABLE && !LanguageUtil.isAvailableLocale(i18nLanguageId)) { return null; } String redirect = path; if (_log.isDebugEnabled()) { _log.debug("Redirect " + redirect); } return new I18nData(i18nPath, i18nLanguageCode, i18nLanguageId, redirect); }
@Override public void prepareLocalizedFieldsForImport() throws LocaleException { Locale defaultLocale = LocaleUtil.fromLanguageId(getDefaultLanguageId()); Locale[] availableLocales = LocaleUtil.fromLanguageIds(getAvailableLanguageIds()); Locale defaultImportLocale = LocalizationUtil.getDefaultImportLocale( KaleoDefinition.class.getName(), getPrimaryKey(), defaultLocale, availableLocales); prepareLocalizedFieldsForImport(defaultImportLocale); }
public void setLocation(String location, Locale locale, Locale defaultLocale) { String languageId = LocaleUtil.toLanguageId(locale); String defaultLanguageId = LocaleUtil.toLanguageId(defaultLocale); if (Validator.isNotNull(location)) { setLocation( LocalizationUtil.updateLocalization( getLocation(), "Location", location, languageId, defaultLanguageId)); } else { setLocation(LocalizationUtil.removeLocalization(getLocation(), "Location", languageId)); } }
public void setName(String name, Locale locale, Locale defaultLocale) { String languageId = LocaleUtil.toLanguageId(locale); String defaultLanguageId = LocaleUtil.toLanguageId(defaultLocale); if (Validator.isNotNull(name)) { setName( LocalizationUtil.updateLocalization( getName(), "Name", name, languageId, defaultLanguageId)); } else { setName(LocalizationUtil.removeLocalization(getName(), "Name", languageId)); } }
protected void addDDMStructures( long userId, long groupId, long classNameId, String fileName, ServiceContext serviceContext) throws DocumentException, PortalException, SystemException { List<Element> structureElements = getDDMStructures(fileName); for (Element structureElement : structureElements) { boolean dynamicStructure = GetterUtil.getBoolean(structureElement.elementText("dynamic-structure")); if (dynamicStructure) { continue; } String name = structureElement.elementText("name"); String description = structureElement.elementText("description"); String ddmStructureKey = name; DDMStructure ddmStructure = DDMStructureLocalServiceUtil.fetchStructure(groupId, ddmStructureKey); if (ddmStructure != null) { continue; } Element structureElementRootElement = structureElement.element("root"); String xsd = structureElementRootElement.asXML(); Map<Locale, String> nameMap = new HashMap<Locale, String>(); nameMap.put(LocaleUtil.getDefault(), name); Map<Locale, String> descriptionMap = new HashMap<Locale, String>(); descriptionMap.put(LocaleUtil.getDefault(), description); DDMStructureLocalServiceUtil.addStructure( userId, groupId, classNameId, ddmStructureKey, nameMap, descriptionMap, xsd, "xml", DDMStructureConstants.TYPE_DEFAULT, serviceContext); } }
@Override public void setTitle(String title, Locale locale, Locale defaultLocale) { String languageId = LocaleUtil.toLanguageId(locale); String defaultLanguageId = LocaleUtil.toLanguageId(defaultLocale); if (Validator.isNotNull(title)) { setTitle( LocalizationUtil.updateLocalization( getTitle(), "Title", title, languageId, defaultLanguageId)); } else { setTitle(LocalizationUtil.removeLocalization(getTitle(), "Title", languageId)); } }
public ResourceBundle getResourceBundle(Locale locale) { ResourceBundle resourceBundle = _resourceBundles.get(LocaleUtil.toLanguageId(locale)); if (resourceBundle == null) { resourceBundle = _resourceBundles.get(locale.getLanguage()); if (resourceBundle == null) { resourceBundle = _resourceBundles.get(LocaleUtil.toLanguageId(LocaleUtil.getDefault())); } } return resourceBundle; }
protected String getTinyMCELanguage(String contentsLanguageId) { Locale contentsLocale = LocaleUtil.fromLanguageId(contentsLanguageId); contentsLanguageId = LocaleUtil.toLanguageId(contentsLocale); String tinyMCELanguage = _tinyMCELanguages.get(contentsLanguageId); if (Validator.isNull(tinyMCELanguage)) { tinyMCELanguage = _tinyMCELanguages.get("en_US"); } return tinyMCELanguage; }
@Override protected Document doGetDocument(Object obj) throws Exception { JournalArticle article = (JournalArticle) obj; Document document = getBaseModelDocument(PORTLET_ID, article); document.addUID(PORTLET_ID, article.getGroupId(), article.getArticleId()); Locale defaultLocale = LocaleUtil.getDefault(); String defaultLanguageId = LocaleUtil.toLanguageId(defaultLocale); String[] languageIds = getLanguageIds(defaultLanguageId, article.getContent()); for (String languageId : languageIds) { String content = extractContent(article, languageId); if (languageId.equals(defaultLanguageId)) { document.addText(Field.CONTENT, content); } document.addText(Field.CONTENT.concat(StringPool.UNDERLINE).concat(languageId), content); } document.addLocalizedText(Field.DESCRIPTION, article.getDescriptionMap()); document.addKeyword(Field.FOLDER_ID, article.getFolderId()); document.addLocalizedText(Field.TITLE, article.getTitleMap()); document.addKeyword(Field.TYPE, article.getType()); document.addKeyword(Field.VERSION, article.getVersion()); document.addKeyword("articleId", article.getArticleId()); document.addDate("displayDate", article.getDisplayDate()); document.addKeyword("layoutUuid", article.getLayoutUuid()); document.addKeyword("structureId", article.getStructureId()); document.addKeyword("templateId", article.getTemplateId()); JournalStructure structure = null; if (Validator.isNotNull(article.getStructureId())) { try { structure = JournalStructureLocalServiceUtil.getStructure( article.getGroupId(), article.getStructureId(), true); } catch (NoSuchStructureException nsse) { } } processStructure(structure, document, article.getContent()); return document; }
protected String getNameXML(String name) { StringBundler sb = new StringBundler(8); sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); sb.append("<root default-locale=\""); sb.append(LocaleUtil.getDefault()); sb.append("\"><Name language-id=\""); sb.append(LocaleUtil.getDefault()); sb.append("\">"); sb.append(name); sb.append("</Name></root>"); return sb.toString(); }
protected void setUpBlogsSettings() throws Exception { Settings settings = SettingsFactoryUtil.getGroupServiceSettings( _group.getGroupId(), BlogsConstants.SERVICE_NAME); ModifiableSettings modifiableSettings = settings.getModifiableSettings(); String subscriptionBodyPreferencesKey = LocalizationUtil.getLocalizedName( "emailEntryUpdatedBody", LocaleUtil.toLanguageId(LocaleUtil.getDefault())); modifiableSettings.setValue(subscriptionBodyPreferencesKey, "[$BLOGS_ENTRY_UPDATE_COMMENT$]"); modifiableSettings.store(); }
protected void prepareLanguagesForImport(DDMStructure structure) throws PortalException { Locale defaultLocale = LocaleUtil.fromLanguageId(structure.getDefaultLanguageId()); Locale[] availableLocales = LocaleUtil.fromLanguageIds(structure.getAvailableLanguageIds()); Locale defaultImportLocale = LocalizationUtil.getDefaultImportLocale( DDMStructure.class.getName(), structure.getPrimaryKey(), defaultLocale, availableLocales); structure.prepareLocalizedFieldsForImport(defaultImportLocale); }
protected Set<Locale> getAvailableLocales(String[] availableLanguageIds) { Set<Locale> availableLocales = new HashSet<>(); for (String availableLanguageId : availableLanguageIds) { Locale availableLocale = LocaleUtil.fromLanguageId(availableLanguageId); availableLocales.add(availableLocale); } if (availableLocales.isEmpty()) { availableLocales.add(LocaleUtil.getSiteDefault()); } return availableLocales; }