(more of prev commit) generate xml layout files from res_src, and run all inflation through LocUtils that tries to substitute strings

This commit is contained in:
Eric House 2014-04-08 06:48:42 -07:00
parent 201e808b4a
commit d27f2d580a
26 changed files with 158 additions and 65 deletions

View file

@ -84,6 +84,10 @@
failonerror="true" output="/dev/null"
/>
<exec dir="." executable="../scripts/mk_xml.py"
failonerror="true" output="/dev/null"
/>
<exec dir="." executable="../scripts/gen_gcmid.sh"
output="src/org/eehouse/android/xw4/GCMConsts.java"
logError="true" failonerror="true"

View file

@ -111,6 +111,7 @@
<string name="key_enable_debug">key_enable_debug</string>
<string name="key_enable_dup_invite">key_enable_dup_invite</string>
<string name="key_download_path">key_download_path</string>
<string name="key_xlations_locale">key_xlations_locale</string>
<!-- Nor is my email address -->
<string name="email_author_email">xwords@eehouse.org</string>
@ -152,6 +153,7 @@
<string name="gamel_menu_storedb">Write DB to SD card</string>
<string name="gamel_menu_loaddb">Load DB from SD card</string>
<string name="enable_dupes_title">Accept duplicate invites</string>
<string name="xlations_locale">Fake locale for translation</string>
<string name="enable_dupes_summary">Accept invitations more than once</string>
<!--string name="dict_url">http://10.0.2.2/~eehouse/and_dicts</string-->

View file

@ -321,6 +321,11 @@
android:defaultValue="false"
/>
<org.eehouse.android.xw4.XWEditTextPreference
android:key="@string/key_xlations_locale"
android:title="@string/xlations_locale"
/>
<!-- For broken devices like my Blaze 4G that report a download
directory that doesn't exist, allow users to set it. Mine:
/sdcard/external_sd/download

View file

