-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add variables to Messages This change adds MessageResource and LocalizedMessageResolver APIs to ease resolving a message given a instance of io.micronaut.views.fields.messages.Message * improves compareTo
- Loading branch information
Showing
8 changed files
with
299 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...s-fieldset/src/main/java/io/micronaut/views/fields/messages/LocalizedMessageResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2017-2024 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.views.fields.messages; | ||
|
||
import io.micronaut.context.LocalizedMessageSource; | ||
import io.micronaut.core.annotation.NonNull; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Utility class to resolve message of a {@link Message} with a given {@link LocalizedMessageSource}. | ||
* @author Sergio del Amo | ||
* @since 5.6.0 | ||
*/ | ||
public class LocalizedMessageResolver { | ||
|
||
private final LocalizedMessageSource messageSource; | ||
|
||
public LocalizedMessageResolver(LocalizedMessageSource messageSource) { | ||
this.messageSource = messageSource; | ||
} | ||
|
||
/** | ||
* | ||
* @param message The message | ||
* @return the resolved message or the defaultMessage if the message is not found. | ||
*/ | ||
@NonNull | ||
public String getMessageOrDefault(@NonNull Message message) { | ||
return messageSource.getMessageOrDefault(message.code(), message.defaultMessage(), message.variables()); | ||
} | ||
|
||
/** | ||
* | ||
* @param message The message | ||
* @return the resolved message or an empty optional if not found. | ||
*/ | ||
@NonNull | ||
public Optional<String> getMessage(@NonNull Message message) { | ||
return messageSource.getMessage(message.code(), message.variables()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
views-fieldset/src/main/java/io/micronaut/views/fields/messages/MessageResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2017-2024 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.views.fields.messages; | ||
|
||
import io.micronaut.context.MessageSource; | ||
import io.micronaut.core.annotation.NonNull; | ||
|
||
import java.util.Locale; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Utility class to resolve message of a {@link Message} with a given {@link MessageSource}. | ||
* @author Sergio del Amo | ||
* @since 5.6.0 | ||
*/ | ||
public class MessageResolver { | ||
|
||
private final MessageSource messageSource; | ||
|
||
public MessageResolver(MessageSource messageSource) { | ||
this.messageSource = messageSource; | ||
} | ||
|
||
/** | ||
* | ||
* @param message The message | ||
* @param locale the Locale | ||
* @return the resolved message or the defaultMessage if the message is not found. | ||
*/ | ||
@NonNull | ||
public String getMessageOrDefault(@NonNull Message message, @NonNull Locale locale) { | ||
return messageSource.getMessage(message.code(), message.defaultMessage(), locale, message.variables()); | ||
} | ||
|
||
/** | ||
* | ||
* @param message The message | ||
* @param locale the Locale | ||
* @return the resolved message or an empty optional if not found. | ||
*/ | ||
@NonNull | ||
public Optional<String> getMessage(@NonNull Message message, @NonNull Locale locale) { | ||
return messageSource.getMessage(message.code(), locale, message.variables()); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
views-fieldset/src/main/java/io/micronaut/views/fields/messages/MessageResolverFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2017-2024 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.views.fields.messages; | ||
|
||
import io.micronaut.context.LocalizedMessageSource; | ||
import io.micronaut.context.MessageSource; | ||
import io.micronaut.context.annotation.Factory; | ||
import io.micronaut.context.annotation.Requires; | ||
import io.micronaut.core.annotation.Internal; | ||
import jakarta.inject.Singleton; | ||
|
||
@Factory | ||
@Internal | ||
final class MessageResolverFactory { | ||
|
||
@Singleton | ||
MessageResolver createMessageResolver(MessageSource messageSource) { | ||
return new MessageResolver(messageSource); | ||
} | ||
|
||
@Requires(beans = LocalizedMessageSource.class) | ||
@Singleton | ||
LocalizedMessageResolver createMessageResolver(LocalizedMessageSource messageSource) { | ||
return new LocalizedMessageResolver(messageSource); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
default.null.message=Property [{0}] of class [{1}] cannot be null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
default.null.message=La propiedad [{0}] de la clase [{1}] no puede ser nulo |