From: Olivier Lamy Date: Mon, 19 Dec 2011 14:43:57 +0000 (+0000) Subject: add i18n rest services for Archiva X-Git-Tag: archiva-1.4-M3~1702 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ef781c9859e100b600a2633d3c05c49cd942e377;p=archiva.git add i18n rest services for Archiva git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1220779 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/CommonServices.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/CommonServices.java new file mode 100644 index 000000000..c33615a5d --- /dev/null +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/CommonServices.java @@ -0,0 +1,49 @@ +package org.apache.archiva.rest.api.services; +/* + * 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. + */ + +import org.codehaus.plexus.redback.authorization.RedbackAuthorization; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; + +/** + * contains some "free" services (i18n) + * + * @author Olivier Lamy + * @since 1.4-M3 + */ +@Path( "/commonServices/" ) +public interface CommonServices +{ + @Path( "getI18nResources" ) + @GET + @Produces( { MediaType.TEXT_PLAIN } ) + @RedbackAuthorization( noRestriction = true ) + /** + * will return properties available in org/apache/archiva/i18n/default.properties + * load default (en) then override with locale used so at least en are returned if no + * translation in the locale asked. + */ + String getI18nResources( @QueryParam( "locale" ) String locale ) + throws ArchivaRestServiceException; +} diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultCommonServices.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultCommonServices.java new file mode 100644 index 000000000..75cffe74d --- /dev/null +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultCommonServices.java @@ -0,0 +1,104 @@ +package org.apache.archiva.rest.services; +/* + * 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. + */ + +import org.apache.archiva.rest.api.services.ArchivaRestServiceException; +import org.apache.archiva.rest.api.services.CommonServices; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Map; +import java.util.Properties; + +/** + * @author Olivier Lamy + */ +@Service( "commonServices#rest" ) +public class DefaultCommonServices + implements CommonServices +{ + + private Logger log = LoggerFactory.getLogger( getClass() ); + + public String getI18nResources( String locale ) + throws ArchivaRestServiceException + { + Properties properties = new Properties(); + + StringBuilder resourceName = new StringBuilder( "org/apache/archiva/i18n/default" ); + try + { + + loadResource( properties, resourceName, locale ); + + } + catch ( IOException e ) + { + log.warn( "skip error loading properties {}", resourceName.toString() ); + } + + StringBuilder output = new StringBuilder(); + + for ( Map.Entry entry : properties.entrySet() ) + { + output.append( (String) entry.getKey() ).append( '=' ).append( (String) entry.getValue() ); + output.append( '\n' ); + } + + return output.toString(); + } + + private void loadResource( Properties properties, StringBuilder resourceName, String locale ) + throws IOException + { + // load default + loadResource( properties, new StringBuilder( resourceName ).append( ".properties" ).toString() ); + // if locale override with locale content + if ( StringUtils.isNotEmpty( locale ) ) + { + loadResource( properties, + new StringBuilder( resourceName ).append( "_" + locale ).append( ".properties" ).toString() ); + } + + } + + private void loadResource( Properties properties, String resourceName ) + throws IOException + { + InputStream is = null; + + try + { + is = Thread.currentThread().getContextClassLoader().getResourceAsStream( resourceName.toString() ); + if ( is != null ) + { + properties.load( is ); + } + } + finally + { + IOUtils.closeQuietly( is ); + } + } +} diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/resources/META-INF/spring-context.xml b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/resources/META-INF/spring-context.xml index 5cd75664d..6f0b6b9bc 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/resources/META-INF/spring-context.xml +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/resources/META-INF/spring-context.xml @@ -57,6 +57,7 @@ + diff --git a/archiva-modules/archiva-web/archiva-webapp-common/src/main/resources/org/apache/archiva/i18n/default.properties b/archiva-modules/archiva-web/archiva-webapp-common/src/main/resources/org/apache/archiva/i18n/default.properties new file mode 100644 index 000000000..22d52654e --- /dev/null +++ b/archiva-modules/archiva-web/archiva-webapp-common/src/main/resources/org/apache/archiva/i18n/default.properties @@ -0,0 +1,21 @@ +# +# 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. +# +# -------------------------------------------------------------------- +# Archiva webapp i18n default en file +# -------------------------------------------------------------------- diff --git a/archiva-modules/archiva-web/archiva-webapp-common/src/main/resources/org/apache/archiva/i18n/default_fr.properties b/archiva-modules/archiva-web/archiva-webapp-common/src/main/resources/org/apache/archiva/i18n/default_fr.properties new file mode 100644 index 000000000..9ad0368a2 --- /dev/null +++ b/archiva-modules/archiva-web/archiva-webapp-common/src/main/resources/org/apache/archiva/i18n/default_fr.properties @@ -0,0 +1,21 @@ +# +# 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. +# +# -------------------------------------------------------------------- +# Archiva webapp i18n fr file +# -------------------------------------------------------------------- diff --git a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/index.html b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/index.html index dc3054724..73fe81d0c 100644 --- a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/index.html +++ b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/index.html @@ -45,16 +45,16 @@ .script("jquery.tmpl.js").wait() .script("archiva/utils.js").wait() .script("jquery.i18n.properties-1.0.9.js").wait() - .script("redback/i18nload.js").wait() - .script("archiva/main-tmpl.js").wait() + .script("archiva/i18nload.js").wait() .script("jquery.cookie.1.0.0.js").wait() .script("knockout-debug.js").wait() .script("jquery-ui-1.8.16.custom.min.js").wait() .script("jquery.validate.js").wait() .script("jquery.json-2.3.min.js").wait() + .script("archiva/main-tmpl.js").wait() .script("redback/operation.js").wait() .script("redback/redback-tmpl.js").wait() - .script("bootstrap-tabs.js") + .script("bootstrap-tabs.js").wait() .script("bootstrap-modal.js").wait() .script("bootstrap-alerts.js").wait() .script("bootstrap-dropdown.js").wait() diff --git a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/i18nload.js b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/i18nload.js new file mode 100644 index 000000000..abee4c156 --- /dev/null +++ b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/i18nload.js @@ -0,0 +1,35 @@ +/* + * 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. + */ + +$(function() { + // load i18n resources from rest call + // first redback then archiva + // -- redback + // load default + loadAndParseFile("restServices/redbackServices/utilServices/getBundleResources", {cache:false, mode: 'map',encoding:'utf-8'}); + // load browser locale + var browserLang = $.i18n.browserLang(); + loadAndParseFile("restServices/redbackServices/utilServices/getBundleResources?locale="+browserLang, {cache:false, mode: 'map',encoding:'utf-8'}); + // -- archiva + // load default + loadAndParseFile("restServices/archivaServices/commonServices/getI18nResources", {cache:false, mode: 'map',encoding:'utf-8'}); + // load browser locale + var browserLang = $.i18n.browserLang(); + loadAndParseFile("restServices/archivaServices/commonServices/getI18nResources?locale="+browserLang, {cache:false, mode: 'map',encoding:'utf-8'}); +}); \ No newline at end of file diff --git a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/redback/i18nload.js b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/redback/i18nload.js deleted file mode 100644 index 36bca5d97..000000000 --- a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/redback/i18nload.js +++ /dev/null @@ -1,26 +0,0 @@ -/* - * 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. - */ - -$(function() { - // load default - loadAndParseFile("restServices/redbackServices/utilServices/getBundleResources", {cache:false, mode: 'map',encoding:'utf-8'}); - // load browser locale - var browserLang = $.i18n.browserLang(); - loadAndParseFile("restServices/redbackServices/utilServices/getBundleResources?locale="+browserLang, {cache:false, mode: 'map',encoding:'utf-8'}); -}); \ No newline at end of file