when a string has format specifiers, highlight them, and offer a

newbie tip explaining the translation must match.
This commit is contained in:
Eric House 2014-05-22 23:50:44 -07:00
parent a6d8a74102
commit 9d23aef31d
6 changed files with 648 additions and 603 deletions

File diff suppressed because it is too large Load diff

View file

@ -111,6 +111,7 @@
<string name="key_na_browseall">key_na_browseall</string>
<string name="key_na_values">key_na_values</string>
<string name="key_na_studycopy">key_na_studycopy</string>
<string name="key_na_fmt_expl">key_na_fmt_expl</string>
<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>

View file

@ -2248,5 +2248,9 @@
<string name="loc_fmts_mismatch">Illegal translation: a translated
string must have the same format specifiers (e.g. %1$s) as the
original.</string>
<string name="not_again_fmt_expl">This string has special format
specifiers (e.g. %1$s). Please be sure that your translation has
the same ones as the original.\n\n(You will not be able to save it
unless it does.)</string>
</resources>

View file

@ -1925,4 +1925,8 @@
<string name="loc_fmts_mismatch">Lagelli noitalsnart: a detalsnart
gnirts tsum evah eht emas tamrof sreificeps e(.g. %1$s) sa eht
lanigiro.</string>
<string name="not_again_fmt_expl">Siht gnirts sah laiceps tamrof
sreificeps e(.g. %1$s). Esaelp eb erus taht ruoy noitalsnart sah
eht emas seno sa eht lanigiro.\n\nuOy( lliw ton eb elba ot evas ti
sselnu ti seod.)</string>
</resources>

View file

@ -1925,4 +1925,8 @@
<string name="loc_fmts_mismatch">ILLEGAL TRANSLATION: A TRANSLATED
STRING MUST HAVE THE SAME FORMAT SPECIFIERS (E.G. %1$s) AS THE
ORIGINAL.</string>
<string name="not_again_fmt_expl">THIS STRING HAS SPECIAL FORMAT
SPECIFIERS (E.G. %1$s). PLEASE BE SURE THAT YOUR TRANSLATION HAS
THE SAME ONES AS THE ORIGINAL.\n\n(YOU WILL NOT BE ABLE TO SAVE IT
UNLESS IT DOES.)</string>
</resources>

View file

@ -22,9 +22,13 @@ package org.eehouse.android.xw4.loc;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextWatcher;
import android.text.style.BackgroundColorSpan;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
@ -46,7 +50,7 @@ public class LocItemEditDelegate extends DelegateBase implements TextWatcher {
private HashSet<String> m_keyFmts;
private EditText m_edit;
private boolean m_haveBlessed;
private static Pattern s_patFormat = Pattern.compile(LocUtils.RES_FORMAT);
private static Pattern s_patFormat = Pattern.compile( LocUtils.RES_FORMAT );
protected LocItemEditDelegate( Activity activity, Bundle savedInstanceState )
{
@ -62,7 +66,12 @@ public class LocItemEditDelegate extends DelegateBase implements TextWatcher {
m_key = key;
TextView view = (TextView)findViewById( R.id.english_view );
view.setText( key );
m_keyFmts = getFmtSet( key, view );
if ( 0 < m_keyFmts.size() ) {
showNotAgainDlg( R.string.not_again_fmt_expl,
R.string.key_na_fmt_expl );
}
view = (TextView)findViewById( R.id.xlated_view_blessed );
String blessed = LocUtils.getBlessedXlation( m_activity, key, true );
m_haveBlessed = null != blessed && 0 < blessed.length();
@ -148,11 +157,7 @@ public class LocItemEditDelegate extends DelegateBase implements TextWatcher {
boolean ok = true;
CharSequence cs = m_edit.getText();
if ( null != cs && 0 < cs.length() ) {
if ( null == m_keyFmts ) {
m_keyFmts = getFmtSet( m_key );
}
ok = m_keyFmts.equals( getFmtSet( cs.toString() ) );
ok = m_keyFmts.equals( getFmtSet( cs.toString(), null ) );
if ( !ok ) {
showOKOnlyDialog( R.string.loc_fmts_mismatch );
@ -161,13 +166,38 @@ public class LocItemEditDelegate extends DelegateBase implements TextWatcher {
return ok;
}
private HashSet<String> getFmtSet( String txt )
// Performance hack: set up highlighting of format specifiers since we're
// searching for them anyway.
private HashSet<String> getFmtSet( String txt, TextView owner )
{
HashSet<String> fmts = new HashSet<String>();
Spannable spanText = null; // null unless used
Matcher matcher = s_patFormat.matcher( txt );
while ( matcher.find() ) {
fmts.add( txt.substring( matcher.start(), matcher.end() ) );
int start = matcher.start();
int end = matcher.end();
fmts.add( txt.substring( start, end ) );
if ( null == owner ) {
continue;
}
if ( null == spanText ) {
spanText = new SpannableString( txt );
}
spanText.setSpan( new BackgroundColorSpan(Color.BLUE), start, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
}
if ( null != owner ) {
if ( null != spanText ) {
owner.setText( spanText );
} else {
owner.setText( txt );
}
}
return fmts;
}