-
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.
Add microsphere-i18n-spring-cloud module
- Loading branch information
1 parent
4812112
commit 79f16c2
Showing
12 changed files
with
295 additions
and
0 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
2 changes: 2 additions & 0 deletions
2
microsphere-i18n-spring-boot/src/test/resources/application.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 @@ | ||
management.endpoint.i18n.enabled=true | ||
management.endpoints.web.exposure.include=* |
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,103 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<groupId>io.github.microsphere-projects</groupId> | ||
<artifactId>microsphere-i18n-parent</artifactId> | ||
<version>${revision}</version> | ||
<relativePath>../microsphere-i18n-parent/pom.xml</relativePath> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>io.github.microsphere-projects</groupId> | ||
<artifactId>microsphere-i18n-spring-cloud</artifactId> | ||
<version>${revision}</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>Microsphere :: Internationalisation :: Spring Cloud</name> | ||
<description>Microsphere Internationalisation Spring Cloud</description> | ||
|
||
<dependencies> | ||
|
||
<!-- Microsphere --> | ||
<dependency> | ||
<groupId>io.github.microsphere-projects</groupId> | ||
<artifactId>microsphere-i18n-spring-boot</artifactId> | ||
<version>${revision}</version> | ||
</dependency> | ||
|
||
<!-- Spring Cloud Dependencies --> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-commons</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-context</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
|
||
<!-- Spring Boot --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-autoconfigure</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
|
||
<!-- Spring Boot Actuator --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-actuator-autoconfigure</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
|
||
<!-- Servlet API --> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>javax.servlet-api</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<!-- slf4j API --> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
|
||
<!-- Testing --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-classic</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
44 changes: 44 additions & 0 deletions
44
.../main/java/io/microsphere/i18n/spring/cloud/autoconfigure/I18nCloudAutoConfiguration.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.cloud.autoconfigure; | ||
|
||
import io.microsphere.i18n.spring.annotation.EnableI18n; | ||
import io.microsphere.i18n.spring.beans.factory.ServiceMessageSourceFactoryBean; | ||
import io.microsphere.i18n.spring.boot.condition.ConditionalOnI18nEnabled; | ||
import io.microsphere.i18n.spring.cloud.event.ReloadableResourceServiceMessageSourceListener; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Import; | ||
|
||
/** | ||
* I18n Auto-Configuration for Spring Cloud | ||
* | ||
* @author <a href="mailto:[email protected]">Mercy</a> | ||
* @since 1.0.0 | ||
*/ | ||
@ConditionalOnI18nEnabled | ||
@ConditionalOnClass(name = { | ||
"org.springframework.cloud.context.environment.EnvironmentChangeEvent", // spring-cloud-context | ||
}) | ||
@Import({ | ||
ReloadableResourceServiceMessageSourceListener.class | ||
}) | ||
public class I18nCloudAutoConfiguration { | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
...o/microsphere/i18n/spring/cloud/event/ReloadableResourceServiceMessageSourceListener.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,75 @@ | ||
/* | ||
* 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.cloud.event; | ||
|
||
import io.microsphere.i18n.ReloadableResourceServiceMessageSource; | ||
import io.microsphere.i18n.ServiceMessageSource; | ||
import io.microsphere.i18n.spring.DelegatingServiceMessageSource; | ||
import io.microsphere.i18n.spring.PropertySourcesServiceMessageSource; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.BeanFactory; | ||
import org.springframework.beans.factory.BeanFactoryAware; | ||
import org.springframework.beans.factory.SmartInitializingSingleton; | ||
import org.springframework.cloud.context.environment.EnvironmentChangeEvent; | ||
import org.springframework.context.ApplicationListener; | ||
|
||
import java.util.Set; | ||
|
||
import static io.microsphere.i18n.spring.constants.I18nConstants.SERVICE_MESSAGE_SOURCE_BEAN_NAME; | ||
|
||
/** | ||
* * An {@link ApplicationListener} of {@link EnvironmentChangeEvent} to reload | ||
* {@link ServiceMessageSource} dynamically at the runtime. | ||
* | ||
* @author <a href="mailto:[email protected]">Mercy</a> | ||
* @see PropertySourcesServiceMessageSource | ||
* @see DelegatingServiceMessageSource | ||
* @see ReloadableResourceServiceMessageSource | ||
* @see ServiceMessageSource | ||
* @see EnvironmentChangeEvent | ||
* @since 1.0.0 | ||
*/ | ||
public class ReloadableResourceServiceMessageSourceListener implements SmartInitializingSingleton, | ||
ApplicationListener<EnvironmentChangeEvent>, BeanFactoryAware { | ||
|
||
private BeanFactory beanFactory; | ||
|
||
private ReloadableResourceServiceMessageSource reloadableResourceServiceMessageSource; | ||
|
||
@Override | ||
public void onApplicationEvent(EnvironmentChangeEvent event) { | ||
Set<String> changedPropertyNames = event.getKeys(); | ||
for (String changedPropertyName : changedPropertyNames) { | ||
String resource = changedPropertyName; | ||
if (reloadableResourceServiceMessageSource.canReload(resource)) { | ||
reloadableResourceServiceMessageSource.reload(resource); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void afterSingletonsInstantiated() { | ||
// Lookup the primary bean of PropertySourcesServiceMessageSource | ||
this.reloadableResourceServiceMessageSource = this.beanFactory.getBean(SERVICE_MESSAGE_SOURCE_BEAN_NAME, | ||
ReloadableResourceServiceMessageSource.class); | ||
} | ||
|
||
@Override | ||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException { | ||
this.beanFactory = beanFactory; | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
microsphere-i18n-spring-cloud/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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ | ||
io.microsphere.i18n.spring.cloud.autoconfigure.I18nCloudAutoConfiguration |
1 change: 1 addition & 0 deletions
1
...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 |
---|---|---|
@@ -0,0 +1 @@ | ||
io.microsphere.i18n.spring.cloud.autoconfigure.I18nCloudAutoConfiguration |
40 changes: 40 additions & 0 deletions
40
...phere-i18n-spring-cloud/src/test/java/io/microsphere/i18n/spring/cloud/I18nBootstrap.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,40 @@ | ||
/* | ||
* 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.cloud; | ||
|
||
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; | ||
|
||
/** | ||
* I18n Bootstrap | ||
* | ||
* @author <a href="mailto:[email protected]">Mercy</a> | ||
* @since TODO | ||
*/ | ||
@EnableAutoConfiguration | ||
public class I18nBootstrap { | ||
|
||
public static void main(String[] args) { | ||
new SpringApplicationBuilder(I18nBootstrap.class) | ||
.web(WebApplicationType.SERVLET) | ||
.properties("common.i18n_messages_en.properties=common.a = common-a-2024") | ||
.run(args); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...ere-i18n-spring-cloud/src/test/resources/META-INF/i18n/common/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 @@ | ||
common.a = a |
1 change: 1 addition & 0 deletions
1
...-i18n-spring-cloud/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 = 啊 |
3 changes: 3 additions & 0 deletions
3
microsphere-i18n-spring-cloud/src/test/resources/application.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,3 @@ | ||
management.endpoint.i18n.enabled=true | ||
management.endpoints.web.exposure.include=* | ||
management.endpoint.env.post.enabled=true |
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