com.liferay.portal.language.override.internal.PLOOverrideResourceBundleManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com.liferay.portal.language.override.service
Show all versions of com.liferay.portal.language.override.service
Liferay Portal Language Override Service
/**
* SPDX-FileCopyrightText: (c) 2023 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/
package com.liferay.portal.language.override.internal;
import com.liferay.petra.concurrent.DCLSingleton;
import com.liferay.petra.string.StringBundler;
import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.language.Language;
import com.liferay.portal.kernel.security.auth.CompanyThreadLocal;
import com.liferay.portal.kernel.service.CompanyLocalService;
import com.liferay.portal.language.LanguageResources;
import com.liferay.portal.language.override.internal.provider.PLOOriginalTranslationThreadLocal;
import com.liferay.portal.language.override.model.PLOEntry;
import com.liferay.portal.language.override.service.PLOEntryLocalService;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
/**
* @author Regisson Aguiar
*/
@Component(service = PLOOverrideResourceBundleManager.class)
public class PLOOverrideResourceBundleManager {
public ResourceBundle getOverrideResourceBundle(Locale locale) {
Map
overrideResourceBundles =
_overrideResourceBundlesDCLSingleton.getSingleton(_supplier);
if (overrideResourceBundles.isEmpty() ||
PLOOriginalTranslationThreadLocal.isUseOriginalTranslation()) {
return null;
}
return overrideResourceBundles.get(
_encodeKey(
CompanyThreadLocal.getCompanyId(),
_language.getLanguageId(locale)));
}
protected static void clearCache() {
_overrideResourceBundlesDCLSingleton.destroy(null);
LanguageResources.clearResourceBundles();
}
private void _add(
Map overrideResourceBundles,
PLOEntry ploEntry) {
overrideResourceBundles.compute(
_encodeKey(ploEntry.getCompanyId(), ploEntry.getLanguageId()),
(key, value) -> {
if (value == null) {
value = new OverrideResourceBundle(new HashMap<>());
}
value.put(ploEntry.getKey(), ploEntry.getValue());
return value;
});
}
private Map
_createOverrideResourceBundles() {
Map overrideResourceBundles =
new ConcurrentHashMap<>();
_companyLocalService.forEachCompanyId(
companyId -> {
for (PLOEntry ploEntry :
_ploEntryLocalService.getPLOEntries(companyId)) {
_add(overrideResourceBundles, ploEntry);
}
});
return overrideResourceBundles;
}
private String _encodeKey(long companyId, String languageId) {
return StringBundler.concat(companyId, StringPool.POUND, languageId);
}
private static final DCLSingleton
© 2015 - 2025 Weber Informatics LLC | Privacy Policy