send and receive array of locales for translation info, one of which

is the device's current default.  Plan's to include a fake as well if
set, but currently that replaces the default.
This commit is contained in:
Eric House 2014-04-28 07:58:23 -07:00
parent b42c372c79
commit 6c46ce2297
3 changed files with 72 additions and 58 deletions
xwords4/android/XWords4/src/org/eehouse/android/xw4

View file

@ -170,7 +170,7 @@ public class UpdateCheckReceiver extends BroadcastReceiver {
} }
// Xlations update // Xlations update
JSONObject xlationUpdate = LocUtils.makeForXlationUpdate( context ); JSONArray xlationUpdate = LocUtils.makeForXlationUpdate( context );
if ( null != xlationUpdate ) { if ( null != xlationUpdate ) {
try { try {
params.put( k_XLATEINFO, xlationUpdate ); params.put( k_XLATEINFO, xlationUpdate );
@ -184,6 +184,7 @@ public class UpdateCheckReceiver extends BroadcastReceiver {
params.put( k_APPGITREV, BuildConstants.GIT_HASH ); params.put( k_APPGITREV, BuildConstants.GIT_HASH );
params.put( k_NAME, packageName ); params.put( k_NAME, packageName );
params.put( k_AVERS, versionCode ); params.put( k_AVERS, versionCode );
DbgUtils.logf( "current update: %s", params.toString() );
new UpdateQueryTask( context, params, fromUI, pm, new UpdateQueryTask( context, params, fromUI, pm,
packageName, dals ).execute(); packageName, dals ).execute();
} catch ( org.json.JSONException jse ) { } catch ( org.json.JSONException jse ) {
@ -398,8 +399,8 @@ public class UpdateCheckReceiver extends BroadcastReceiver {
// translations info // translations info
if ( jobj.has( k_XLATEINFO ) ) { if ( jobj.has( k_XLATEINFO ) ) {
JSONObject data = jobj.getJSONObject( k_XLATEINFO ); JSONArray data = jobj.getJSONArray( k_XLATEINFO );
int nAdded = LocUtils.addXlation( m_context, data ); int nAdded = LocUtils.addXlations( m_context, data );
if ( 0 < nAdded ) { if ( 0 < nAdded ) {
gotOne = true; gotOne = true;
String msg = LocUtils.getString( m_context, R.string String msg = LocUtils.getString( m_context, R.string

View file

@ -430,7 +430,7 @@ public class XWPrefs {
setPrefsString( context, keyID, TextUtils.join( "\n", value ) ); setPrefsString( context, keyID, TextUtils.join( "\n", value ) );
} }
public static String getLocale( Context context ) public static String getFakeLocale( Context context )
{ {
return getPrefsString( context, R.string.key_xlations_locale ); return getPrefsString( context, R.string.key_xlations_locale );
} }

View file

@ -44,6 +44,7 @@ import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -62,8 +63,6 @@ import org.eehouse.android.xw4.XWPrefs;
public class LocUtils { public class LocUtils {
private static final int FMT_LEN = 4; private static final int FMT_LEN = 4;
private static final String k_LOCALE = "locale"; private static final String k_LOCALE = "locale";
private static final String k_XLATPROTO = "proto";
private static final int XLATE_CUR_VERSION = 1;
private static final String k_XLATEVERS = "xlatevers"; private static final String k_XLATEVERS = "xlatevers";
private static Map<String, String> s_xlationsLocal = null; private static Map<String, String> s_xlationsLocal = null;
@ -242,58 +241,77 @@ public class LocUtils {
public static void saveLocalData( Context context ) public static void saveLocalData( Context context )
{ {
DBUtils.saveXlations( context, s_curLocale, s_xlationsLocal, false ); DBUtils.saveXlations( context, getCurLocale( context ),
s_xlationsLocal, false );
} }
public static JSONObject makeForXlationUpdate( Context context ) public static JSONArray makeForXlationUpdate( Context context )
{ {
JSONObject result = null; String locale = getCurLocale( context );
if ( null != s_curLocale && 0 < s_curLocale.length() ) { String fake = XWPrefs.getFakeLocale( context );
try { JSONArray result = new JSONArray()
String version = DBUtils.getStringFor( context, k_XLATEVERS, "0" ); .put( entryForLocale( context, locale ) );
result = new JSONObject() if ( null != fake && 0 < fake.length() && ! fake.equals(locale) ) {
.put( k_XLATPROTO, XLATE_CUR_VERSION ) result.put( entryForLocale( context, fake ) );
.put( k_LOCALE, s_curLocale )
.put( k_XLATEVERS, version );
} catch ( org.json.JSONException jse ) {
DbgUtils.loge( jse );
}
} }
return result; return result;
} }
private static JSONObject entryForLocale( Context context, String locale )
{
JSONObject result = null;
try {
String version =
DBUtils.getStringFor( context, localeKey(locale), "0" );
result = new JSONObject()
.put( k_LOCALE, locale )
.put( k_XLATEVERS, version );
} catch ( org.json.JSONException jse ) {
DbgUtils.loge( jse );
}
return result;
}
private static String localeKey( String locale )
{
return String.format( "%s:%s", k_XLATEVERS, locale );
}
private static final String k_OLD = "old"; private static final String k_OLD = "old";
private static final String k_NEW = "new"; private static final String k_NEW = "new";
private static final String k_PAIRS = "pairs"; private static final String k_PAIRS = "pairs";
public static int addXlation( Context context, JSONObject data ) public static int addXlations( Context context, JSONArray data )
{ {
int nAdded = 0; int nAdded = 0;
try { try {
int newVersion = data.getInt( k_NEW ); int nLocales = data.length();
JSONArray pairs = data.getJSONArray( k_PAIRS ); for ( int ii = 0; ii < nLocales; ++ii ) {
DbgUtils.logf( "got pairs of len %d, version %d", pairs.length(), JSONObject entry = data.getJSONObject( ii );
newVersion ); String locale = entry.getString( k_LOCALE );
String newVersion = entry.getString( k_NEW );
JSONArray pairs = entry.getJSONArray( k_PAIRS );
DbgUtils.logf( "addXlations: locale %s: got pairs of len %d, version %s", locale,
pairs.length(), newVersion );
int len = pairs.length(); int len = pairs.length();
Map<String,String> newXlations = new HashMap<String,String>( len ); Map<String,String> newXlations = new HashMap<String,String>( len );
for ( int ii = 0; ii < len; ++ii ) { for ( int jj = 0; jj < len; ++jj ) {
JSONObject pair = pairs.getJSONObject( ii ); JSONObject pair = pairs.getJSONObject( jj );
int id = pair.getInt( "id" ); int id = pair.getInt( "id" );
String key = context.getString( id ); String key = context.getString( id );
Assert.assertNotNull( key ); Assert.assertNotNull( key );
String txt = pair.getString( "loc" ); String txt = pair.getString( "loc" );
txt = replaceEscaped( txt ); txt = replaceEscaped( txt );
newXlations.put( key, txt ); newXlations.put( key, txt );
}
DBUtils.saveXlations( context, locale, newXlations, true );
DBUtils.setStringFor( context, localeKey(locale), newVersion );
nAdded += len;
} }
DBUtils.saveXlations( context, s_curLocale, newXlations, true );
DBUtils.setStringFor( context, k_XLATEVERS,
String.format( "%d", newVersion ) );
s_xlationsBlessed = null; s_xlationsBlessed = null;
loadXlations( context ); loadXlations( context );
nAdded = len;
} catch ( org.json.JSONException jse ) { } catch ( org.json.JSONException jse ) {
DbgUtils.loge( jse ); DbgUtils.loge( jse );
} }
@ -354,13 +372,23 @@ public class LocUtils {
} }
} }
private static void loadXlations( Context context ) private static String getCurLocale( Context context )
{ {
if ( null == s_curLocale ) { if ( null == s_curLocale ) {
s_curLocale = XWPrefs.getLocale( context ); String locale = XWPrefs.getFakeLocale( context );
if ( null == locale || 0 == locale.length() ) {
locale = Locale.getDefault().toString();
}
s_curLocale = locale;
} }
return s_curLocale;
}
private static void loadXlations( Context context )
{
if ( null == s_xlationsLocal || null == s_xlationsBlessed ) { if ( null == s_xlationsLocal || null == s_xlationsBlessed ) {
Object[] asObjs = DBUtils.getXlations( context, s_curLocale ); Object[] asObjs = DBUtils.getXlations( context,
getCurLocale( context ) );
s_xlationsLocal = (Map<String,String>)asObjs[0]; s_xlationsLocal = (Map<String,String>)asObjs[0];
s_xlationsBlessed = (Map<String,String>)asObjs[1]; s_xlationsBlessed = (Map<String,String>)asObjs[1];
DbgUtils.logf( "loadXlations: got %d local strings, %d blessed strings", DbgUtils.logf( "loadXlations: got %d local strings, %d blessed strings",
@ -387,16 +415,6 @@ public class LocUtils {
return s_idsToKeys.get( id ); return s_idsToKeys.get( id );
} }
private static boolean isEnabled( Context context )
{
if ( null == s_enabled ) {
s_curLocale = XWPrefs.getLocale( context );
s_enabled = new Boolean( null != s_curLocale &&
0 < s_curLocale.length() );
}
return s_enabled;
}
private static void xlateView( Context context, View view, int depth ) private static void xlateView( Context context, View view, int depth )
{ {
// DbgUtils.logf( "xlateView(depth=%d, view=%s, canRecurse=%b)", depth, // DbgUtils.logf( "xlateView(depth=%d, view=%s, canRecurse=%b)", depth,
@ -408,11 +426,6 @@ public class LocUtils {
} else if ( view instanceof TextView ) { } else if ( view instanceof TextView ) {
TextView tv = (TextView)view; TextView tv = (TextView)view;
tv.setText( xlateString( context, tv.getText().toString() ) ); tv.setText( xlateString( context, tv.getText().toString() ) );
// } else if ( view instanceof CheckBox ) {
// CheckBox box = (CheckBox)view;
// String str = box.getText().toString();
// str = xlateString( context, str );
// box.setText( str );
} else if ( view instanceof Spinner ) { } else if ( view instanceof Spinner ) {
Spinner sp = (Spinner)view; Spinner sp = (Spinner)view;
CharSequence prompt = sp.getPrompt(); CharSequence prompt = sp.getPrompt();