Changeset 35745
- Timestamp:
- 2021-11-24T13:59:16+13:00 (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/XSLTUtil.java
r35707 r35745 828 828 } 829 829 830 // generates javascript: 2 arrays are declared and populated with strings that declare variables and assign their values 831 // to be strings loaded from the interface_name.properties file for the language. 830 /** 831 * Generates a Javascript object graph to store language strings. Leaf nodes contain the string and the previous node, the key. 832 * Further preceding nodes denote the prefix of the string (i.e. the gs3 language strings object, which interface they belong to etc.). 833 * Accessing a language string on a well-formed JS object graph will be similar to this: const myString = gs.text.atea.asr.Title; 834 * @param interface_name The name of the interface to retrieve language strings for. 835 * @param lang The language to retrieve. 836 * @param prefix The prefix to place the language strings under (e.g. atea.asr). 837 * @param prependToPrefix 838 * @return Javascript code that will generate the language string object graph. 839 */ 832 840 public static String getInterfaceStringsAsJavascript(String interface_name, String lang, String prefix, String prependToPrefix) 833 841 { 834 842 // now we allow looking for files in the interface's resources folder 835 CustomClassLoader my_loader = new CustomClassLoader(XSLTUtil.class.getClassLoader(), GSFile.interfaceResourceDir(GlobalProperties.getGSDL3Home(), interface_name)); 843 CustomClassLoader my_loader = new CustomClassLoader(XSLTUtil.class.getClassLoader(), 844 GSFile.interfaceResourceDir(GlobalProperties.getGSDL3Home(), interface_name)); 836 845 String prefixwithdot = prefix + "."; 837 838 // 1. Generating Javascript of the form:839 // if(!gs.text) { gs.text = new Array(); }840 // if(!gs.text.dse) { gs.text.dse = new Array(); }841 846 842 847 StringBuffer outputStr = new StringBuffer(); 843 848 HashSet<String> initialisedDepths = new HashSet<>(); 844 849 850 // Initialise the root prefix object 845 851 outputStr.append("if(!" + prependToPrefix + ") { "); 846 outputStr.append(prependToPrefix + " = new Array(); ");852 outputStr.append(prependToPrefix + " = {}; "); 847 853 outputStr.append("}\n"); 848 854 initialisedDepths.add(prependToPrefix); 849 855 850 // Handle '.' characters in the prefix. JS identifies these as object accessors, hence requiring a new array for each 'level'. 856 // Initialise the prefix object structure, handling '.' characters. 857 // JS identifies these as property accessors, hence requiring a new object for each 'level'. 851 858 String currentDepth = prependToPrefix; 852 859 String[] prefixComponents = prefix.split("\\."); … … 856 863 857 864 outputStr.append("if(!" + currentDepth + ") { "); 858 outputStr.append(currentDepth + " = new Array(); ");865 outputStr.append(currentDepth + " = {}; "); 859 866 outputStr.append("}\n"); 860 867 … … 862 869 } 863 870 864 int foundCount = 0; 865 866 for (String dictName : new String[] { "interface_" + interface_name, "interface_default", "interface_default2" }) 867 { 868 // get all the keys from the english dictionary as this is a complete set 871 for (String dictName : new String[] { "interface_" + interface_name, "interface_default", 872 "interface_default2" }) { 873 // get all the keys from the english dictionary as this is a complete set 869 874 Dictionary dict = new Dictionary(dictName, "en", my_loader); 870 875 Enumeration keys = dict.getKeys(); 871 if (keys == null) 872 { 876 if (keys == null) { 873 877 continue; 874 878 } … … 876 880 // Get all properties in the language-specific dictionary with the given key prefix 877 881 // Create Javascript strings of the form: 878 // prependToPrefix.key= "value";\n 879 while (keys.hasMoreElements()) 880 { 882 // prefixPath.key= "value";\n 883 while (keys.hasMoreElements()) { 881 884 String key = (String) keys.nextElement(); 882 if (key.startsWith(prefixwithdot)) 883 { 885 if (key.startsWith(prefixwithdot)) { 884 886 String[] keyComponents = key.split("\\."); 885 887 currentDepth = prependToPrefix + "." + prefix; … … 893 895 894 896 outputStr.append("if(!" + currentDepth + ") { "); 895 outputStr.append(currentDepth + " = new Array(); ");897 outputStr.append(currentDepth + " = {}; "); 896 898 outputStr.append("}\n"); 897 899 … … 899 901 } 900 902 901 903 // get the language dependent value for the key. This will return the english if no value found for the given lang 902 904 String value = getInterfaceText(interface_name, dictName, lang, key, null); 903 905 … … 910 912 } 911 913 } 912 913 if (foundCount > 0)914 {915 break;916 }917 914 } 918 915
Note:
See TracChangeset
for help on using the changeset viewer.