@ -39,6 +39,8 @@ import android.os.Handler;
import junit.framework.Assert;
import org.eehouse.android.xw4.loc.LocUtils;
public class BTInviteActivity extends InviteActivity
implements CompoundButton.OnCheckedChangeListener {
@ -158,7 +160,7 @@ public class BTInviteActivity extends InviteActivity
public View getView( final int position, View convertView,
ViewGroup parent ) {
CheckBox box = (CheckBox)
Utils.inflate( BTInviteActivity.this, R.layout.btinviter_item );
LocUtils.inflate( BTInviteActivity.this, R.layout.btinviter_item );
box.setText( m_devs[position] );
box.setOnCheckedChangeListener( BTInviteActivity.this );
return box;

View file

@ -2255,8 +2255,8 @@ public class BoardDelegate extends DelegateBase
m_dlgTitleStr =
LocUtils.getString( m_activity, R.string.msg_ask_password_fmt,
m_pwdName );
m_passwdLyt = (LinearLayout)Utils.inflate( m_activity,
R.layout.passwd_view );
m_passwdLyt = (LinearLayout)LocUtils.inflate( m_activity,
R.layout.passwd_view );
m_passwdEdit = (EditText)m_passwdLyt.findViewById( R.id.edit );
}

View file

@ -26,7 +26,6 @@ import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

View file

@ -26,13 +26,14 @@ import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;
import android.widget.LinearLayout;
import org.eehouse.android.xw4.loc.LocUtils;
public class ChatDelegate extends DelegateBase
implements View.OnClickListener {
@ -56,14 +57,12 @@ public class ChatDelegate extends DelegateBase
if ( null != pairs ) {
LinearLayout layout = (LinearLayout)
m_activity.findViewById( R.id.chat_history );
LayoutInflater factory = LayoutInflater.from( m_activity );
for ( DBUtils.HistoryPair pair : pairs ) {
TextView view = (TextView)factory
.inflate( pair.sourceLocal
? R.layout.chat_history_local
: R.layout.chat_history_remote,
null );
TextView view = (TextView)
LocUtils.inflate( m_activity, pair.sourceLocal
? R.layout.chat_history_local
: R.layout.chat_history_remote );
view.setText( pair.msg );
layout.addView( view );
}

View file

@ -23,6 +23,7 @@ import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import org.eehouse.android.xw4.DlgDelegate.Action;
import org.eehouse.android.xw4.loc.LocUtils;
@ -55,6 +56,13 @@ public class DelegateBase implements DlgDelegate.DlgClickNotify,
return handled;
}
protected void setContentView( int resID )
{
m_activity.setContentView( resID );
View view = Utils.getContentView( m_activity );
LocUtils.xlateView( m_activity, view );
}
protected void showDialog( DlgID dlgID )
{
m_delegate.showDialog( dlgID );

View file

@ -45,6 +45,7 @@ import junit.framework.Assert;
import org.eehouse.android.xw4.DlgDelegate.Action;
import org.eehouse.android.xw4.jni.JNIUtilsImpl;
import org.eehouse.android.xw4.jni.XwJNI;
import org.eehouse.android.xw4.loc.LocUtils;
public class DictBrowseActivity extends XWListActivity
implements View.OnClickListener, OnItemSelectedListener {
@ -103,9 +104,9 @@ public class DictBrowseActivity extends XWListActivity
public Object getItem( int position )
{
TextView text =
(TextView)Utils.inflate( DictBrowseActivity.this,
android.R.layout.simple_list_item_1 );
TextView text = (TextView)
LocUtils.inflate( DictBrowseActivity.this,
android.R.layout.simple_list_item_1 );
String str = XwJNI.dict_iter_nthWord( m_dictClosure, position );
if ( null != str ) {
text.setText( str );

View file

@ -35,7 +35,6 @@ import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

View file

@ -35,7 +35,6 @@ import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
@ -61,6 +60,7 @@ import org.eehouse.android.xw4.jni.XwJNI;
import org.eehouse.android.xw4.jni.JNIUtilsImpl;
import org.eehouse.android.xw4.jni.GameSummary;
import org.eehouse.android.xw4.DictUtils.DictLoc;
import org.eehouse.android.xw4.loc.LocUtils;
public class DictsDelegate extends DelegateBase
implements View.OnClickListener, AdapterView.OnItemLongClickListener,
@ -86,8 +86,6 @@ public class DictsDelegate extends DelegateBase
private boolean m_launchedForMissing = false;
private LayoutInflater m_factory;
private class DictListAdapter implements ExpandableListAdapter {
private Context m_context;
private XWListItem[][] m_cache;
@ -188,9 +186,8 @@ public class DictsDelegate extends DelegateBase
public View getGroupView( int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent )
{
View row =
Utils.inflate( m_activity,
android.R.layout.simple_expandable_list_item_1 );
View row = LocUtils.inflate( m_activity, android.R.layout
.simple_expandable_list_item_1 );
TextView view = (TextView)row.findViewById( android.R.id.text1 );
view.setText( m_langs[groupPosition] );
return view;
@ -383,8 +380,6 @@ public class DictsDelegate extends DelegateBase
Resources res = m_activity.getResources();
m_locNames = res.getStringArray( R.array.loc_names );
m_factory = LayoutInflater.from( m_activity );
m_downloadStr = m_activity.getString( R.string.download_dicts );
m_activity.setContentView( R.layout.dict_browse );

View file

@ -37,6 +37,8 @@ import java.util.Iterator;
import junit.framework.Assert;
import org.eehouse.android.xw4.loc.LocUtils;
public class DlgDelegate {
public static enum Action {
@ -401,7 +403,7 @@ public class DlgDelegate {
private Dialog createAboutDialog()
{
final View view = Utils.inflate( m_activity, R.layout.about_dlg );
final View view = LocUtils.inflate( m_activity, R.layout.about_dlg );
TextView vers = (TextView)view.findViewById( R.id.version_string );
vers.setText( String.format( m_activity.getString(R.string.about_vers_fmt),
m_activity.getString(R.string.app_version),

View file

@ -38,7 +38,6 @@ import android.widget.Button;
import android.app.Dialog;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
@ -58,6 +57,7 @@ import org.eehouse.android.xw4.DlgDelegate.Action;
import org.eehouse.android.xw4.jni.*;
import org.eehouse.android.xw4.jni.CurGameInfo.DeviceRole;
import org.eehouse.android.xw4.jni.CommsAddrRec.CommsConnType;
import org.eehouse.android.xw4.loc.LocUtils;
public class GameConfig extends XWActivity
implements View.OnClickListener
@ -153,15 +153,14 @@ public class GameConfig extends XWActivity
{
Dialog dialog = super.onCreateDialog( id );
if ( null == dialog ) {
LayoutInflater factory;
DialogInterface.OnClickListener dlpos;
AlertDialog.Builder ab;
final DlgID dlgID = DlgID.values()[id];
switch (dlgID) {
case PLAYER_EDIT:
View playerEditView
= Utils.inflate( this, R.layout.player_edit );
View playerEditView =
LocUtils.inflate( this, R.layout.player_edit );
dialog = new AlertDialog.Builder( this )
.setTitle(R.string.player_edit_title)
@ -205,7 +204,7 @@ public class GameConfig extends XWActivity
};
dialog = new AlertDialog.Builder( this )
.setTitle( R.string.force_title )
.setView( Utils.inflate( this, layoutForDlg(dlgID) ) )
.setView( LocUtils.inflate( this, layoutForDlg(dlgID) ) )
.setPositiveButton( R.string.button_ok, dlpos )
.create();
DialogInterface.OnDismissListener dismiss =
@ -686,7 +685,6 @@ public class GameConfig extends XWActivity
// only enable delete if one will remain (or two if networked)
boolean canDelete = names.length > 2
|| (localOnlyGame() && names.length > 1);
LayoutInflater factory = LayoutInflater.from(this);
View.OnClickListener lstnr = new View.OnClickListener() {
@Override
public void onClick( View view ) {
@ -710,7 +708,7 @@ public class GameConfig extends XWActivity
view.setOnClickListener( lstnr );
m_playerLayout.addView( view );
View divider = factory.inflate( R.layout.divider_view, null );
View divider = LocUtils.inflate( this, R.layout.divider_view );
m_playerLayout.addView( divider );
}

View file

@ -29,6 +29,7 @@ import android.widget.LinearLayout;
import android.widget.TextView;
import org.eehouse.android.xw4.DBUtils.GameGroupInfo;
import org.eehouse.android.xw4.loc.LocUtils;
public class GameListGroup extends ExpiringLinearLayout
implements SelectableItem.LongClickHandler,
@ -59,8 +60,8 @@ public class GameListGroup extends ExpiringLinearLayout
SelectableItem cb,
GroupStateListener gcb )
{
GameListGroup result =
(GameListGroup)Utils.inflate( context, R.layout.game_list_group );
GameListGroup result = (GameListGroup)
LocUtils.inflate( context, R.layout.game_list_group );
result.m_cb = cb;
result.m_gcb = gcb;
result.m_groupPosition = groupPosition;

View file

@ -266,7 +266,7 @@ public class GameListItem extends LinearLayout
boolean[] isLocal = new boolean[1];
for ( int ii = 0; ii < summary.nPlayers; ++ii ) {
ExpiringLinearLayout tmp = (ExpiringLinearLayout)
Utils.inflate( m_context, R.layout.player_list_elem );
LocUtils.inflate( m_context, R.layout.player_list_elem );
tview = (TextView)tmp.findViewById( R.id.item_name );
tview.setText( summary.summarizePlayer( ii ) );
tview = (TextView)tmp.findViewById( R.id.item_score );
@ -377,7 +377,7 @@ public class GameListItem extends LinearLayout
SelectableItem cb )
{
GameListItem result =
(GameListItem)Utils.inflate( context, R.layout.game_list_item );
(GameListItem)LocUtils.inflate( context, R.layout.game_list_item );
result.init( handler, rowid, groupPosition, fieldID, cb );
return result;
}

View file

@ -310,7 +310,7 @@ public class GamesListDelegate extends DelegateBase
case GET_NAME:
layout =
(LinearLayout)Utils.inflate( m_activity, R.layout.dflt_name );
(LinearLayout)LocUtils.inflate( m_activity, R.layout.dflt_name );
final EditText etext =
(EditText)layout.findViewById( R.id.name_edit );
etext.setText( CommonPrefs.getDefaultPlayerName( m_activity,
@ -357,7 +357,7 @@ public class GamesListDelegate extends DelegateBase
m_selGroupIDs = new HashSet<Long>();
getBundledData( savedInstanceState );
m_activity.setContentView( R.layout.game_list );
setContentView( R.layout.game_list );
ListView listview = m_activity.getListView();
DBUtils.setDBChangeListener( this );
@ -1159,7 +1159,8 @@ public class GamesListDelegate extends DelegateBase
DialogInterface.OnClickListener lstnr,
DlgID dlgID )
{
m_namer = (GameNamer)Utils.inflate( m_activity, R.layout.rename_game );
m_namer = (GameNamer)
LocUtils.inflate( m_activity, R.layout.rename_game );
m_namer.setName( curname );
m_namer.setLabel( labelID );
Dialog dialog = new AlertDialog.Builder( m_activity )

View file

@ -48,6 +48,8 @@ import java.util.ArrayList;
import junit.framework.Assert;
import org.eehouse.android.xw4.loc.LocUtils;
public class LookupAlert extends LinearLayout
implements View.OnClickListener, Dialog.OnKeyListener,
AdapterView.OnItemClickListener {
@ -353,7 +355,8 @@ public class LookupAlert extends LinearLayout
public static Dialog createDialog( Activity parent, Bundle bundle )
{
LookupAlert view = (LookupAlert)Utils.inflate( parent, R.layout.lookup );
LookupAlert view = (LookupAlert)
LocUtils.inflate( parent, R.layout.lookup );
view.init( parent, bundle );
Dialog result = new AlertDialog.Builder( parent )

View file

@ -42,7 +42,7 @@ import org.eehouse.android.xw4.jni.CurGameInfo;
import org.eehouse.android.xw4.jni.CommonPrefs;
import org.eehouse.android.xw4.jni.CommsAddrRec;
import org.eehouse.android.xw4.jni.XwJNI;
import org.eehouse.android.xw4.loc.LocUtils;
public class NewGameActivity extends XWActivity {
@ -77,6 +77,7 @@ public class NewGameActivity extends XWActivity {
m_groupID = getIntent().getLongExtra( GROUPID_EXTRA, -1 );
setContentView( R.layout.new_game );
LocUtils.xlateView( this, Utils.getContentView( this ) );
TextView desc = (TextView)findViewById( R.id.newgame_local_desc );
m_dict = CommonPrefs.getDefaultHumanDict( this );
@ -213,7 +214,7 @@ public class NewGameActivity extends XWActivity {
switch( dlgID ) {
case NAME_GAME:
final GameNamer namerView =
(GameNamer)Utils.inflate( this, R.layout.rename_game );
(GameNamer)LocUtils.inflate( this, R.layout.rename_game );
namerView.setLabel( m_nameForBT ? R.string.btname_label
: R.string.smsname_label );
namerView.setName( m_gameName );

View file

@ -30,6 +30,8 @@ import android.util.AttributeSet;
import android.view.View;
import android.widget.Spinner;
import org.eehouse.android.xw4.loc.LocUtils;
public class SMSCheckBoxPreference extends CheckBoxPreference {
private Context m_context;
@ -72,7 +74,7 @@ public class SMSCheckBoxPreference extends CheckBoxPreference {
public static Dialog onCreateDialog( final Activity activity, final int id )
{
final View layout = Utils.inflate( activity, R.layout.confirm_sms );
final View layout = LocUtils.inflate( activity, R.layout.confirm_sms );
DialogInterface.OnClickListener lstnr =
new DialogInterface.OnClickListener() {

View file

@ -41,6 +41,7 @@ import java.util.Comparator;
import java.util.Iterator;
import org.eehouse.android.xw4.DlgDelegate.Action;
import org.eehouse.android.xw4.loc.LocUtils;
public class SMSInviteActivity extends InviteActivity {
@ -124,7 +125,7 @@ public class SMSInviteActivity extends InviteActivity {
switch( dlgID ) {
case GET_NUMBER:
final GameNamer namerView =
(GameNamer)Utils.inflate( this, R.layout.rename_game );
(GameNamer)LocUtils.inflate( this, R.layout.rename_game );
namerView.setLabel( R.string.get_sms_number );
namerView.setKeyListener(DialerKeyListener.getInstance());
lstnr = new DialogInterface.OnClickListener() {
@ -383,8 +384,8 @@ public class SMSInviteActivity extends InviteActivity {
// for by-index access.)
SMSListItem item =
(SMSListItem)Utils.inflate( SMSInviteActivity.this,
R.layout.smsinviter_item );
(SMSListItem)LocUtils.inflate( SMSInviteActivity.this,
R.layout.smsinviter_item );
item.setChecked( m_phoneRecs.get(position).m_checked );
CompoundButton.OnCheckedChangeListener lstnr =

View file

@ -38,7 +38,6 @@ import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract.PhoneLookup;
import android.telephony.TelephonyManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
@ -253,12 +252,6 @@ public class Utils {
return name;
}
public static View inflate( Context context, int layoutId )
{
LayoutInflater factory = LayoutInflater.from( context );
return factory.inflate( layoutId, null );
}
public static void setChecked( Activity activity, int id, boolean value )
{
CheckBox cbx = (CheckBox)activity.findViewById( id );

View file

@ -31,6 +31,8 @@ import android.widget.CheckBox;
import junit.framework.Assert;
import org.eehouse.android.xw4.loc.LocUtils;
public class XWListItem extends LinearLayout
implements SelectableItem.LongClickHandler, View.OnClickListener {
private int m_position;
@ -162,7 +164,8 @@ public class XWListItem extends LinearLayout
public static XWListItem inflate( Context context, SelectableItem selCB )
{
XWListItem item = (XWListItem)Utils.inflate( context, R.layout.list_item );
XWListItem item = (XWListItem)
LocUtils.inflate( context, R.layout.list_item );
item.setSelCB( selCB );
return item;
}

View file

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

View file

@ -92,7 +92,7 @@ public class LocListItem extends LinearLayout implements OnFocusChangeListener {
int position )
{
LocListItem result =
(LocListItem)Utils.inflate( context, R.layout.loc_list_item );
(LocListItem)LocUtils.inflate( context, R.layout.loc_list_item );
result.m_pair = pair;
result.m_position = position;

View file

@ -23,9 +23,14 @@ import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import java.util.Iterator;
import java.util.HashMap;
@ -36,6 +41,7 @@ import junit.framework.Assert;
import org.eehouse.android.xw4.R;
import org.eehouse.android.xw4.DbgUtils;
import org.eehouse.android.xw4.DBUtils;
import org.eehouse.android.xw4.XWPrefs;
public class LocUtils {
// Keep this in sync with gen_loc_ids.py and what's used in the menu.xml
@ -43,6 +49,7 @@ public class LocUtils {
private static final String LOC_PREFIX = "loc:";
private static HashMap<String, String>s_xlations = null;
private static HashMap<Integer, String> s_idsToKeys = null;
private static Boolean s_enabled = null;
public interface LocIface {
void setText( CharSequence text );
@ -64,6 +71,37 @@ public class LocUtils {
}
}
public static View inflate( Context context, int resID )
{
LayoutInflater factory = LayoutInflater.from( context );
View view = factory.inflate( resID, null );
xlateView( context, view );
return view;
}
public static void xlateView( Context context, View view )
{
if ( view instanceof ViewGroup ) {
ViewGroup asGroup = (ViewGroup)view;
int count = asGroup.getChildCount();
for ( int ii = 0; ii < count; ++ii ) {
View child = asGroup.getChildAt( ii );
xlateView( context, child );
}
} else if ( view instanceof Button ) {
Button button = (Button)view;
String str = button.getText().toString();
str = xlateString( context, str );
button.setText( str );
} else if ( view instanceof TextView ) {
TextView tv = (TextView)view;
String str = tv.getText().toString();
str = xlateString( context, str );
tv.setText( str );
} else {
}
}
public static void xlateMenu( Activity activity, Menu menu )
{
xlateMenu( activity, menu, 0 );
@ -217,4 +255,13 @@ public class LocUtils {
return s_idsToKeys.get( id );
}
private static boolean isEnabled( Context context )
{
if ( null == s_enabled ) {
String locale = XWPrefs.getLocale( context );
s_enabled = new Boolean( null != locale && 0 < locale.length() );
}
return s_enabled;
}
}

View file

@ -3,28 +3,50 @@
import glob, sys, re, os
from lxml import etree
pairs = {}
# sys.exit(0)
g_xmlTypes = [
{ 'elemName': 'item',
'attrType' : '{http://schemas.android.com/apk/res/android}title'
},
{ 'elemName': 'Button',
'attrType' : '{http://schemas.android.com/apk/res/android}text'
},
{ 'elemName': 'TextView',
'attrType' : '{http://schemas.android.com/apk/res/android}text'
},
]
g_pairs = {}
STR_REF = re.compile('@string/(.*)$')
def xform(src, dest):
print "looking at file", src
doc = etree.parse(src)
for item in doc.findall('item'):
title = item.get('{http://schemas.android.com/apk/res/android}title')
if title:
match = STR_REF.match(title)
if match:
key = match.group(1)
if key in pairs:
print key, "loc:" + key
item.set('{http://schemas.android.com/apk/res/android}title', "loc:" + key)
root = doc.getroot();
for child in root.iter():
for elem in g_xmlTypes:
if child.tag == elem['elemName']:
value = child.get(elem['attrType'])
match = value and STR_REF.match(value)
if match:
key = match.group(1)
if key in g_pairs:
print child.tag, key, "loc:" + key
child.set(elem['attrType'], "loc:" + key)
# create directory if needed, then write file
dir = os.path.dirname( dest )
if not os.path.exists(dir): os.makedirs(dir)
doc.write( dest, pretty_print=True )
# Gather all localizable strings
for path in glob.iglob( "res/values/strings.xml" ):
for action, elem in etree.iterparse(path):
if "end" == action and 'string' == elem.tag:
pairs[elem.get('name')] = True
g_pairs[elem.get('name')] = True
for subdir, dirs, files in os.walk('res_src'):
for file in files: