Skip to content

Commit c81cfa2

Browse files
committed
Implement ResourceBundle usage
1 parent 9d8603e commit c81cfa2

25 files changed

Lines changed: 304 additions & 95 deletions

File tree

aiservices/google/src/main/java/com/microsoft/semantickernel/aiservices/google/chatcompletion/GeminiChatCompletion.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.microsoft.semantickernel.exceptions.AIException;
2222
import com.microsoft.semantickernel.exceptions.SKCheckedException;
2323
import com.microsoft.semantickernel.exceptions.SKException;
24+
import com.microsoft.semantickernel.localization.SemanticKernelResources;
2425
import com.microsoft.semantickernel.orchestration.FunctionResult;
2526
import com.microsoft.semantickernel.orchestration.FunctionResultMetadata;
2627
import com.microsoft.semantickernel.orchestration.InvocationContext;
@@ -97,7 +98,8 @@ private Mono<List<ChatMessageContent<?>>> internalChatMessageContentsAsync(
9798
try {
9899
GenerativeModel model = getGenerativeModel(kernel, invocationContext);
99100
return MonoConverter.fromApiFuture(model.generateContentAsync(contents))
100-
.doOnError(e -> LOGGER.error("Error generating chat completion", e))
101+
.doOnError(e -> LOGGER.error(
102+
SemanticKernelResources.getString("error.generating.chat.completion"), e))
101103
.flatMap(result -> {
102104
// Get ChatMessageContent from the response
103105
GeminiChatMessageContent<?> response = getGeminiChatMessageContentFromResponse(
@@ -253,7 +255,8 @@ private GenerativeModel getGenerativeModel(@Nullable Kernel kernel,
253255

254256
if (settings.getResultsPerPrompt() < 1
255257
|| settings.getResultsPerPrompt() > MAX_RESULTS_PER_PROMPT) {
256-
throw SKCheckedException.build("Error building generative model.",
258+
throw SKCheckedException.build(
259+
SemanticKernelResources.getString("error.building.generative.model"),
257260
new AIException(AIException.ErrorCodes.INVALID_REQUEST,
258261
String.format(
259262
"Results per prompt must be in range between 1 and %d, inclusive.",

semantickernel-api/src/main/java/com/microsoft/semantickernel/KernelPluginCollection.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import com.microsoft.semantickernel.contextvariables.CaseInsensitiveMap;
55
import com.microsoft.semantickernel.exceptions.SKException;
6+
import com.microsoft.semantickernel.localization.SemanticKernelResources;
67
import com.microsoft.semantickernel.plugin.KernelPlugin;
78
import com.microsoft.semantickernel.semanticfunctions.KernelFunction;
89
import com.microsoft.semantickernel.semanticfunctions.KernelFunctionMetadata;
@@ -122,7 +123,8 @@ KernelPlugin getPlugin(String pluginName) {
122123
*/
123124
void add(KernelPlugin plugin) {
124125
if (plugins.containsKey(plugin.getName())) {
125-
LOGGER.warn("Plugin {} already exists, overwriting existing plugin", plugin.getName());
126+
LOGGER.warn(SemanticKernelResources.getString(
127+
"plugin.already.exists.overwriting.existing.plugin"), plugin.getName());
126128
}
127129

128130
plugins.put(plugin.getName(), plugin);

semantickernel-api/src/main/java/com/microsoft/semantickernel/contextvariables/ContextVariableTypeConverter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package com.microsoft.semantickernel.contextvariables;
33

44
import com.microsoft.semantickernel.exceptions.SKException;
5+
import com.microsoft.semantickernel.localization.SemanticKernelResources;
56
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
67
import java.util.ArrayList;
78
import java.util.Collections;
@@ -144,7 +145,8 @@ public <U> U toObject(ContextVariableTypes types, @Nullable Object t, Class<U> c
144145
return (U) converter.get().toObject((T) t);
145146
}
146147

147-
LOGGER.warn("No converter found for {} to {}", t.getClass(), clazz);
148+
LOGGER.warn(SemanticKernelResources.getString("no.converter.found.for.to"), t.getClass(),
149+
clazz);
148150
return null;
149151
}
150152

semantickernel-api/src/main/java/com/microsoft/semantickernel/exceptions/AIException.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
package com.microsoft.semantickernel.exceptions;
33

4+
import com.microsoft.semantickernel.localization.SemanticKernelResources;
45
import javax.annotation.Nonnull;
56
import javax.annotation.Nullable;
67

@@ -65,53 +66,57 @@ public enum ErrorCodes {
6566
/**
6667
* Unknown error.
6768
*/
68-
UNKNOWN_ERROR("Unknown error"),
69+
UNKNOWN_ERROR(SemanticKernelResources.getString("unknown.error")),
6970

7071
/**
7172
* No response.
7273
*/
73-
NO_RESPONSE("No response"),
74+
NO_RESPONSE(SemanticKernelResources.getString("no.response")),
7475
/**
7576
* Access denied.
7677
*/
77-
ACCESS_DENIED("Access is denied"),
78+
ACCESS_DENIED(SemanticKernelResources.getString("access.is.denied")),
7879

7980
/**
8081
* Invalid request.
8182
*/
82-
INVALID_REQUEST("The request was invalid"),
83+
INVALID_REQUEST(SemanticKernelResources.getString("the.request.was.invalid")),
8384
/**
8485
* Invalid response.
8586
*/
86-
INVALID_RESPONSE_CONTENT("The content of the response was invalid"),
87+
INVALID_RESPONSE_CONTENT(
88+
SemanticKernelResources.getString("the.content.of.the.response.was.invalid")),
8789

8890
/**
8991
* Throttling.
9092
*/
91-
THROTTLING("The request was throttled"),
93+
THROTTLING(SemanticKernelResources.getString("the.request.was.throttled")),
9294
/**
9395
* Request timeout.
9496
*/
95-
REQUEST_TIMEOUT("The request timed out"),
97+
REQUEST_TIMEOUT(SemanticKernelResources.getString("the.request.timed.out")),
9698

9799
/**
98100
* Service error.
99101
*/
100-
SERVICE_ERROR("There was an error in the service"),
102+
SERVICE_ERROR(SemanticKernelResources.getString("there.was.an.error.in.the.service")),
101103

102104
/**
103105
* Model not available.
104106
*/
105-
MODEL_NOT_AVAILABLE("The requested model is not available"),
107+
MODEL_NOT_AVAILABLE(
108+
SemanticKernelResources.getString("the.requested.model.is.not.available")),
106109

107110
/**
108111
* Invalid configuration.
109112
*/
110-
INVALID_CONFIGURATION("The supplied configuration was invalid"),
113+
INVALID_CONFIGURATION(
114+
SemanticKernelResources.getString("the.supplied.configuration.was.invalid")),
111115
/**
112116
* Function type not supported.
113117
*/
114-
FUNCTION_TYPE_NOT_SUPPORTED("The function is not supported");
118+
FUNCTION_TYPE_NOT_SUPPORTED(
119+
SemanticKernelResources.getString("the.function.is.not.supported"));
115120

116121
private final String message;
117122

semantickernel-api/src/main/java/com/microsoft/semantickernel/exceptions/ConfigurationException.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
package com.microsoft.semantickernel.exceptions;
33

4+
import com.microsoft.semantickernel.localization.SemanticKernelResources;
45
import javax.annotation.Nonnull;
56
import javax.annotation.Nullable;
67

@@ -65,27 +66,31 @@ public enum ErrorCodes {
6566
/**
6667
* Unknown error
6768
*/
68-
UNKNOWN_ERROR("Unknown error"),
69+
UNKNOWN_ERROR(SemanticKernelResources.getString("unknown.error")),
6970

7071
/**
7172
* Could not find configuration file
7273
*/
73-
CONFIGURATION_NOT_FOUND("Could not find configuration file"),
74+
CONFIGURATION_NOT_FOUND(
75+
SemanticKernelResources.getString("could.not.find.configuration.file")),
7476

7577
/**
7678
* Could not parse or load configuration file
7779
*/
78-
COULD_NOT_READ_CONFIGURATION("Could not parse or load configuration file"),
80+
COULD_NOT_READ_CONFIGURATION(
81+
SemanticKernelResources.getString("could.not.parse.or.load.configuration.file")),
7982

8083
/**
8184
* Could not find any valid configuration settings
8285
*/
83-
NO_VALID_CONFIGURATIONS_FOUND("Could not find any valid configuration settings"),
86+
NO_VALID_CONFIGURATIONS_FOUND(
87+
SemanticKernelResources.getString("could.not.find.any.valid.configuration.settings")),
8488

8589
/**
8690
* Could not find value for configuration key
8791
*/
88-
VALUE_NOT_FOUND("Could not find value for configuration key");
92+
VALUE_NOT_FOUND(
93+
SemanticKernelResources.getString("could.not.find.value.for.configuration.key"));
8994

9095
private final String message;
9196

semantickernel-api/src/main/java/com/microsoft/semantickernel/implementation/EmbeddedResourceLoader.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
package com.microsoft.semantickernel.implementation;
33

44
import com.microsoft.semantickernel.exceptions.SKException;
5+
import com.microsoft.semantickernel.localization.SemanticKernelResources;
56
import java.io.BufferedReader;
67
import java.io.File;
78
import java.io.FileNotFoundException;
89
import java.io.IOException;
910
import java.io.InputStream;
1011
import java.io.InputStreamReader;
1112
import java.nio.file.Files;
13+
import java.text.MessageFormat;
1214
import java.util.Arrays;
1315
import java.util.List;
1416
import java.util.Objects;
@@ -126,7 +128,10 @@ private static String readInputStream(String fileName, InputStream inputStream)
126128
return bf.lines().collect(Collectors.joining("\n"));
127129
} catch (IOException e) {
128130
// IGNORE
129-
LOGGER.trace("Failed to load file: " + fileName, e);
131+
LOGGER.trace(
132+
MessageFormat.format(SemanticKernelResources.getString("failed.to.load.file.0"),
133+
fileName),
134+
e);
130135
}
131136
return null;
132137
}

semantickernel-api/src/main/java/com/microsoft/semantickernel/implementation/ServiceLoadUtil.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.List;
99
import java.util.ServiceLoader;
1010
import java.util.function.Supplier;
11+
import com.microsoft.semantickernel.localization.SemanticKernelResources;
1112
import org.slf4j.Logger;
1213
import org.slf4j.LoggerFactory;
1314

@@ -54,11 +55,13 @@ public static <T> Supplier<T> findServiceLoader(Class<T> clazz, String alternati
5455
| IllegalAccessException
5556
| NoSuchMethodException
5657
| RuntimeException e) {
57-
LOGGER.error("Unable to load service " + clazz.getName() + " ", e);
58+
LOGGER.error(String.format(
59+
SemanticKernelResources.getString("unable.to.load.service.s"), clazz.getName()),
60+
e);
5861
}
5962

6063
if (impl == null) {
61-
throw new RuntimeException("Service not found: " + clazz.getName());
64+
throw new RuntimeException(String.format("Service not found: %s", clazz.getName()));
6265
}
6366
}
6467

semantickernel-api/src/main/java/com/microsoft/semantickernel/implementation/chatcompletion/ChatXMLPromptParser.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.fasterxml.jackson.databind.JsonNode;
66
import com.fasterxml.jackson.databind.ObjectMapper;
77
import com.microsoft.semantickernel.exceptions.SKException;
8+
import com.microsoft.semantickernel.localization.SemanticKernelResources;
89
import com.microsoft.semantickernel.orchestration.ToolCallBehavior;
910
import java.io.ByteArrayInputStream;
1011
import java.io.IOException;
@@ -203,7 +204,7 @@ private static <T> ChatPromptParseVisitor<T> getFunctionDefinitions(String promp
203204
}
204205
}
205206
} catch (IOException | XMLStreamException | IllegalArgumentException e) {
206-
LOGGER.error("Error parsing prompt", e);
207+
LOGGER.error(SemanticKernelResources.getString("error.parsing.prompt"), e);
207208
}
208209
return chatPromptParseVisitor;
209210
}

semantickernel-api/src/main/java/com/microsoft/semantickernel/implementation/templateengine/tokenizer/blocks/CodeBlock.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
import com.microsoft.semantickernel.contextvariables.ContextVariableType;
77
import com.microsoft.semantickernel.contextvariables.ContextVariableTypes;
88
import com.microsoft.semantickernel.exceptions.SKException;
9+
import com.microsoft.semantickernel.localization.SemanticKernelResources;
910
import com.microsoft.semantickernel.orchestration.FunctionResult;
1011
import com.microsoft.semantickernel.orchestration.InvocationContext;
1112
import com.microsoft.semantickernel.semanticfunctions.KernelFunctionArguments;
1213
import com.microsoft.semantickernel.semanticfunctions.KernelFunctionMetadata;
1314
import com.microsoft.semantickernel.templateengine.semantickernel.TemplateException;
1415
import com.microsoft.semantickernel.templateengine.semantickernel.TemplateException.ErrorCodes;
16+
import java.text.MessageFormat;
1517
import java.util.Collections;
1618
import java.util.List;
1719
import java.util.Optional;
@@ -44,12 +46,14 @@ public CodeBlock(List<Block> tokens, String content) {
4446
public boolean isValid() {
4547
Optional<Block> invalid = tokens.stream().filter(token -> !token.isValid()).findFirst();
4648
if (invalid.isPresent()) {
47-
LOGGER.error("Invalid block" + invalid.get().getContent());
49+
LOGGER.error(MessageFormat.format(SemanticKernelResources.getString("invalid.block.0"),
50+
invalid.get().getContent()));
4851
return false;
4952
}
5053

5154
if (!this.tokens.isEmpty() && this.tokens.get(0).getType() == BlockTypes.NAMED_ARG) {
52-
LOGGER.error("Unexpected named argument found. Expected function name first.");
55+
LOGGER.error(SemanticKernelResources.getString(
56+
"unexpected.named.argument.found.expected.function.name.first"));
5357
return false;
5458
}
5559

@@ -62,23 +66,27 @@ public boolean isValid() {
6266

6367
private boolean isValidFunctionCall() {
6468
if (this.tokens.get(0).getType() != BlockTypes.FUNCTION_ID) {
65-
LOGGER.error("Unexpected second token found: " + tokens.get(1).getContent());
69+
LOGGER.error(MessageFormat.format(
70+
SemanticKernelResources.getString("unexpected.second.token.found.0"),
71+
tokens.get(1).getContent()));
6672
return false;
6773
}
6874

6975
if (this.tokens.get(1).getType() != BlockTypes.VALUE &&
7076
this.tokens.get(1).getType() != BlockTypes.VARIABLE &&
7177
this.tokens.get(1).getType() != BlockTypes.NAMED_ARG) {
7278
LOGGER.error(
73-
"The first arg of a function must be a quoted string, variable or named argument");
79+
SemanticKernelResources.getString(
80+
"the.first.arg.of.a.function.must.be.a.quoted.string.variable.or.named.argument"));
7481
return false;
7582
}
7683

7784
for (int i = 2; i < this.tokens.size(); i++) {
7885
if (this.tokens.get(i).getType() != BlockTypes.NAMED_ARG) {
7986
LOGGER.error(
80-
"Functions only support named arguments after the first argument. Argument " + i
81-
+ " is not named.");
87+
SemanticKernelResources.getString(
88+
"functions.only.support.named.arguments.after.the.first.argument"),
89+
i);
8290
return false;
8391
}
8492
}

semantickernel-api/src/main/java/com/microsoft/semantickernel/implementation/templateengine/tokenizer/blocks/NamedArgBlock.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.microsoft.semantickernel.contextvariables.ContextVariableTypes;
77
import com.microsoft.semantickernel.exceptions.SKException;
88
import com.microsoft.semantickernel.implementation.Verify;
9+
import com.microsoft.semantickernel.localization.SemanticKernelResources;
910
import com.microsoft.semantickernel.semanticfunctions.KernelFunctionArguments;
1011
import javax.annotation.Nullable;
1112
import org.slf4j.Logger;
@@ -109,18 +110,20 @@ private static String splitAndGetPart(String text, int x) {
109110
@Override
110111
public boolean isValid() {
111112
if (Verify.isNullOrEmpty(this.name)) {
112-
LOGGER.error("A named argument must have a name");
113+
LOGGER.error(SemanticKernelResources.getString("a.named.argument.must.have.a.name"));
113114
return false;
114115
}
115116

116117
if (this.valBlock != null && !this.valBlock.isValid()) {
117-
LOGGER.error("There was an issue with the named argument value for '" + name);
118+
LOGGER.error(SemanticKernelResources.getString(
119+
"there.was.an.issue.with.the.named.argument.value.for"), name);
118120
return false;
119121
} else if (this.varBlock != null && !this.varBlock.isValid()) {
120-
LOGGER.error("There was an issue with the named argument value for '" + name);
122+
LOGGER.error(SemanticKernelResources.getString(
123+
"there.was.an.issue.with.the.named.argument.value.for"), name);
121124
return false;
122125
} else if (this.valBlock == null && this.varBlock == null) {
123-
LOGGER.error("A named argument must have a value");
126+
LOGGER.error(SemanticKernelResources.getString("a.named.argument.must.have.a.value"));
124127
return false;
125128
}
126129

0 commit comments

Comments
 (0)