View Javadoc
1   /**
2    * Copyright 2011-2013 The Kuali Foundation Licensed under the Educational
3    * Community License, Version 2.0 (the "License"); you may not use this file
4    * except in compliance with the License. You may obtain a copy of the License
5    * at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12   * License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  package org.kuali.mobility.util;
16  
17  import java.util.Collections;
18  import java.util.HashMap;
19  import java.util.List;
20  import java.util.Map;
21  
22  import javax.annotation.Resource;
23  import javax.servlet.http.HttpServletRequest;
24  
25  import org.springframework.context.annotation.DependsOn;
26  import org.springframework.stereotype.Component;
27  
28  /**
29   * A bean used to help with localisation
30   * @author Kuali Mobility Team (mobility.collab@kuali.org)
31   * @since 
32   */
33  @Component(value="localisationUtil")
34  @DependsOn(value={"supportedLanguages"})
35  public class LocalisationUtil {
36  
37  	/**
38  	 * A reference to the list of supported languages
39  	 */
40  	@Resource(name="supportedLanguages")
41  	private List<String> supportedLanguages;
42  	
43  	/**
44  	 * Returns a map of language code to String for that language
45  	 * @param fieldName
46  	 * @param request
47  	 * @return
48  	 */
49  	public Map<String, String> getLocalisedString(String fieldName, HttpServletRequest request){
50  		Map<String, String> fieldMap = new HashMap<String, String>();
51  		
52  		String value;
53  		for(String language : this.supportedLanguages){
54  			value = request.getParameter(fieldName + "." + language);
55  			if(value != null){
56  				fieldMap.put(language, value);
57  			}
58  		}
59  		return fieldMap;
60  	}
61  	
62  	/**
63  	 * Gets the localised code for a field.
64  	 * This is the code that is used in localised properties files, or as a key
65  	 * for the database.
66  	 * @param fieldName
67  	 * @param request
68  	 * @return
69  	 */
70  	public String getLocalisedStringCode(String fieldName, HttpServletRequest request){
71  		String code = request.getParameter(fieldName + ".code");
72  		return code == null ? generateNewCode() : code;
73  	}
74  	
75  	/**
76  	 * Gets the list of supported languages
77  	 * @return
78  	 */
79  	public List<String> getSupportedLanguages(){
80  		return Collections.unmodifiableList(supportedLanguages);
81  	}
82  	
83  	/**
84  	 * Returns true if the language is a language that we support.
85  	 * @param language
86  	 * @return
87  	 */
88  	public boolean isSupportedLanguage(String language){
89  		return supportedLanguages.contains(language);
90  	}
91  	
92  	/**
93  	 * Generate a new
94  	 * @return
95  	 */
96  	private String generateNewCode(){
97  		// TODO use something better
98  		return System.currentTimeMillis() + "";
99  	}
100 }