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

View file

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

View file

@ -430,7 +430,7 @@ public class XWPrefs {
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 );
}

View file

@ -44,6 +44,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -62,8 +63,6 @@ import org.eehouse.android.xw4.XWPrefs;
public class LocUtils {
private static final int FMT_LEN = 4;
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 Map<String, String> s_xlationsLocal = null;
@ -242,58 +241,77 @@ public class LocUtils {
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;
if ( null != s_curLocale && 0 < s_curLocale.length() ) {
try {
String version = DBUtils.getStringFor( context, k_XLATEVERS, "0" );
result = new JSONObject()
.put( k_XLATPROTO, XLATE_CUR_VERSION )
.put( k_LOCALE, s_curLocale )
.put( k_XLATEVERS, version );
} catch ( org.json.JSONException jse ) {
DbgUtils.loge( jse );
}
String locale = getCurLocale( context );
String fake = XWPrefs.getFakeLocale( context );
JSONArray result = new JSONArray()
.put( entryForLocale( context, locale ) );
if ( null != fake && 0 < fake.length() && ! fake.equals(locale) ) {
result.put( entryForLocale( context, fake ) );
}
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_NEW = "new";
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;
try {
int newVersion = data.getInt( k_NEW );
JSONArray pairs = data.getJSONArray( k_PAIRS );
DbgUtils.logf( "got pairs of len %d, version %d", pairs.length(),
newVersion );
int nLocales = data.length();
for ( int ii = 0; ii < nLocales; ++ii ) {
JSONObject entry = data.getJSONObject( ii );
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();
Map<String,String> newXlations = new HashMap<String,String>( len );
for ( int ii = 0; ii < len; ++ii ) {
JSONObject pair = pairs.getJSONObject( ii );
int id = pair.getInt( "id" );
String key = context.getString( id );
Assert.assertNotNull( key );
String txt = pair.getString( "loc" );
txt = replaceEscaped( txt );
newXlations.put( key, txt );
int len = pairs.length();
Map<String,String> newXlations = new HashMap<String,String>( len );
for ( int jj = 0; jj < len; ++jj ) {
JSONObject pair = pairs.getJSONObject( jj );
int id = pair.getInt( "id" );
String key = context.getString( id );
Assert.assertNotNull( key );
String txt = pair.getString( "loc" );
txt = replaceEscaped( 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;
loadXlations( context );
nAdded = len;
} catch ( org.json.JSONException 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 ) {
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 ) {
Object[] asObjs = DBUtils.getXlations( context, s_curLocale );
Object[] asObjs = DBUtils.getXlations( context,
getCurLocale( context ) );
s_xlationsLocal = (Map<String,String>)asObjs[0];
s_xlationsBlessed = (Map<String,String>)asObjs[1];
DbgUtils.logf( "loadXlations: got %d local strings, %d blessed strings",
@ -387,16 +415,6 @@ public class LocUtils {
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 )
{
// DbgUtils.logf( "xlateView(depth=%d, view=%s, canRecurse=%b)", depth,
@ -408,11 +426,6 @@ public class LocUtils {
} else if ( view instanceof TextView ) {
TextView tv = (TextView)view;
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 ) {
Spinner sp = (Spinner)view;
CharSequence prompt = sp.getPrompt();