|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2015 Oracle and/or its affiliates. All rights reserved. |
| 3 | + * Copyright (c) 2018, 2022 Eclipse Krazo committers and contributors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + * SPDX-License-Identifier: Apache-2.0 |
| 18 | + */ |
| 19 | +package org.eclipse.krazo.test.ext.thymeleaf.i18n; |
| 20 | + |
| 21 | +import org.thymeleaf.context.ITemplateContext; |
| 22 | +import org.thymeleaf.messageresolver.AbstractMessageResolver; |
| 23 | + |
| 24 | +import java.text.MessageFormat; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.Locale; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +/** |
| 30 | + * Second custom message resolver for testing multiple resolvers. |
| 31 | + * This resolver has lower priority (order=50) than CustomMessageResolver (order=0). |
| 32 | + * |
| 33 | + * @author Satoshi Seto |
| 34 | + */ |
| 35 | +public class CustomMessageResolver2 extends AbstractMessageResolver { |
| 36 | + |
| 37 | + private static final Map<String, String> MESSAGES_EN = new HashMap<>(); |
| 38 | + private static final Map<String, String> MESSAGES_JA = new HashMap<>(); |
| 39 | + private static final Object[] EMPTY_MESSAGE_PARAMETERS = new Object[0]; |
| 40 | + |
| 41 | + static { |
| 42 | + // English messages (only resolved if CustomMessageResolver doesn't handle them) |
| 43 | + MESSAGES_EN.put("resolver2.message", "This is from CustomMessageResolver2"); |
| 44 | + MESSAGES_EN.put("resolver2.greeting", "Hello from Resolver2, {0}!"); |
| 45 | + MESSAGES_EN.put("custom.greeting", "This should NOT be used (Resolver2 has lower priority)"); |
| 46 | + |
| 47 | + // Japanese messages |
| 48 | + MESSAGES_JA.put("resolver2.message", "これはCustomMessageResolver2からです"); |
| 49 | + MESSAGES_JA.put("resolver2.greeting", "Resolver2からこんにちは、{0}さん!"); |
| 50 | + MESSAGES_JA.put("custom.greeting", "これは使われないはず(Resolver2は優先度が低い)"); |
| 51 | + } |
| 52 | + |
| 53 | + public CustomMessageResolver2() { |
| 54 | + // Lower priority than CustomMessageResolver (which has order=0) |
| 55 | + setOrder(50); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public String resolveMessage( |
| 60 | + ITemplateContext context, |
| 61 | + Class<?> origin, |
| 62 | + String key, |
| 63 | + Object[] messageParameters) { |
| 64 | + |
| 65 | + Locale locale = context.getLocale(); |
| 66 | + Map<String, String> messages = locale != null && locale.getLanguage().equals("ja") |
| 67 | + ? MESSAGES_JA |
| 68 | + : MESSAGES_EN; |
| 69 | + |
| 70 | + String message = messages.get(key); |
| 71 | + if (message == null) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + return formatMessage(locale, message, messageParameters); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public String createAbsentMessageRepresentation( |
| 80 | + ITemplateContext context, |
| 81 | + Class<?> origin, |
| 82 | + String key, |
| 83 | + Object[] messageParameters) { |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + private String formatMessage(Locale locale, String message, Object[] params) { |
| 88 | + if (message == null) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + final MessageFormat messageFormat = new MessageFormat(message, locale); |
| 92 | + return messageFormat.format((params != null ? params : EMPTY_MESSAGE_PARAMETERS)); |
| 93 | + } |
| 94 | +} |
0 commit comments