| Language.java |
1 /**
2 * Distribution License:
3 * JSword is free software; you can redistribute it and/or modify it under
4 * the terms of the GNU Lesser General Public License, version 2.1 as published by
5 * the Free Software Foundation. This program is distributed in the hope
6 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8 * See the GNU Lesser General Public License for more details.
9 *
10 * The License is available on the internet at:
11 * http://www.gnu.org/copyleft/lgpl.html
12 * or by writing to:
13 * Free Software Foundation, Inc.
14 * 59 Temple Place - Suite 330
15 * Boston, MA 02111-1307, USA
16 *
17 * Copyright: 2007
18 * The copyright to this program is held by it's authors.
19 *
20 * ID: $Id: Languages.java 1462 2007-07-02 02:32:23Z dmsmith $
21 */
22 package org.crosswire.common.util;
23
24 /**
25 * A single language, paring an ISO-639 code to a localized representation of
26 * the language.
27 *
28 * @see gnu.lgpl.License for license details.<br>
29 * The copyright to this program is held by it's authors.
30 * @author DM Smith [dmsmith555 at yahoo dot com]
31 */
32 public class Language implements Comparable<Language> {
33 public static final Language DEFAULT_LANG = new Language(null);
34
35 /**
36 * A single language defined by an ISO-639 code. If the code is null or
37 * empty then it is considered to be DEFAULT_LANG_CODE (that is, English).
38 *
39 * @param iso639Code
40 * the particular language
41 */
42 public Language(String iso639Code) {
43 this.code = Languages.getLanguageCode(iso639Code);
44 }
45
46 /**
47 * Determine whether this language is valid. The code is valid if it is in
48 * iso639.properties.
49 *
50 * @return true if the language is valid.
51 */
52 public boolean isValidLanguage() {
53 return Languages.isValidLanguage(code);
54 }
55
56 /**
57 * Get the language code.
58 *
59 * @return the code for the language
60 */
61 public String getCode() {
62 return code;
63 }
64
65 /**
66 * Get the language name.
67 *
68 * @return the name of the language
69 */
70 public String getName() {
71 if (name == null) {
72 name = Languages.getLanguageName(code);
73 }
74 return name;
75 }
76
77 /**
78 * Determine whether this language is a Left-to-Right or a Right-to-Left
79 * language. Note: This is problematic. Languages do not have direction.
80 * Scripts do. Further, there are over 7000 living languages, many of which
81 * are written in Right-to-Left scripts and are not listed here.
82 *
83 * @return true if the language is Left-to-Right.
84 */
85 public boolean isLeftToRight() {
86 if (!knowsDirection) {
87 // TODO(DMS): Improve this.
88 ltor = !("he".equals(code) || // Hebrew
89 "ar".equals(code) || // Arabic
90 "fa".equals(code) || // Farsi/Persian
91 "ur".equals(code) || // Uighur
92 "uig".equals(code) || // Uighur, too
93 "syr".equals(code) || // Syriac
94 "iw".equals(code)); // Java's notion of Hebrew
95
96 knowsDirection = true;
97 }
98
99 return ltor;
100 }
101
102 /*
103 * (non-Javadoc)
104 *
105 * @see java.lang.Object#hashCode()
106 */
107 @Override
108 public int hashCode() {
109 return code.hashCode();
110 }
111
112 /*
113 * (non-Javadoc)
114 *
115 * @see java.lang.Object#equals(java.lang.Object)
116 */
117 @Override
118 public boolean equals(Object obj) {
119 if (this == obj) {
120 return true;
121 }
122
123 if (obj == null || getClass() != obj.getClass()) {
124 return false;
125 }
126
127 final Language other = (Language) obj;
128
129 return code.equals(other.code);
130 }
131
132 /*
133 * (non-Javadoc)
134 *
135 * @see java.lang.Object#toString()
136 */
137 @Override
138 public String toString() {
139 return getName();
140 }
141
142 /*
143 * (non-Javadoc)
144 *
145 * @see java.lang.Comparable#compareTo(java.lang.Object)
146 */
147 public int compareTo(Language o) {
148 return getName().compareTo(o.toString());
149 }
150
151 private String code;
152 private String name;
153 private boolean knowsDirection;
154 private boolean ltor;
155 }
156