generate mapping from actual strings to R.string.id that can be used at runtime to convert/lookup menu titles. Works to uppercase a menu! Changes break dbg and bt builds -- to be fixed.

This commit is contained in:
Eric House 2014-04-03 22:27:50 -07:00
parent f2bfd36780
commit 11dabca721
5 changed files with 92 additions and 5 deletions

View file

@ -98,10 +98,16 @@
<arg value="${THUMBNAIL_ENABLED}" />
</exec>
</target>
<!--
<target name="-pre-compile">
<exec dir="." executable="../scripts/gen_loc_ids.py"
output="src/org/eehouse/android/xw4/loc/LocIDs.java"
failonerror="true"
>
</exec>
</target>
<!--
/* This is typically used for code obfuscation.
Compiled code location: ${out.classes.absolute.dir}
If this is not done in place, override ${out.dex.input.absolute.dir} */

View file

@ -623,7 +623,7 @@ public class GamesListDelegate extends DelegateBase
{
MenuInflater inflater = m_activity.getMenuInflater();
inflater.inflate( R.menu.games_list_menu, menu );
LocUtils.xlateMenu( menu );
LocUtils.xlateMenu( m_activity, menu );
return true;
}

View file

@ -0,0 +1 @@
/LocIDs.java

View file

@ -30,6 +30,9 @@ import org.eehouse.android.xw4.R;
import org.eehouse.android.xw4.DbgUtils;
public class LocUtils {
// Keep this in sync with gen_loc_ids.py and what's used in the menu.xml
// files to mark me-localized strings.
private static final String LOC_PREFIX = "loc:";
public interface LocIface {
void setText( CharSequence text );
@ -51,7 +54,7 @@ public class LocUtils {
}
}
public static void xlateMenu( Menu menu )
public static void xlateMenu( Context context, Menu menu )
{
int count = menu.size();
DbgUtils.logf( "xlateMenu: menu has %d items", count );
@ -59,8 +62,15 @@ public class LocUtils {
MenuItem item = menu.getItem( ii );
String title = item.getTitle().toString();
DbgUtils.logf( "xlateMenu: %d; %s", ii, title );
if ( title.startsWith( "loc:" ) ) {
item.setTitle( title.substring( 4 ) );
if ( title.startsWith( LOC_PREFIX ) ) {
String asKey = title.substring( LOC_PREFIX.length() );
int id = LocIDs.getID( asKey );
if ( LocIDs.NOT_FOUND != id ) {
asKey = context.getString( id ).toUpperCase();
} else {
DbgUtils.logf( "nothing for %s", asKey );
}
item.setTitle( asKey );
}
}
}

View file

@ -0,0 +1,70 @@
#!/usr/bin/python
# Generate a java file with a string->ID mapping for every R.string.id
# in R.java where the string is used in a menu in the form This allows
# "loc:<string>"runtime lookup based on the string, which is the only
# thing it's possible to store in the menu's title (since there's no
# AttributeSet provided with menu inflation.
import glob, sys, re, os
pairs = {}
# Get all string IDs that are used in menus -- the ones we care about
TITLE = re.compile('.*android:title="loc:(.*)".*')
for path in glob.iglob( "res/menu/*.xml" ):
for line in open( path, "r" ):
line.strip()
mtch = TITLE.match(line)
if mtch:
pairs[mtch.group(1)] = True
# Get all string IDs, but only keep those we've seen in menus
# LINE = re.compile('.*public static final int (.*)=(0x.*);.*')
# for line in open("gen/org/eehouse/android/xw4/R.java", "r"):
# line.strip()
# mtch = LINE.match(line)
# if mtch:
# key = mtch.group(1)
# if key in pairs:
# pairs[key] = mtch.group(2)
# beginning of the class file
print """
/***********************************************************************
* Generated file; do not edit!!!
***********************************************************************/
package org.eehouse.android.xw4.loc;
import java.util.HashMap;
import org.eehouse.android.xw4.R;
public class LocIDs {
public static final int NOT_FOUND = -1;
protected static HashMap<String,Integer> s_map;
static {
s_map = new HashMap<String,Integer>();
"""
for key in pairs.keys():
print " s_map.put(\"%s\", R.string.%s);" % (key, key)
# Now the end of the class
print """
}
protected static int getID( String key )
{
int result = NOT_FOUND;
if ( s_map.containsKey( key ) ) {
result = s_map.get( key );
}
return result;
}
}
/* end generated file */
"""