Skip to content

Commit

Permalink
Add microsphere-i18n-spring-cloud module
Browse files Browse the repository at this point in the history
  • Loading branch information
mercyblitz committed May 9, 2024
1 parent 4812112 commit 79f16c2
Show file tree
Hide file tree
Showing 12 changed files with 295 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,36 @@
*/
public interface ReloadableResourceServiceMessageSource extends ResourceServiceMessageSource {

/**
* Reload if {@link #canReload(String)} returns <code>true</code>,
* The calling {@link #initializeResource(String)} as default
*
* @param changedResource Changes in the resource
*/
default void reload(String changedResource) {
initializeResource(changedResource);
}

/**
* Reload if {@link #canReload(Iterable)} returns <code>true</code>,
* The calling {@link #initializeResources(Iterable)} as default
* @param changedResources Changes in the resources
*/
default void reload(Iterable<String> changedResources) {
initializeResources(changedResources);
}

/**
* Whether the specified resource can be overloaded
*
* @param changedResource Changes in the resource
* @return Supported by default, returning <code>true<code>
*/
default boolean canReload(String changedResource) {
Set<String> resources = getInitializeResources();
return resources.contains(changedResource);
}

/**
* Whether the specified resource list can be overloaded
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
management.endpoint.i18n.enabled=true
management.endpoints.web.exposure.include=*
103 changes: 103 additions & 0 deletions microsphere-i18n-spring-cloud/pom.xml
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>
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 {

}
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;
}
}
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.microsphere.i18n.spring.cloud.autoconfigure.I18nCloudAutoConfiguration
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
common.a = a
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
common.a = 啊
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
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<module>microsphere-i18n-openfeign</module>
<module>microsphere-i18n-spring</module>
<module>microsphere-i18n-spring-boot</module>
<module>microsphere-i18n-spring-cloud</module>
</modules>

<distributionManagement>
Expand Down

0 comments on commit 79f16c2

Please sign in to comment.