new type of problem: make a string array in ListPreference localizable

This commit is contained in:
Eric House 2014-04-04 08:00:30 -07:00
parent 589b740af6
commit 073c461693
3 changed files with 43 additions and 8 deletions

View file

@ -287,10 +287,10 @@
</string-array>
<string-array name="game_summary_values">
<item>@string/game_summary_field_empty</item>
<item>@string/game_summary_field_language</item>
<item>@string/game_summary_field_opponents</item>
<item>@string/game_summary_field_state</item>
<item>loc:game_summary_field_empty</item>
<item>loc:game_summary_field_language</item>
<item>loc:game_summary_field_opponents</item>
<item>loc:game_summary_field_state</item>
</string-array>
<string-array name="confirm_sms_reasons">

View file

@ -23,6 +23,11 @@ package org.eehouse.android.xw4;
import android.content.Context;
import android.util.AttributeSet;
import junit.framework.Assert;
import org.eehouse.android.xw4.loc.LocUtils;
public class XWSumListPreference extends XWListPreference {
private static final int[] s_ADDROWS = {
@ -37,25 +42,32 @@ public class XWSumListPreference extends XWListPreference {
}
// Why I exist: insert the rowid and gameid lines if debug is on
@Override
protected void onAttachedToActivity()
{
super.onAttachedToActivity();
CharSequence[] entries = getEntries();
CharSequence[] newEntries = LocUtils.xlateStrings( m_context, entries );
if ( ! newEntries.equals( entries ) ) {
setEntries( newEntries );
setEntryValues( newEntries );
}
if ( BuildConfig.DEBUG || XWPrefs.getDebugEnabled( m_context ) ) {
CharSequence[] entries = getEntries();
entries = getEntries();
CharSequence lastRow = entries[entries.length - 1];
boolean done = false;
String[] addRows = new String[s_ADDROWS.length];
for ( int ii = 0; !done && ii < s_ADDROWS.length; ++ii ) {
String addRow = m_context.getString( s_ADDROWS[ii] );
String addRow = LocUtils.getString( m_context, s_ADDROWS[ii] );
done = lastRow.equals( addRow );
addRows[ii] = addRow;
}
if ( !done ) {
CharSequence[] newEntries =
new CharSequence[entries.length + addRows.length];
newEntries = new CharSequence[entries.length + addRows.length];
System.arraycopy( entries, 0, newEntries, 0,
entries.length );
System.arraycopy( addRows, 0, newEntries, entries.length,

View file

@ -82,6 +82,29 @@ public class LocUtils {
}
}
public static String xlateString( Context context, String str )
{
if ( str.startsWith( LOC_PREFIX ) ) {
str = str.substring( LOC_PREFIX.length() );
int id = LocIDs.getID( str );
if ( LocIDs.NOT_FOUND != id ) {
str = getString( context, id );
} else {
DbgUtils.logf( "nothing for %s", str );
}
}
return str;
}
public static CharSequence[] xlateStrings( Context context, CharSequence[] strs )
{
CharSequence[] result = new CharSequence[strs.length];
for ( int ii = 0; ii < strs.length; ++ii ) {
result[ii] = xlateString( context, strs[ii].toString() );
}
return result;
}
public static String getString( Context context, int id )
{
String str = context.getString( id );