From b5b29a78780f62e51545ab39a460827203b337e8 Mon Sep 17 00:00:00 2001 From: Mercy Ma Date: Wed, 22 May 2024 23:15:34 +0800 Subject: [PATCH] Add microsphere-i18n-spring-cloud-server module --- microsphere-i18n-dependencies/pom.xml | 18 +++ microsphere-i18n-spring-cloud-server/pom.xml | 121 ++++++++++++++++++ .../server/annotation/EnableI18nServer.java | 38 ++++++ .../annotation/I18nServerConfiguration.java | 90 +++++++++++++ .../controller/I18nServerController.java | 53 ++++++++ .../main/resources/META-INF/spring.factories | 0 ...ot.autoconfigure.AutoConfiguration.imports | 0 .../cloud/server/I18nServerBootstrap.java | 41 ++++++ .../i18n/common/i18n_messages_en.properties | 1 + .../common/i18n_messages_zh_CN.properties | 1 + .../src/test/resources/application.properties | 9 ++ .../src/test/resources/application.properties | 1 + pom.xml | 1 + 13 files changed, 374 insertions(+) create mode 100644 microsphere-i18n-spring-cloud-server/pom.xml create mode 100644 microsphere-i18n-spring-cloud-server/src/main/java/io/microsphere/i18n/spring/cloud/server/annotation/EnableI18nServer.java create mode 100644 microsphere-i18n-spring-cloud-server/src/main/java/io/microsphere/i18n/spring/cloud/server/annotation/I18nServerConfiguration.java create mode 100644 microsphere-i18n-spring-cloud-server/src/main/java/io/microsphere/i18n/spring/cloud/server/controller/I18nServerController.java create mode 100644 microsphere-i18n-spring-cloud-server/src/main/resources/META-INF/spring.factories create mode 100644 microsphere-i18n-spring-cloud-server/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports create mode 100644 microsphere-i18n-spring-cloud-server/src/test/java/io/microsphere/i18n/spring/cloud/server/I18nServerBootstrap.java create mode 100644 microsphere-i18n-spring-cloud-server/src/test/resources/META-INF/i18n/common/i18n_messages_en.properties create mode 100644 microsphere-i18n-spring-cloud-server/src/test/resources/META-INF/i18n/common/i18n_messages_zh_CN.properties create mode 100644 microsphere-i18n-spring-cloud-server/src/test/resources/application.properties diff --git a/microsphere-i18n-dependencies/pom.xml b/microsphere-i18n-dependencies/pom.xml index d7359f5..80d1448 100644 --- a/microsphere-i18n-dependencies/pom.xml +++ b/microsphere-i18n-dependencies/pom.xml @@ -39,6 +39,24 @@ ${revision} + + io.github.microsphere-projects + microsphere-i18n-spring-boot + ${revision} + + + + io.github.microsphere-projects + microsphere-i18n-spring-cloud + ${revision} + + + + io.github.microsphere-projects + microsphere-i18n-spring-cloud-server + ${revision} + + diff --git a/microsphere-i18n-spring-cloud-server/pom.xml b/microsphere-i18n-spring-cloud-server/pom.xml new file mode 100644 index 0000000..4a0fd25 --- /dev/null +++ b/microsphere-i18n-spring-cloud-server/pom.xml @@ -0,0 +1,121 @@ + + + + io.github.microsphere-projects + microsphere-i18n-parent + ${revision} + ../microsphere-i18n-parent/pom.xml + + 4.0.0 + + io.github.microsphere-projects + microsphere-i18n-spring-cloud-server + ${revision} + jar + + Microsphere :: Internationalisation :: Spring Cloud :: Server + Microsphere Internationalisation Spring Cloud Server + + + + + + io.github.microsphere-projects + microsphere-i18n-spring-cloud + ${revision} + + + + + org.springframework.cloud + spring-cloud-commons + true + + + + org.springframework.cloud + spring-cloud-context + true + + + + + org.springframework.boot + spring-boot-autoconfigure + true + + + + org.springframework.boot + spring-boot-starter-web + true + + + + + org.springframework.boot + spring-boot-actuator-autoconfigure + true + + + + + javax.servlet + javax.servlet-api + provided + + + + + org.slf4j + slf4j-api + true + + + + + junit + junit + test + + + + org.junit.jupiter + junit-jupiter + test + + + + org.assertj + assertj-core + test + + + + com.jayway.jsonpath + json-path-assert + test + + + + org.springframework + spring-test + test + + + + org.springframework.boot + spring-boot-test + test + + + + ch.qos.logback + logback-classic + test + + + + \ No newline at end of file diff --git a/microsphere-i18n-spring-cloud-server/src/main/java/io/microsphere/i18n/spring/cloud/server/annotation/EnableI18nServer.java b/microsphere-i18n-spring-cloud-server/src/main/java/io/microsphere/i18n/spring/cloud/server/annotation/EnableI18nServer.java new file mode 100644 index 0000000..1eb7f8f --- /dev/null +++ b/microsphere-i18n-spring-cloud-server/src/main/java/io/microsphere/i18n/spring/cloud/server/annotation/EnableI18nServer.java @@ -0,0 +1,38 @@ +/* + * 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.server.annotation; + +import org.springframework.context.annotation.Import; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Enable I18n Server + * + * @author Mercy + * @since 1.0.0 + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Import(I18nServerConfiguration.class) +public @interface EnableI18nServer { +} diff --git a/microsphere-i18n-spring-cloud-server/src/main/java/io/microsphere/i18n/spring/cloud/server/annotation/I18nServerConfiguration.java b/microsphere-i18n-spring-cloud-server/src/main/java/io/microsphere/i18n/spring/cloud/server/annotation/I18nServerConfiguration.java new file mode 100644 index 0000000..eb80639 --- /dev/null +++ b/microsphere-i18n-spring-cloud-server/src/main/java/io/microsphere/i18n/spring/cloud/server/annotation/I18nServerConfiguration.java @@ -0,0 +1,90 @@ +/* + * 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.server.annotation; + +import io.microsphere.i18n.CompositeServiceMessageSource; +import io.microsphere.i18n.ServiceMessageSource; +import io.microsphere.i18n.spring.PropertySourcesServiceMessageSource; +import io.microsphere.i18n.spring.cloud.server.controller.I18nServerController; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.event.ApplicationStartedEvent; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.Lazy; +import org.springframework.context.event.EventListener; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.Environment; + +import java.util.Arrays; +import java.util.List; +import java.util.Locale; +import java.util.Set; +import java.util.stream.Collectors; + +import static io.microsphere.i18n.AbstractResourceServiceMessageSource.DEFAULT_RESOURCE_NAME_PREFIX; +import static io.microsphere.i18n.AbstractResourceServiceMessageSource.DEFAULT_RESOURCE_NAME_SUFFIX; +import static io.microsphere.spring.util.PropertySourcesUtils.findPropertyNames; +import static java.util.Arrays.asList; + +/** + * The Configuration Class for I18n Server + * + * @author Mercy + * @see PropertySourcesServiceMessageSource + * @since 1.0.0 + */ +@Import(I18nServerController.class) +public class I18nServerConfiguration { + + @Autowired + private ConfigurableEnvironment environment; + + @Autowired + private DiscoveryClient discoveryClient; + + @Bean + @Lazy + public CompositeServiceMessageSource lazyCompositeServiceMessageSource() { + return new CompositeServiceMessageSource(); + } + + @EventListener(ApplicationStartedEvent.class) + public void onApplicationStartedEvent(ApplicationStartedEvent event) { + ConfigurableApplicationContext context = event.getApplicationContext(); + CompositeServiceMessageSource lazyCompositeServiceMessageSource = + context.getBean("lazyCompositeServiceMessageSource", CompositeServiceMessageSource.class); + List services = discoveryClient.getServices(); + List serviceMessageSources = services.stream() + .map(this::buildServiceMessageSource) + .collect(Collectors.toList()); + lazyCompositeServiceMessageSource.setServiceMessageSources(serviceMessageSources); + } + + private ServiceMessageSource buildServiceMessageSource(String service) { + PropertySourcesServiceMessageSource serviceMessageSource = new PropertySourcesServiceMessageSource(service); + serviceMessageSource.setEnvironment(environment); + // FIXME Hardcode + serviceMessageSource.setDefaultLocale(Locale.getDefault()); + serviceMessageSource.setSupportedLocales(asList(Locale.getDefault(), Locale.ENGLISH)); + serviceMessageSource.init(); + return serviceMessageSource; + } + +} diff --git a/microsphere-i18n-spring-cloud-server/src/main/java/io/microsphere/i18n/spring/cloud/server/controller/I18nServerController.java b/microsphere-i18n-spring-cloud-server/src/main/java/io/microsphere/i18n/spring/cloud/server/controller/I18nServerController.java new file mode 100644 index 0000000..27d9b62 --- /dev/null +++ b/microsphere-i18n-spring-cloud-server/src/main/java/io/microsphere/i18n/spring/cloud/server/controller/I18nServerController.java @@ -0,0 +1,53 @@ +/* + * 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.server.controller; + +import io.microsphere.i18n.spring.PropertySourcesServiceMessageSource; +import io.microsphere.i18n.spring.boot.actuate.I18nEndpoint; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static io.microsphere.i18n.AbstractResourceServiceMessageSource.DEFAULT_RESOURCE_NAME_PREFIX; +import static io.microsphere.i18n.AbstractResourceServiceMessageSource.DEFAULT_RESOURCE_NAME_SUFFIX; +import static io.microsphere.spring.util.PropertySourcesUtils.findPropertyNames; + +/** + * The REST-Controler for I18n server + * + * @author Mercy + * @see PropertySourcesServiceMessageSource + * @since 1.0.0 + */ +@RestController +public class I18nServerController { + + @Autowired + private I18nEndpoint i18nEndpoint; + + @GetMapping("/messages") + public Map> getMessages() { + return i18nEndpoint.invoke(); + } + +} diff --git a/microsphere-i18n-spring-cloud-server/src/main/resources/META-INF/spring.factories b/microsphere-i18n-spring-cloud-server/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..e69de29 diff --git a/microsphere-i18n-spring-cloud-server/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/microsphere-i18n-spring-cloud-server/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000..e69de29 diff --git a/microsphere-i18n-spring-cloud-server/src/test/java/io/microsphere/i18n/spring/cloud/server/I18nServerBootstrap.java b/microsphere-i18n-spring-cloud-server/src/test/java/io/microsphere/i18n/spring/cloud/server/I18nServerBootstrap.java new file mode 100644 index 0000000..bba0471 --- /dev/null +++ b/microsphere-i18n-spring-cloud-server/src/test/java/io/microsphere/i18n/spring/cloud/server/I18nServerBootstrap.java @@ -0,0 +1,41 @@ +/* + * 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.server; + +import io.microsphere.i18n.spring.cloud.server.annotation.EnableI18nServer; +import org.springframework.boot.WebApplicationType; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +/** + * I18n Server Bootstrap + * + * @author Mercy + * @since 1.0.0 + */ +@SpringBootApplication +@EnableDiscoveryClient +@EnableI18nServer +public class I18nServerBootstrap { + + public static void main(String[] args) { + new SpringApplicationBuilder(I18nServerBootstrap.class) + .web(WebApplicationType.SERVLET) + .run(args); + } +} diff --git a/microsphere-i18n-spring-cloud-server/src/test/resources/META-INF/i18n/common/i18n_messages_en.properties b/microsphere-i18n-spring-cloud-server/src/test/resources/META-INF/i18n/common/i18n_messages_en.properties new file mode 100644 index 0000000..ecc8d4c --- /dev/null +++ b/microsphere-i18n-spring-cloud-server/src/test/resources/META-INF/i18n/common/i18n_messages_en.properties @@ -0,0 +1 @@ +common.a = a \ No newline at end of file diff --git a/microsphere-i18n-spring-cloud-server/src/test/resources/META-INF/i18n/common/i18n_messages_zh_CN.properties b/microsphere-i18n-spring-cloud-server/src/test/resources/META-INF/i18n/common/i18n_messages_zh_CN.properties new file mode 100644 index 0000000..6aadb97 --- /dev/null +++ b/microsphere-i18n-spring-cloud-server/src/test/resources/META-INF/i18n/common/i18n_messages_zh_CN.properties @@ -0,0 +1 @@ +common.a = 啊 \ No newline at end of file diff --git a/microsphere-i18n-spring-cloud-server/src/test/resources/application.properties b/microsphere-i18n-spring-cloud-server/src/test/resources/application.properties new file mode 100644 index 0000000..ccd719b --- /dev/null +++ b/microsphere-i18n-spring-cloud-server/src/test/resources/application.properties @@ -0,0 +1,9 @@ +spring.application.name=i18n-server +management.endpoint.i18n.enabled=true +management.endpoints.web.exposure.include=* +management.endpoint.env.post.enabled=true + +spring.cloud.discovery.client.simple.instances.test-application[0].instanceId=1 +spring.cloud.discovery.client.simple.instances.test-application[0].serviceId=test-application +spring.cloud.discovery.client.simple.instances.test-application[0].host=127.0.0.1 +spring.cloud.discovery.client.simple.instances.test-application[0].port=8080 \ No newline at end of file diff --git a/microsphere-i18n-spring-cloud/src/test/resources/application.properties b/microsphere-i18n-spring-cloud/src/test/resources/application.properties index be2f681..abdecaa 100644 --- a/microsphere-i18n-spring-cloud/src/test/resources/application.properties +++ b/microsphere-i18n-spring-cloud/src/test/resources/application.properties @@ -1,3 +1,4 @@ +spring.application.name=i18n-bootstrap management.endpoint.i18n.enabled=true management.endpoints.web.exposure.include=* management.endpoint.env.post.enabled=true \ No newline at end of file diff --git a/pom.xml b/pom.xml index 0f1f1ff..09a118d 100644 --- a/pom.xml +++ b/pom.xml @@ -63,6 +63,7 @@ microsphere-i18n-spring microsphere-i18n-spring-boot microsphere-i18n-spring-cloud + microsphere-i18n-spring-cloud-server