IDEA-341743 Refactor LocalizationUtil to a singleton object and make some function as Path extension function

GitOrigin-RevId: 45cb00ee18919de78ce7b8417437377af7b81d90
This commit is contained in:
Vera Petrenkova
2024-02-13 10:04:02 +01:00
committed by intellij-monorepo-bot
parent bc31610df0
commit 4d41a0c5b5
4 changed files with 29 additions and 29 deletions

View File

@@ -29,7 +29,7 @@ import java.util.Set;
*/
public abstract class InspectionToolWrapper<T extends InspectionProfileEntry, E extends InspectionEP> {
public static final InspectionToolWrapper[] EMPTY_ARRAY = new InspectionToolWrapper[0];
public static final String INSPECTION_DESCRIPTIONS_FOLDER = "inspectionDescriptions";
private static final String INSPECTION_DESCRIPTIONS_FOLDER = "inspectionDescriptions";
private static final Logger LOG = Logger.getInstance(InspectionToolWrapper.class);
@@ -187,10 +187,10 @@ public abstract class InspectionToolWrapper<T extends InspectionProfileEntry, E
Application app = ApplicationManager.getApplication();
Path path = Path.of(INSPECTION_DESCRIPTIONS_FOLDER).resolve(getDescriptionFileName());
if (myEP == null || app.isUnitTestMode() || app.isHeadlessEnvironment()) {
return LocalizationUtil.Companion.getResourceAsStream(getDescriptionContextClass().getClassLoader(), path);
return LocalizationUtil.INSTANCE.getResourceAsStream(getDescriptionContextClass().getClassLoader(), path);
}
return LocalizationUtil.Companion.getResourceAsStream(myEP.getPluginDescriptor().getPluginClassLoader(), path);
return LocalizationUtil.INSTANCE.getResourceAsStream(myEP.getPluginDescriptor().getPluginClassLoader(), path);
}
private @NotNull String getDescriptionFileName() {

View File

@@ -79,7 +79,7 @@ public class DynamicBundle extends AbstractBundle {
) {
Path bundlePath = FileSystems.getDefault().getPath(defaultPath.replaceAll("\\.", "/"));
ClassLoader pluginClassLoader = languagePluginClassLoader(bundleClassLoader, locale);
List<Path> paths = LocalizationUtil.Companion.getLocalizedPaths(bundlePath, locale);
List<Path> paths = LocalizationUtil.INSTANCE.getLocalizedPaths(bundlePath, locale);
Map<BundleOrder, ResourceBundle> bundleOrderMap = new HashMap<>();
if (pluginClassLoader != null) {
resolveBundleOrder(pluginClassLoader, true, bundlePath, paths, bundleOrderMap, bundleResolver, locale);
@@ -97,7 +97,7 @@ public class DynamicBundle extends AbstractBundle {
@ApiStatus.Internal
private static List<ResourceBundle> getBundlesFromLocalizationFolder(Path pathToBundle, ClassLoader loader, Locale locale) {
List<Path> paths = LocalizationUtil.Companion.getFolderLocalizedPaths(pathToBundle, locale);
List<Path> paths = LocalizationUtil.INSTANCE.getFolderLocalizedPaths(pathToBundle, locale);
List<ResourceBundle> resourceBundles = new ArrayList<>();
for (Path path : paths) {
try {

View File

@@ -13,24 +13,23 @@ import kotlin.io.path.nameWithoutExtension
import kotlin.io.path.pathString
@ApiStatus.Internal
class LocalizationUtil {
companion object {
object LocalizationUtil {
private fun getPluginClassLoader(): ClassLoader? = DynamicBundle.findLanguageBundle()?.pluginDescriptor?.pluginClassLoader
private fun convertPathToLocalizationFolderUsage(path: Path, locale: Locale, withRegion: Boolean): Path {
private fun Path.convertToLocalizationFolderUsage(locale: Locale, withRegion: Boolean): Path {
val localizationFolderName = "localization"
var result = Path(localizationFolderName).resolve(locale.language)
if (withRegion && locale.country.isNotEmpty()) {
result = result.resolve(locale.country)
}
result = result.resolve(path)
result = result.resolve(this)
return result
}
private fun convertPathToLocaleSuffixUsage(path: Path, locale: Locale?, withRegion: Boolean): Path {
if (locale == null) return path
val fileName = StringBuilder(path.nameWithoutExtension)
val extension = path.extension
val foldersPath = path.parent ?: Path("")
private fun Path.convertPathToLocaleSuffixUsage(locale: Locale?, withRegion: Boolean): Path {
if (locale == null) return this
val fileName = StringBuilder(this.nameWithoutExtension)
val extension = this.extension
val foldersPath = this.parent ?: Path("")
val language = locale.language
if (!language.isEmpty()) {
fileName.append('_').append(language)
@@ -61,20 +60,22 @@ class LocalizationUtil {
@JvmOverloads
fun getLocalizedPaths(path: Path, specialLocale: Locale? = null): List<Path> {
val locale = specialLocale ?: getLocale()
val result = mutableListOf<Path>()
//localizations/zh/CN/inspectionDescriptions/name.html
result.add(convertPathToLocalizationFolderUsage(path, locale, true))
val result = listOf(
//localizations/zh/CN/inspectionDescriptions/name.html
path.convertToLocalizationFolderUsage(locale, true),
//inspectionDescriptions/name_zh_CN.html
result.add(convertPathToLocaleSuffixUsage(path, locale, true))
//inspectionDescriptions/name_zh_CN.html
path.convertPathToLocaleSuffixUsage(locale, true),
//localizations/zh/inspectionDescriptions/name.html
result.add(convertPathToLocalizationFolderUsage(path, locale, false))
//localizations/zh/inspectionDescriptions/name.html
path.convertToLocalizationFolderUsage(locale, false),
//inspectionDescriptions/name_zh.html
result.add(convertPathToLocaleSuffixUsage(path, locale, false))
//inspectionDescriptions/name.html
result.add(path)
//inspectionDescriptions/name_zh.html
path.convertPathToLocaleSuffixUsage(locale, false),
//inspectionDescriptions/name.html
path
)
return result
}
@@ -83,11 +84,10 @@ class LocalizationUtil {
val locale = specialLocale ?: getLocale()
val result = mutableListOf<Path>()
//localizations/zh/CN/inspectionDescriptions/name.html
result.add(convertPathToLocalizationFolderUsage(path, locale, true))
result.add(path.convertToLocalizationFolderUsage(locale, true))
//localizations/zh/inspectionDescriptions/name.html
result.add(convertPathToLocalizationFolderUsage(path, locale, false))
result.add(path.convertToLocalizationFolderUsage(locale, false))
return result
}
}
}

View File

@@ -38,7 +38,7 @@ final class ResourceTextDescriptor implements TextDescriptor {
@Override
public @NotNull String getText() throws IOException {
InputStream inputStream = LocalizationUtil.Companion.getResourceAsStream(myLoader, Path.of(myResourcePath));
InputStream inputStream = LocalizationUtil.INSTANCE.getResourceAsStream(myLoader, Path.of(myResourcePath));
if (inputStream != null) {
try (inputStream) {
return ResourceUtil.loadText(inputStream); //NON-NLS