mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-20 22:26:54 +01:00
use a single string array for lang code and name
This commit is contained in:
parent
a738e60f1d
commit
bd6bc71603
5 changed files with 53 additions and 110 deletions
|
@ -48,6 +48,7 @@ public class DictLangCache {
|
|||
private static final String TAG = DictLangCache.class.getSimpleName();
|
||||
private static Map<Integer, String> s_langNames;
|
||||
private static Map<String, Integer> s_langCodes;
|
||||
private static Map<Integer, String> s_langCodeStrs;
|
||||
|
||||
private static int s_adaptedLang = -1;
|
||||
private static LangsArrayAdapter s_langsAdapter;
|
||||
|
@ -152,10 +153,10 @@ public class DictLangCache {
|
|||
|
||||
public static String getLangName( Context context, int code )
|
||||
{
|
||||
Map<Integer, String> namesArray = getLangNames( context );
|
||||
String name = namesArray.get( code );
|
||||
makeMaps( context );
|
||||
String name = s_langNames.get( code );
|
||||
if ( null == name ) {
|
||||
name = namesArray.get( 0 );
|
||||
name = s_langNames.get( 0 );
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
@ -191,7 +192,7 @@ public class DictLangCache {
|
|||
public static boolean haveDict( Context context, String lang, String name )
|
||||
{
|
||||
boolean result = false;
|
||||
getLangNames( context ); /* inits s_langCodes */
|
||||
makeMaps( context );
|
||||
Integer code = s_langCodes.get( lang );
|
||||
if ( null != code ) {
|
||||
result = haveDict( context, code, name );
|
||||
|
@ -248,7 +249,7 @@ public class DictLangCache {
|
|||
ArrayList<DictAndLoc> al = new ArrayList<>();
|
||||
DictAndLoc[] dals = DictUtils.dictList( context );
|
||||
|
||||
getLangNames( context ); /* inits s_langNames */
|
||||
makeMaps( context );
|
||||
|
||||
for ( DictAndLoc dal : dals ) {
|
||||
DictInfo info = getInfo( context, dal );
|
||||
|
@ -293,14 +294,10 @@ public class DictLangCache {
|
|||
return getInfo( context, dal ).langCode;
|
||||
}
|
||||
|
||||
private static String[] s_langCodeStrs;
|
||||
static String getLangCodeStr( Context context, int code )
|
||||
public static String getLangCodeStr( Context context, int code )
|
||||
{
|
||||
if ( null == s_langCodeStrs ) {
|
||||
s_langCodeStrs = context.getResources()
|
||||
.getStringArray( R.array.language_codes );
|
||||
}
|
||||
return s_langCodeStrs[code];
|
||||
makeMaps( context );
|
||||
return s_langCodeStrs.get( code );
|
||||
}
|
||||
|
||||
public static String getDictMD5Sum( Context context, String dict )
|
||||
|
@ -326,7 +323,7 @@ public class DictLangCache {
|
|||
|
||||
public static int getLangLangCode( Context context, String lang )
|
||||
{
|
||||
getLangNames( context ); /* inits s_langCodes */
|
||||
makeMaps( context );
|
||||
|
||||
Integer code = s_langCodes.get( lang );
|
||||
if ( null == code ) {
|
||||
|
@ -337,16 +334,16 @@ public class DictLangCache {
|
|||
|
||||
public static String userLangForLc( Context context, String lc )
|
||||
{
|
||||
makeMaps( context );
|
||||
String result = null;
|
||||
Map<Integer, String> namesArray = getLangNames( context );
|
||||
|
||||
getLangCodeStr( context, 0 ); // force load of s_langCodeStrs
|
||||
for ( int code = 0; code < s_langCodeStrs.length; ++code ) {
|
||||
if ( lc.equals(s_langCodeStrs[code]) ) {
|
||||
result = namesArray.get(code);
|
||||
for ( Integer code : s_langCodeStrs.keySet() ) {
|
||||
if ( s_langCodeStrs.get(code).equals(lc) ) {
|
||||
result = s_langNames.get(code);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -464,26 +461,24 @@ public class DictLangCache {
|
|||
return s_dictsAdapter;
|
||||
}
|
||||
|
||||
private static Map<Integer, String> getLangNames( Context context )
|
||||
private static void makeMaps( Context context )
|
||||
{
|
||||
if ( null == s_langNames ) {
|
||||
Resources res = context.getResources();
|
||||
String[] names = res.getStringArray( R.array.language_names );
|
||||
|
||||
s_langCodes = new HashMap<>();
|
||||
s_langNames = new HashMap<>();
|
||||
for ( int ii = 0; ii < names.length; ++ii ) {
|
||||
String name = names[ii];
|
||||
s_langCodes.put( name, ii );
|
||||
s_langNames.put( ii, name );
|
||||
}
|
||||
s_langCodeStrs = new HashMap<>();
|
||||
|
||||
// Hex is out-of-order, so can't be in the res-based array. Hard
|
||||
// code it: it's a hack anyway.
|
||||
s_langCodes.put( "Hex", 127 );
|
||||
s_langNames.put( 127, "Hex" );
|
||||
Resources res = context.getResources();
|
||||
String[] entries = res.getStringArray( R.array.languages_map );
|
||||
for ( int ii = 0; ii < entries.length; ii += 3 ) {
|
||||
Integer code = Integer.parseInt(entries[ii]);
|
||||
String name = entries[ii+1];
|
||||
String lc = entries[ii+2];
|
||||
s_langCodes.put( name, code );
|
||||
s_langNames.put( code, name );
|
||||
s_langCodeStrs.put( code, lc );
|
||||
}
|
||||
}
|
||||
return s_langNames;
|
||||
}
|
||||
|
||||
public static int getDictCount( Context context, String name )
|
||||
|
|
|
@ -2676,7 +2676,7 @@ public class GamesListDelegate extends ListDelegateBase
|
|||
|
||||
String lc = LocUtils.getCurLangCode( m_activity );
|
||||
if ( !lc.equals("en") ) {
|
||||
int code = LocUtils.codeForLangCode( m_activity, lc );
|
||||
int code = DictLangCache.getLangLangCode( m_activity, lc );
|
||||
if ( 0 < code ) {
|
||||
String[] names = DictLangCache.getHaveLang( m_activity, code );
|
||||
if ( 0 == names.length ) {
|
||||
|
|
|
@ -62,7 +62,6 @@ public class LookupAlertView extends LinearLayout
|
|||
private static final int STATE_URLS = 2;
|
||||
private static final int STATE_LOOKUP = 3;
|
||||
|
||||
private static String[] s_langCodes;
|
||||
private static String[] s_lookupNames;
|
||||
private static String[] s_lookupUrls;
|
||||
private static ArrayAdapter<String> s_urlsAdapter;
|
||||
|
@ -235,7 +234,7 @@ public class LookupAlertView extends LinearLayout
|
|||
|
||||
private void lookupWord( Context context, String word, String fmt )
|
||||
{
|
||||
String langCode = s_langCodes[s_lang];
|
||||
String langCode = DictLangCache.getLangCodeStr( context, s_lang );
|
||||
String dict_url = String.format( fmt, langCode, word );
|
||||
Uri uri = Uri.parse( dict_url );
|
||||
Intent intent = new Intent( Intent.ACTION_VIEW, uri );
|
||||
|
@ -250,15 +249,12 @@ public class LookupAlertView extends LinearLayout
|
|||
|
||||
private void setLang( Context context, int lang )
|
||||
{
|
||||
if ( null == s_langCodes ) {
|
||||
s_langCodes = context.getResources().getStringArray( R.array.language_codes );
|
||||
}
|
||||
|
||||
if ( s_lang != lang ) {
|
||||
String[] urls = context.getResources().getStringArray( R.array.lookup_urls );
|
||||
ArrayList<String> tmpUrls = new ArrayList<>();
|
||||
ArrayList<String> tmpNames = new ArrayList<>();
|
||||
String langCode = String.format( ":%s:", s_langCodes[lang] );
|
||||
String langCode = String
|
||||
.format( ":%s:", DictLangCache.getLangCodeStr( context, lang ) );
|
||||
for ( int ii = 0; ii < urls.length; ii += 3 ) {
|
||||
String codes = urls[ii+1];
|
||||
if ( 0 == codes.length() || codes.contains( langCode ) ) {
|
||||
|
|
|
@ -151,28 +151,6 @@ public class LocUtils {
|
|||
return xlated;
|
||||
}
|
||||
|
||||
private static Map<String, Integer> s_langCodeMap = null;
|
||||
public static int codeForLangCode( Context context, String lc )
|
||||
{
|
||||
int result = 0;
|
||||
if ( null == s_langCodeMap ) {
|
||||
s_langCodeMap = new HashMap<>();
|
||||
String[] langCodes =
|
||||
context.getResources().getStringArray( R.array.language_codes );
|
||||
for ( int ii = 0; ii < langCodes.length; ++ii ) {
|
||||
String item = langCodes[ii];
|
||||
if ( 0 < item.length() ) {
|
||||
s_langCodeMap.put( item, ii );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( s_langCodeMap.containsKey( lc ) ) {
|
||||
result = s_langCodeMap.get( lc );
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void xlateView( Activity activity )
|
||||
{
|
||||
xlateView( activity, Utils.getContentView( activity ) );
|
||||
|
|
|
@ -245,55 +245,29 @@
|
|||
<item>@string/loc_downloads</item>
|
||||
</string-array>
|
||||
|
||||
<!-- These two arrays need to be kept in sync -->
|
||||
<string-array name="language_names">
|
||||
<item>@string/lang_unknown</item> <!-- Unknown -->
|
||||
<item>@string/lang_name_english</item> <!-- 1 -->
|
||||
<item>@string/lang_name_french</item> <!-- 2 -->
|
||||
<item>@string/lang_name_german</item> <!-- 3 -->
|
||||
<item>@string/lang_name_turkish</item> <!-- 4 -->
|
||||
<item>@string/lang_name_arabic</item> <!-- 5 -->
|
||||
<item>@string/lang_name_spanish</item> <!-- 6 -->
|
||||
<item>@string/lang_name_swedish</item> <!-- 7 -->
|
||||
<item>@string/lang_name_polish</item> <!-- 8 -->
|
||||
<item>@string/lang_name_danish</item> <!-- 9 -->
|
||||
<item>@string/lang_name_italian</item> <!-- A -->
|
||||
<item>@string/lang_name_dutch</item> <!-- B -->
|
||||
<item>@string/lang_name_catalan</item> <!-- C -->
|
||||
<item>@string/lang_name_portuguese</item> <!-- D -->
|
||||
<item></item> <!-- E -->
|
||||
<item>@string/lang_name_russian</item> <!--F-->
|
||||
<item></item> <!-- 10 -->
|
||||
<item>@string/lang_name_czech</item> <!--0x11-->
|
||||
<item>@string/lang_name_greek</item> <!--0x12-->
|
||||
<item>@string/lang_name_slovak</item> <!--0x13-->
|
||||
<item>@string/lang_name_hungarian</item> <!--0x14-->
|
||||
<item>@string/lang_name_romanian</item> <!--0x15-->
|
||||
</string-array>
|
||||
<string-array name="languages_map">
|
||||
<item>0</item><item>@string/lang_unknown</item><item>--</item>
|
||||
<item>1</item><item>@string/lang_name_english</item><item>en</item>
|
||||
<item>2</item><item>@string/lang_name_french</item><item>fr</item>
|
||||
<item>3</item><item>@string/lang_name_german</item><item>de</item>
|
||||
<item>4</item><item>@string/lang_name_turkish</item><item>tr</item>
|
||||
<item>5</item><item>@string/lang_name_arabic</item><item>ar</item>
|
||||
<item>6</item><item>@string/lang_name_spanish</item><item>es</item>
|
||||
<item>7</item><item>@string/lang_name_swedish</item><item>sv</item>
|
||||
<item>8</item><item>@string/lang_name_polish</item><item>pl</item>
|
||||
<item>9</item><item>@string/lang_name_danish</item><item>da</item>
|
||||
<item>10</item><item>@string/lang_name_italian</item><item>it</item>
|
||||
<item>11</item><item>@string/lang_name_dutch</item><item>nl</item>
|
||||
<item>12</item><item>@string/lang_name_catalan</item><item>ca</item>
|
||||
<item>13</item><item>@string/lang_name_portuguese</item><item>pt</item>
|
||||
<item>15</item><item>@string/lang_name_russian</item><item>ru</item>
|
||||
<item>17</item><item>@string/lang_name_czech</item><item>cs</item>
|
||||
<item>18</item><item>@string/lang_name_greek</item><item>el</item>
|
||||
<item>19</item><item>@string/lang_name_slovak</item><item>sk</item>
|
||||
<item>20</item><item>@string/lang_name_hungarian</item><item>hu</item>
|
||||
<item>21</item><item>@string/lang_name_romanian</item><item>ro</item>
|
||||
|
||||
<string-array name="language_codes">
|
||||
<item></item>
|
||||
<item>en</item>
|
||||
<item>fr</item>
|
||||
<item>de</item>
|
||||
<item>tr</item>
|
||||
<item>ar</item>
|
||||
<item>es</item>
|
||||
<item>sv</item>
|
||||
<item>pl</item>
|
||||
<item>da</item>
|
||||
<item>it</item>
|
||||
<item>nl</item>
|
||||
<item>ca</item>
|
||||
<item>pt</item>
|
||||
<item></item>
|
||||
<item>ru</item>
|
||||
<item></item>
|
||||
<item>cs</item>
|
||||
<item>el</item>
|
||||
<item>sk</item>
|
||||
<item>hu</item>
|
||||
<item>ro</item>
|
||||
<item>127</item><item>Hex</item><item>--</item>
|
||||
</string-array>
|
||||
|
||||
<!-- Triples of Name, supported codes, and URL format string -->
|
||||
|
|
Loading…
Reference in a new issue