use a single string array for lang code and name

This commit is contained in:
Eric House 2021-11-29 09:26:13 -08:00
parent a738e60f1d
commit bd6bc71603
5 changed files with 53 additions and 110 deletions

View file

@ -48,6 +48,7 @@ public class DictLangCache {
private static final String TAG = DictLangCache.class.getSimpleName(); private static final String TAG = DictLangCache.class.getSimpleName();
private static Map<Integer, String> s_langNames; private static Map<Integer, String> s_langNames;
private static Map<String, Integer> s_langCodes; private static Map<String, Integer> s_langCodes;
private static Map<Integer, String> s_langCodeStrs;
private static int s_adaptedLang = -1; private static int s_adaptedLang = -1;
private static LangsArrayAdapter s_langsAdapter; private static LangsArrayAdapter s_langsAdapter;
@ -152,10 +153,10 @@ public class DictLangCache {
public static String getLangName( Context context, int code ) public static String getLangName( Context context, int code )
{ {
Map<Integer, String> namesArray = getLangNames( context ); makeMaps( context );
String name = namesArray.get( code ); String name = s_langNames.get( code );
if ( null == name ) { if ( null == name ) {
name = namesArray.get( 0 ); name = s_langNames.get( 0 );
} }
return name; return name;
} }
@ -191,7 +192,7 @@ public class DictLangCache {
public static boolean haveDict( Context context, String lang, String name ) public static boolean haveDict( Context context, String lang, String name )
{ {
boolean result = false; boolean result = false;
getLangNames( context ); /* inits s_langCodes */ makeMaps( context );
Integer code = s_langCodes.get( lang ); Integer code = s_langCodes.get( lang );
if ( null != code ) { if ( null != code ) {
result = haveDict( context, code, name ); result = haveDict( context, code, name );
@ -248,7 +249,7 @@ public class DictLangCache {
ArrayList<DictAndLoc> al = new ArrayList<>(); ArrayList<DictAndLoc> al = new ArrayList<>();
DictAndLoc[] dals = DictUtils.dictList( context ); DictAndLoc[] dals = DictUtils.dictList( context );
getLangNames( context ); /* inits s_langNames */ makeMaps( context );
for ( DictAndLoc dal : dals ) { for ( DictAndLoc dal : dals ) {
DictInfo info = getInfo( context, dal ); DictInfo info = getInfo( context, dal );
@ -293,14 +294,10 @@ public class DictLangCache {
return getInfo( context, dal ).langCode; return getInfo( context, dal ).langCode;
} }
private static String[] s_langCodeStrs; public static String getLangCodeStr( Context context, int code )
static String getLangCodeStr( Context context, int code )
{ {
if ( null == s_langCodeStrs ) { makeMaps( context );
s_langCodeStrs = context.getResources() return s_langCodeStrs.get( code );
.getStringArray( R.array.language_codes );
}
return s_langCodeStrs[code];
} }
public static String getDictMD5Sum( Context context, String dict ) public static String getDictMD5Sum( Context context, String dict )
@ -326,7 +323,7 @@ public class DictLangCache {
public static int getLangLangCode( Context context, String lang ) public static int getLangLangCode( Context context, String lang )
{ {
getLangNames( context ); /* inits s_langCodes */ makeMaps( context );
Integer code = s_langCodes.get( lang ); Integer code = s_langCodes.get( lang );
if ( null == code ) { if ( null == code ) {
@ -337,16 +334,16 @@ public class DictLangCache {
public static String userLangForLc( Context context, String lc ) public static String userLangForLc( Context context, String lc )
{ {
makeMaps( context );
String result = null; String result = null;
Map<Integer, String> namesArray = getLangNames( context );
getLangCodeStr( context, 0 ); // force load of s_langCodeStrs for ( Integer code : s_langCodeStrs.keySet() ) {
for ( int code = 0; code < s_langCodeStrs.length; ++code ) { if ( s_langCodeStrs.get(code).equals(lc) ) {
if ( lc.equals(s_langCodeStrs[code]) ) { result = s_langNames.get(code);
result = namesArray.get(code);
break; break;
} }
} }
return result; return result;
} }
@ -464,26 +461,24 @@ public class DictLangCache {
return s_dictsAdapter; return s_dictsAdapter;
} }
private static Map<Integer, String> getLangNames( Context context ) private static void makeMaps( Context context )
{ {
if ( null == s_langNames ) { if ( null == s_langNames ) {
Resources res = context.getResources();
String[] names = res.getStringArray( R.array.language_names );
s_langCodes = new HashMap<>(); s_langCodes = new HashMap<>();
s_langNames = new HashMap<>(); s_langNames = new HashMap<>();
for ( int ii = 0; ii < names.length; ++ii ) { s_langCodeStrs = new HashMap<>();
String name = names[ii];
s_langCodes.put( name, ii );
s_langNames.put( ii, name );
}
// Hex is out-of-order, so can't be in the res-based array. Hard Resources res = context.getResources();
// code it: it's a hack anyway. String[] entries = res.getStringArray( R.array.languages_map );
s_langCodes.put( "Hex", 127 ); for ( int ii = 0; ii < entries.length; ii += 3 ) {
s_langNames.put( 127, "Hex" ); 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 ) public static int getDictCount( Context context, String name )

View file

@ -2676,7 +2676,7 @@ public class GamesListDelegate extends ListDelegateBase
String lc = LocUtils.getCurLangCode( m_activity ); String lc = LocUtils.getCurLangCode( m_activity );
if ( !lc.equals("en") ) { if ( !lc.equals("en") ) {
int code = LocUtils.codeForLangCode( m_activity, lc ); int code = DictLangCache.getLangLangCode( m_activity, lc );
if ( 0 < code ) { if ( 0 < code ) {
String[] names = DictLangCache.getHaveLang( m_activity, code ); String[] names = DictLangCache.getHaveLang( m_activity, code );
if ( 0 == names.length ) { if ( 0 == names.length ) {

View file

@ -62,7 +62,6 @@ public class LookupAlertView extends LinearLayout
private static final int STATE_URLS = 2; private static final int STATE_URLS = 2;
private static final int STATE_LOOKUP = 3; private static final int STATE_LOOKUP = 3;
private static String[] s_langCodes;
private static String[] s_lookupNames; private static String[] s_lookupNames;
private static String[] s_lookupUrls; private static String[] s_lookupUrls;
private static ArrayAdapter<String> s_urlsAdapter; private static ArrayAdapter<String> s_urlsAdapter;
@ -235,7 +234,7 @@ public class LookupAlertView extends LinearLayout
private void lookupWord( Context context, String word, String fmt ) 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 ); String dict_url = String.format( fmt, langCode, word );
Uri uri = Uri.parse( dict_url ); Uri uri = Uri.parse( dict_url );
Intent intent = new Intent( Intent.ACTION_VIEW, uri ); Intent intent = new Intent( Intent.ACTION_VIEW, uri );
@ -250,15 +249,12 @@ public class LookupAlertView extends LinearLayout
private void setLang( Context context, int lang ) private void setLang( Context context, int lang )
{ {
if ( null == s_langCodes ) {
s_langCodes = context.getResources().getStringArray( R.array.language_codes );
}
if ( s_lang != lang ) { if ( s_lang != lang ) {
String[] urls = context.getResources().getStringArray( R.array.lookup_urls ); String[] urls = context.getResources().getStringArray( R.array.lookup_urls );
ArrayList<String> tmpUrls = new ArrayList<>(); ArrayList<String> tmpUrls = new ArrayList<>();
ArrayList<String> tmpNames = 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 ) { for ( int ii = 0; ii < urls.length; ii += 3 ) {
String codes = urls[ii+1]; String codes = urls[ii+1];
if ( 0 == codes.length() || codes.contains( langCode ) ) { if ( 0 == codes.length() || codes.contains( langCode ) ) {

View file

@ -151,28 +151,6 @@ public class LocUtils {
return xlated; 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 ) public static void xlateView( Activity activity )
{ {
xlateView( activity, Utils.getContentView( activity ) ); xlateView( activity, Utils.getContentView( activity ) );

View file

@ -245,55 +245,29 @@
<item>@string/loc_downloads</item> <item>@string/loc_downloads</item>
</string-array> </string-array>
<!-- These two arrays need to be kept in sync --> <string-array name="languages_map">
<string-array name="language_names"> <item>0</item><item>@string/lang_unknown</item><item>--</item>
<item>@string/lang_unknown</item> <!-- Unknown --> <item>1</item><item>@string/lang_name_english</item><item>en</item>
<item>@string/lang_name_english</item> <!-- 1 --> <item>2</item><item>@string/lang_name_french</item><item>fr</item>
<item>@string/lang_name_french</item> <!-- 2 --> <item>3</item><item>@string/lang_name_german</item><item>de</item>
<item>@string/lang_name_german</item> <!-- 3 --> <item>4</item><item>@string/lang_name_turkish</item><item>tr</item>
<item>@string/lang_name_turkish</item> <!-- 4 --> <item>5</item><item>@string/lang_name_arabic</item><item>ar</item>
<item>@string/lang_name_arabic</item> <!-- 5 --> <item>6</item><item>@string/lang_name_spanish</item><item>es</item>
<item>@string/lang_name_spanish</item> <!-- 6 --> <item>7</item><item>@string/lang_name_swedish</item><item>sv</item>
<item>@string/lang_name_swedish</item> <!-- 7 --> <item>8</item><item>@string/lang_name_polish</item><item>pl</item>
<item>@string/lang_name_polish</item> <!-- 8 --> <item>9</item><item>@string/lang_name_danish</item><item>da</item>
<item>@string/lang_name_danish</item> <!-- 9 --> <item>10</item><item>@string/lang_name_italian</item><item>it</item>
<item>@string/lang_name_italian</item> <!-- A --> <item>11</item><item>@string/lang_name_dutch</item><item>nl</item>
<item>@string/lang_name_dutch</item> <!-- B --> <item>12</item><item>@string/lang_name_catalan</item><item>ca</item>
<item>@string/lang_name_catalan</item> <!-- C --> <item>13</item><item>@string/lang_name_portuguese</item><item>pt</item>
<item>@string/lang_name_portuguese</item> <!-- D --> <item>15</item><item>@string/lang_name_russian</item><item>ru</item>
<item></item> <!-- E --> <item>17</item><item>@string/lang_name_czech</item><item>cs</item>
<item>@string/lang_name_russian</item> <!--F--> <item>18</item><item>@string/lang_name_greek</item><item>el</item>
<item></item> <!-- 10 --> <item>19</item><item>@string/lang_name_slovak</item><item>sk</item>
<item>@string/lang_name_czech</item> <!--0x11--> <item>20</item><item>@string/lang_name_hungarian</item><item>hu</item>
<item>@string/lang_name_greek</item> <!--0x12--> <item>21</item><item>@string/lang_name_romanian</item><item>ro</item>
<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="language_codes"> <item>127</item><item>Hex</item><item>--</item>
<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>
</string-array> </string-array>
<!-- Triples of Name, supported codes, and URL format string --> <!-- Triples of Name, supported codes, and URL format string -->