// // ########### FROM SVN REVISION 3895 // /****************************************************************************** * * swlocale.cpp - implementation of Class SWLocale used for retrieval * of locale lookups * * $Id$ * * Copyright 2000-2013 CrossWire Bible Society (http://www.crosswire.org) * CrossWire Bible Society * P. O. Box 2528 * Tempe, AZ 85280-2528 * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation version 2. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * */ package org.crosswire.sword.mgr; import java.util.HashMap; import org.crosswire.sword.keys.*; public class SWLocale { public static String DEFAULT_LOCALE_NAME = "en"; public static int ENDOFABBREVS = -2; String bookLongNames[]; String bookPrefAbbrev[]; HashMap lookupTable = new HashMap(); HashMap mergedAbbrevs = new HashMap(); SWConfig localeSource; String name; String description; String encoding; VersificationMgr.Abbreviation bookAbbrevs[]; public SWLocale() { name = "en_US"; description = "en_US"; bookAbbrevs = CanonBuiltinAbbrevs.builtin_abbrevs; } public SWLocale(String ifilename) throws java.io.IOException { name = null; description = null; encoding = null; bookAbbrevs = null; bookLongNames = null; bookPrefAbbrev = null; if (ifilename != null) { localeSource = new SWConfig(ifilename); } else { localeSource = new SWConfig((String)null); localeSource.setProperty("Meta", "Name", DEFAULT_LOCALE_NAME); localeSource.setProperty("Meta", "Description", "English (US)"); bookAbbrevs = CanonBuiltinAbbrevs.builtin_abbrevs; } try { name = localeSource.getProperty("Meta", "Name"); } catch (Exception e) { /* no name, bad locale file */ } try { description = localeSource.getProperty("Meta", "Description"); } catch (Exception e) { /* no description, maybe bad locale file */ } try { encoding = localeSource.getProperty("Meta", "Encoding"); } catch (Exception e) { /* no encoding; either empty (==Latin1) or UTF-8 */ } } public String translate(String text) { String entry = (String)lookupTable.get(text); if (entry == null) { String confEntry = null; boolean found = false; String textBuf = text; if (textBuf.startsWith("prefAbbr_")) { textBuf = textBuf.substring(textBuf.indexOf("_") + 1); try { confEntry = localeSource.getProperty("Pref Abbrevs", textBuf); found = confEntry != null; } catch (Exception e) {} } if (!found) { try { confEntry = localeSource.getProperty("Text", textBuf); found = confEntry != null; } catch (Exception e) { /* no entry in locale file */} } if (!found) { lookupTable.put(text, textBuf); } else lookupTable.put(text, confEntry); entry = (String)lookupTable.get(text); } return entry; } /** * This function is used to get the name of the languages which this object is handling. * @return The name of the managed language. A possible example is "de". */ public String getName() { return name; } public String getEncoding() { return encoding; } public String getDescription() { return description; } public void augment(SWLocale addFrom) { localeSource.augment(addFrom.localeSource); } public VersificationMgr.Abbreviation[] getBookAbbrevs() { return bookAbbrevs; } /* TODO: Port to Java. Currently only works with builtin_book_abbrevs * public VersificationMgr.Abbreviation[] getBookAbbrevs(int *retSize) { static const char *nullstr = ""; if (!bookAbbrevs) { // Assure all english abbrevs are present for (int j = 0; builtin_abbrevs[j].osis[0]; j++) { p->mergedAbbrevs[builtin_abbrevs[j].ab] = builtin_abbrevs[j].osis; } ConfigEntMap::iterator it = localeSource->getSection("Book Abbrevs").begin(); ConfigEntMap::iterator end = localeSource->getSection("Book Abbrevs").end(); for (; it != end; it++) { p->mergedAbbrevs[it->first.c_str()] = it->second.c_str(); } int size = (int)p->mergedAbbrevs.size(); bookAbbrevs = new struct abbrev[size + 1]; int i = 0; for (LookupMap::iterator it = p->mergedAbbrevs.begin(); it != p->mergedAbbrevs.end(); it++, i++) { bookAbbrevs[i].ab = it->first.c_str(); bookAbbrevs[i].osis = it->second.c_str(); } bookAbbrevs[i].ab = nullstr; bookAbbrevs[i].osis = nullstr; abbrevsCnt = size; } *retSize = abbrevsCnt; return bookAbbrevs; } */ }