Skip to content

Commit

Permalink
Add microsphere-i18n-spring-cloud-server module
Browse files Browse the repository at this point in the history
  • Loading branch information
mercyblitz committed May 22, 2024
1 parent 3a44c42 commit b5b29a7
Show file tree
Hide file tree
Showing 13 changed files with 374 additions and 0 deletions.
18 changes: 18 additions & 0 deletions microsphere-i18n-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@
<version>${revision}</version>
</dependency>

<dependency>
<groupId>io.github.microsphere-projects</groupId>
<artifactId>microsphere-i18n-spring-boot</artifactId>
<version>${revision}</version>
</dependency>

<dependency>
<groupId>io.github.microsphere-projects</groupId>
<artifactId>microsphere-i18n-spring-cloud</artifactId>
<version>${revision}</version>
</dependency>

<dependency>
<groupId>io.github.microsphere-projects</groupId>
<artifactId>microsphere-i18n-spring-cloud-server</artifactId>
<version>${revision}</version>
</dependency>

</dependencies>

</dependencyManagement>
Expand Down
121 changes: 121 additions & 0 deletions microsphere-i18n-spring-cloud-server/pom.xml
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>
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 {
}
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;
}

}
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.
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);
}
}
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,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
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
Loading

0 comments on commit b5b29a7

Please sign in to comment.