1 29 30 package com.caucho.quercus.lib.gettext; 31 32 import com.caucho.vfs.BasicDependencyContainer; 33 import com.caucho.quercus.QuercusModuleException; 34 import com.caucho.quercus.env.Env; 35 import com.caucho.quercus.env.StringValue; 36 import com.caucho.quercus.env.UnicodeValue; 37 import com.caucho.quercus.lib.gettext.expr.PluralExpr; 38 import com.caucho.vfs.Depend; 39 import com.caucho.vfs.Path; 40 41 import java.io.IOException ; 42 import java.util.ArrayList ; 43 import java.util.HashMap ; 44 import java.util.Locale ; 45 46 49 class GettextResource 50 { 51 protected Path _pathPO; 52 private Path _pathMO; 53 private Path _currentPath; 54 55 private BasicDependencyContainer _depend; 56 57 private PluralExpr _pluralExpr; 58 59 private HashMap <UnicodeValue, ArrayList <UnicodeValue>> _translations; 60 61 protected GettextResource(Env env, 62 Path root, 63 Locale locale, 64 CharSequence category, 65 CharSequence domain) 66 { 67 StringBuilder sb = new StringBuilder (locale.toString()); 68 sb.append('/'); 69 sb.append(category); 70 sb.append('/'); 71 sb.append(domain); 72 sb.append(".po"); 73 74 _pathPO = lookupPath(env, root, sb.toString()); 75 76 sb.setCharAt(sb.length() - 2, 'm'); 77 _pathMO = lookupPath(env, root, sb.toString()); 78 79 init(); 80 } 81 82 private Path lookupPath(Env env, Path root, String relPath) 83 { 84 return root.lookup(relPath); 85 } 86 87 private void init() 88 { 89 if (_pathPO != null && _pathPO.exists()) 90 _currentPath = _pathPO; 91 else if (_pathMO != null && _pathMO.exists()) 92 _currentPath = _pathMO; 93 else 94 return; 95 96 try { 97 GettextParser parser; 98 99 if (_depend == null) 100 _depend = new BasicDependencyContainer(); 101 102 _depend.add(new Depend(_currentPath)); 103 104 if (_currentPath == _pathPO) 105 parser = new POFileParser(_currentPath); 106 else 107 parser = new MOFileParser(_currentPath); 108 109 _pluralExpr = parser.getPluralExpr(); 110 _translations = parser.readTranslations(); 111 112 parser.close(); 113 114 } catch (IOException e) { 115 throw new QuercusModuleException(e.getMessage()); 116 } 117 } 118 119 124 protected UnicodeValue getTranslation(StringValue key) 125 { 126 if (isModified()) 127 init(); 128 129 return getTranslationImpl(key, 0); 130 } 131 132 private boolean isModified() 133 { 134 if (_depend == null) 135 return true; 136 137 return _depend.isModified(); 138 } 139 140 146 protected UnicodeValue getTranslation(StringValue key, int quantity) 147 { 148 if (isModified()) 149 init(); 150 151 if (_pluralExpr != null) 152 return getTranslationImpl(key, _pluralExpr.eval(quantity)); 153 else 154 return null; 155 } 156 157 165 protected UnicodeValue getTranslationImpl(StringValue key, int index) 166 { 167 if (_translations == null) 168 return null; 169 170 ArrayList <UnicodeValue> pluralForms = _translations.get(key); 171 172 if (pluralForms == null || pluralForms.size() == 0) 173 return null; 174 175 if (index < pluralForms.size()) 176 return pluralForms.get(index); 177 else 178 return pluralForms.get(0); 179 } 180 } 181 | Popular Tags |