Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Extract messages to the properties bundle #44

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions server/src/main/java/com/microsoft/java/bs/core/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.microsoft.java.bs.core.i18n.MessageUtils;
import com.microsoft.java.bs.core.internal.managers.BuildTargetManager;
import com.microsoft.java.bs.core.internal.server.GradleBuildServer;
import com.microsoft.java.bs.core.internal.services.BuildTargetService;
Expand All @@ -18,7 +19,7 @@
* Main entry point for the BSP server.
*/
public class Launcher {

/**
* The property name for the build server storage location.
*/
Expand Down Expand Up @@ -54,7 +55,7 @@ private static org.eclipse.lsp4j.jsonrpc.Launcher<BuildClient> createLauncher()

private static void checkRequiredProperties() {
if (System.getProperty(PROP_BUILD_SERVER_STORAGE) == null) {
throw new IllegalStateException("The property 'buildServerStorage' is not set");
throw new IllegalStateException(MessageUtils.get("error.serverStorageMissing"));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.microsoft.java.bs.core.i18n;

import java.util.Locale;
import java.util.ResourceBundle;

/**
* Utils to get the messages from properties file.
*/
public class MessageUtils {
private static ResourceBundle messages;

static {
// By default, use the system default locale
messages = ResourceBundle.getBundle("messages");
}

public static void setLocale(Locale locale) {
messages = ResourceBundle.getBundle("messages", locale);
}

public static String get(String key) {
return messages.getString(key);
}

private MessageUtils() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.gradle.tooling.ModelBuilder;
import org.gradle.tooling.ProjectConnection;

import com.microsoft.java.bs.core.i18n.MessageUtils;
import com.microsoft.java.bs.gradle.model.GradleSourceSets;

/**
Expand Down Expand Up @@ -83,7 +84,7 @@ private File copyPluginJarFile() {
Files.write(pluginJarFile.toPath(), pluginJarBytes);
}
} catch (IOException | NoSuchAlgorithmException e) {
throw new IllegalStateException("Failed to get plugin jar.", e);
throw new IllegalStateException(MessageUtils.get("error.pluginMissing"), e);
}
return pluginJarFile;
}
Expand Down Expand Up @@ -116,7 +117,7 @@ classpath files('%s')
}
return initScriptFile;
} catch (IOException | NoSuchAlgorithmException e) {
throw new IllegalStateException("Failed to get init.script", e);
throw new IllegalStateException(MessageUtils.get("error.initScriptMissing"), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.microsoft.java.bs.core.i18n.MessageUtils;
import com.microsoft.java.bs.core.internal.managers.BuildTargetManager;
import com.microsoft.java.bs.core.internal.model.GradleBuildTarget;
import com.microsoft.java.bs.gradle.model.GradleSourceSet;
Expand Down Expand Up @@ -61,8 +62,7 @@ public SourcesResult getBuildTargetSources(SourcesParams params) {
for (BuildTargetIdentifier btId : params.getTargets()) {
GradleBuildTarget target = buildTargetManager.getGradleBuildTarget(btId);
if (target == null) {
logger.warn("Skip sources collection for the build target: {}"
+ "because it cannot be found in the cache.", btId.getUri());
logger.warn(MessageUtils.get("warning.skipBuildTargetSources"), btId.getUri());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's log message, not UI message. I don't think nls is needed for them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It depends. For example, in the future, we can have a log appender that append the log to the client via BSP notification like OnBuildLogMessage, where users can see it.

On the other hand, if we are sure that no need to translate those log messages. We can simply ignore them in other language bundle files. The JVM will be fallback to the default if it does not exist.

continue;
}

Expand Down Expand Up @@ -90,8 +90,7 @@ public ResourcesResult getBuildTargetResources(ResourcesParams params) {
for (BuildTargetIdentifier btId : params.getTargets()) {
GradleBuildTarget target = buildTargetManager.getGradleBuildTarget(btId);
if (target == null) {
logger.warn("Skip resources collection for the build target: {}"
+ "because it cannot be found in the cache.", btId.getUri());
logger.warn(MessageUtils.get("warning.skipBuildTargetResources"), btId.getUri());
continue;
}

Expand All @@ -114,8 +113,7 @@ public OutputPathsResult getBuildTargetOutputPaths(OutputPathsParams params) {
for (BuildTargetIdentifier btId : params.getTargets()) {
GradleBuildTarget target = buildTargetManager.getGradleBuildTarget(btId);
if (target == null) {
logger.warn("Skip output collection for the build target: {}"
+ "because it cannot be found in the cache.", btId.getUri());
logger.warn(MessageUtils.get("warning.skipBuildTargetOutputPaths"), btId.getUri());
continue;
}

Expand Down
7 changes: 7 additions & 0 deletions server/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error.serverStorageMissing=The property 'buildServerStorage' is not set.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good start for localization.

error.pluginMissing=Failed to get plugin jar.
error.initScriptMissing=Failed to get init.script.

warning.skipBuildTargetSources=Skip sources collection for the build target: {} because it cannot be found in the cache.
warning.skipBuildTargetResources=Skip resources collection for the build target: {} because it cannot be found in the cache.
warning.skipBuildTargetOutputPaths=Skip output paths collection for the build target: {} because it cannot be found in the cache.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.microsoft.java.bs.core.i18n;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.util.Locale;

import org.junit.jupiter.api.Test;

class MessageUtilsTest {
@Test
void testGet() {
assertNotNull(MessageUtils.get("error.serverStorageMissing"));
}

@Test
void testSetLocale() {
MessageUtils.setLocale(new Locale("zh", "CN"));
assertNotNull(MessageUtils.get("error.serverStorageMissing"));
}

}