-
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-server module
- Loading branch information
1 parent
3a44c42
commit b5b29a7
Showing
13 changed files
with
374 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
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,121 @@ | ||
<?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-server</artifactId> | ||
<version>${revision}</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>Microsphere :: Internationalisation :: Spring Cloud :: Server</name> | ||
<description>Microsphere Internationalisation Spring Cloud Server</description> | ||
|
||
<dependencies> | ||
|
||
<!-- Microsphere --> | ||
<dependency> | ||
<groupId>io.github.microsphere-projects</groupId> | ||
<artifactId>microsphere-i18n-spring-cloud</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.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.jayway.jsonpath</groupId> | ||
<artifactId>json-path-assert</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> |
38 changes: 38 additions & 0 deletions
38
...er/src/main/java/io/microsphere/i18n/spring/cloud/server/annotation/EnableI18nServer.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,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 <a href="mailto:[email protected]">Mercy</a> | ||
* @since 1.0.0 | ||
*/ | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@Import(I18nServerConfiguration.class) | ||
public @interface EnableI18nServer { | ||
} |
90 changes: 90 additions & 0 deletions
90
...main/java/io/microsphere/i18n/spring/cloud/server/annotation/I18nServerConfiguration.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,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 <a href="mailto:[email protected]">Mercy</a> | ||
* @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<String> services = discoveryClient.getServices(); | ||
List<ServiceMessageSource> 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; | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
...rc/main/java/io/microsphere/i18n/spring/cloud/server/controller/I18nServerController.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,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 <a href="mailto:[email protected]">Mercy</a> | ||
* @see PropertySourcesServiceMessageSource | ||
* @since 1.0.0 | ||
*/ | ||
@RestController | ||
public class I18nServerController { | ||
|
||
@Autowired | ||
private I18nEndpoint i18nEndpoint; | ||
|
||
@GetMapping("/messages") | ||
public Map<String, Map<String, String>> getMessages() { | ||
return i18nEndpoint.invoke(); | ||
} | ||
|
||
} |
Empty file.
Empty file.
41 changes: 41 additions & 0 deletions
41
...oud-server/src/test/java/io/microsphere/i18n/spring/cloud/server/I18nServerBootstrap.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,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 <a href="mailto:[email protected]">Mercy</a> | ||
* @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); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...n-spring-cloud-server/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
...pring-cloud-server/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 = 啊 |
9 changes: 9 additions & 0 deletions
9
microsphere-i18n-spring-cloud-server/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,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 |
1 change: 1 addition & 0 deletions
1
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 |
---|---|---|
@@ -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 |
Oops, something went wrong.