-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
063309d
commit efc4292
Showing
14 changed files
with
276 additions
and
6 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
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
122 changes: 122 additions & 0 deletions
122
...-i18n-spring-boot/src/main/java/io/microsphere/i18n/spring/boot/actuate/I18nEndpoint.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,122 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 | ||
* | ||
* http://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.microsphere.i18n.spring.boot.actuate; | ||
|
||
import io.microsphere.i18n.AbstractResourceServiceMessageSource; | ||
import io.microsphere.i18n.CompositeServiceMessageSource; | ||
import io.microsphere.i18n.ServiceMessageSource; | ||
import io.microsphere.i18n.spring.DelegatingServiceMessageSource; | ||
import io.microsphere.i18n.spring.beans.factory.ServiceMessageSourceFactoryBean; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint; | ||
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import static io.microsphere.i18n.spring.constants.I18nConstants.SERVICE_MESSAGE_SOURCE_BEAN_NAME; | ||
import static java.util.Collections.emptyList; | ||
import static org.springframework.util.CollectionUtils.isEmpty; | ||
|
||
/** | ||
* I18n Spring Boot Actuator Endpoint | ||
* <prev> | ||
* { | ||
* "common" : { | ||
* "zh_CN" : { | ||
* "error.a" : "a" | ||
* } | ||
* }, | ||
* "test" : { | ||
* "test.i18n_messages_zh_CN.properties" : { | ||
* | ||
* }, | ||
* "META-INF/i18n/test/i18n_messages_en.properties":{ | ||
* "test.a" : "test-a" | ||
* "test.hello" : "Hello,{}" | ||
* } | ||
* ... | ||
* } | ||
* } | ||
* </prev> | ||
* | ||
* @author <a href="mailto:[email protected]">Mercy</a> | ||
* @since 1.0.0 | ||
*/ | ||
@Endpoint(id = "i18n") | ||
public class I18nEndpoint { | ||
|
||
private List<ServiceMessageSource> serviceMessageSources; | ||
|
||
@Autowired | ||
@Qualifier(SERVICE_MESSAGE_SOURCE_BEAN_NAME) | ||
public void initServiceMessageSources(ServiceMessageSource serviceMessageSource) { | ||
List<ServiceMessageSource> serviceMessageSources = emptyList(); | ||
if (serviceMessageSource instanceof DelegatingServiceMessageSource) { | ||
DelegatingServiceMessageSource delegatingServiceMessageSource = (DelegatingServiceMessageSource) serviceMessageSource; | ||
serviceMessageSources = delegatingServiceMessageSource.getDelegate().getServiceMessageSources(); | ||
} | ||
this.serviceMessageSources = serviceMessageSources; | ||
} | ||
|
||
@ReadOperation | ||
public Map<String, Object> invoke() { | ||
List<ServiceMessageSource> serviceMessageSources = this.serviceMessageSources; | ||
int size = serviceMessageSources.size(); | ||
Map<String, Object> result = new HashMap<>(size); | ||
for (int i = 0; i < size; i++) { | ||
ServiceMessageSource serviceMessageSource = serviceMessageSources.get(i); | ||
if (serviceMessageSource instanceof CompositeServiceMessageSource) { | ||
CompositeServiceMessageSource compositeServiceMessageSource = (CompositeServiceMessageSource) serviceMessageSource; | ||
for (ServiceMessageSource sms : compositeServiceMessageSource.getServiceMessageSources()) { | ||
if (sms instanceof AbstractResourceServiceMessageSource) { | ||
AbstractResourceServiceMessageSource resourceServiceMessageSource = (AbstractResourceServiceMessageSource) sms; | ||
String source = serviceMessageSource.getSource(); | ||
List<Locale> supportedLocales = resourceServiceMessageSource.getSupportedLocales(); | ||
Map<String, Map<String, String>> localizedMessages = new HashMap<>(supportedLocales.size()); | ||
|
||
|
||
for (Locale supportedLocale : supportedLocales) { | ||
Map<String, String> messages = resourceServiceMessageSource.getMessages(supportedLocale); | ||
if (!isEmpty(messages)) { | ||
localizedMessages.put(supportedLocale.toString(), messages); | ||
} | ||
} | ||
|
||
result.put(source, localizedMessages); | ||
} | ||
} | ||
} | ||
|
||
} | ||
return result; | ||
} | ||
|
||
private AbstractResourceServiceMessageSource getResourceServiceMessageSource(ServiceMessageSource serviceMessageSource) { | ||
if (serviceMessageSource instanceof ServiceMessageSourceFactoryBean) { | ||
ServiceMessageSourceFactoryBean smffb = (ServiceMessageSourceFactoryBean) serviceMessageSource; | ||
CompositeServiceMessageSource compositeServiceMessageSource = smffb.getDelegate(); | ||
} | ||
return null; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
.../io/microsphere/i18n/spring/boot/actuate/autoconfigure/I18nEndpointAutoConfiguration.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,49 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 | ||
* | ||
* http://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.microsphere.i18n.spring.boot.actuate.autoconfigure; | ||
|
||
import io.microsphere.i18n.spring.boot.actuate.I18nEndpoint; | ||
import io.microsphere.i18n.spring.boot.autoconfigure.I18nAutoConfiguration; | ||
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint; | ||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
/** | ||
* I18n Spring Boot Actuator Endpoint Auto-Configuration | ||
* | ||
* @author <a href="mailto:[email protected]">Mercy</a> | ||
* @see I18nEndpoint | ||
* @since 1.0.0 | ||
*/ | ||
@ConditionalOnClass(name = { | ||
"io.microsphere.i18n.ServiceMessageSource", // microsphere-i18n-core | ||
"io.microsphere.i18n.spring.context.I18nConfiguration", // microsphere-i18n-spring | ||
"org.springframework.boot.actuate.endpoint.annotation.Endpoint", // spring-boot-actuator-autoconfigure | ||
}) | ||
@ConditionalOnAvailableEndpoint(endpoint = I18nEndpoint.class) | ||
@AutoConfigureAfter(I18nAutoConfiguration.class) | ||
public class I18nEndpointAutoConfiguration { | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public I18nEndpoint i18nEndpoint() { | ||
return new I18nEndpoint(); | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
microsphere-i18n-spring-boot/src/main/resources/META-INF/spring.factories
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ | ||
io.microsphere.i18n.spring.boot.autoconfigure.I18nAutoConfiguration | ||
io.microsphere.i18n.spring.boot.autoconfigure.I18nAutoConfiguration,\ | ||
io.microsphere.i18n.spring.boot.actuate.autoconfigure.I18nEndpointAutoConfiguration |
3 changes: 2 additions & 1 deletion
3
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
io.microsphere.i18n.spring.boot.autoconfigure.I18nAutoConfiguration | ||
io.microsphere.i18n.spring.boot.autoconfigure.I18nAutoConfiguration | ||
io.microsphere.i18n.spring.boot.actuate.autoconfigure.I18nEndpointAutoConfiguration |
44 changes: 44 additions & 0 deletions
44
...sphere/i18n/spring/boot/actuate/autoconfigure/I18nEndpointAutoConfigurationBootstrap.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,44 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 | ||
* | ||
* http://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.microsphere.i18n.spring.boot.actuate.autoconfigure; | ||
|
||
import io.microsphere.i18n.spring.beans.factory.ServiceMessageSourceFactoryBean; | ||
import org.springframework.boot.WebApplicationType; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.builder.SpringApplicationBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
/** | ||
* TODO Comment | ||
* | ||
* @author <a href="mailto:[email protected]">Mercy</a> | ||
* @since TODO | ||
*/ | ||
@EnableAutoConfiguration | ||
public class I18nEndpointAutoConfigurationBootstrap { | ||
|
||
public static void main(String[] args) { | ||
new SpringApplicationBuilder(I18nEndpointAutoConfigurationBootstrap.class) | ||
.web(WebApplicationType.SERVLET) | ||
.run(args); | ||
} | ||
|
||
@Bean | ||
public static ServiceMessageSourceFactoryBean testServiceMessageSource() { | ||
return new ServiceMessageSourceFactoryBean("test"); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...e-i18n-spring-boot/src/test/resources/META-INF/i18n/common/i18n_messages_zh_CN.properties
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 @@ | ||
common.a = a |
2 changes: 2 additions & 0 deletions
2
...sphere-i18n-spring-boot/src/test/resources/META-INF/i18n/test/i18n_messages_en.properties
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,2 @@ | ||
test.a = test-a | ||
test.hello = Hello,{} |
2 changes: 2 additions & 0 deletions
2
...ere-i18n-spring-boot/src/test/resources/META-INF/i18n/test/i18n_messages_zh_CN.properties
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,2 @@ | ||
test.a = 测试-a | ||
test.hello = 您好,{} |
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
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