Created
October 13, 2017 06:00
-
-
Save joaocsousa/1f9e1f70c932f017fd63a611bcf1fdd5 to your computer and use it in GitHub Desktop.
LayoutInflater.Factory sample with text translations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package joaocsousa.github.com.myapplication; | |
import android.content.Context; | |
import android.os.Bundle; | |
import android.support.annotation.Nullable; | |
import android.support.annotation.StringRes; | |
import android.support.v4.view.LayoutInflaterCompat; | |
import android.support.v7.app.AppCompatActivity; | |
import android.util.AttributeSet; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.widget.TextView; | |
import java.util.HashMap; | |
import java.util.Locale; | |
import java.util.Map; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
LayoutInflaterCompat.setFactory2(getLayoutInflater(), new CustomLayoutInflater(this, new StringIdExtractor(), new Translations())); | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
public static class CustomLayoutInflater implements LayoutInflater.Factory2 { | |
private final AppCompatActivity appCompatActivity; | |
private final StringIdExtractor stringIdExtractor; | |
private final Translations translations; | |
public CustomLayoutInflater(AppCompatActivity appCompatActivity, StringIdExtractor stringIdExtractor, Translations translations) { | |
this.appCompatActivity = appCompatActivity; | |
this.stringIdExtractor = stringIdExtractor; | |
this.translations = translations; | |
} | |
@Override | |
public View onCreateView(View parent, String name, Context context, AttributeSet attributeSet) { | |
View view = appCompatActivity.getDelegate().createView(parent, name, context, attributeSet); | |
if (view == null) { | |
view = appCompatActivity.onCreateView(parent, name, context, attributeSet); | |
} | |
if (view == null) { | |
view = appCompatActivity.onCreateView(name, context, attributeSet); | |
} | |
if (view != null && view instanceof TextView) { | |
int stringId = stringIdExtractor.getStringId(attributeSet); | |
String translation = translations.getTranslationFor(stringId); | |
((TextView) view).setText(translation); | |
} | |
return view; | |
} | |
@Override | |
public View onCreateView(String name, Context context, AttributeSet attributeSet) { | |
return null; | |
} | |
} | |
public static class StringIdExtractor { | |
public static final int NOT_FOUND = Integer.MIN_VALUE; | |
private static final String NAMESPACE = "http://schemas.android.com/apk/res/android"; | |
private static final String TAG_TEXT = "text"; | |
@StringRes | |
public int getStringId(@Nullable AttributeSet attributeSet) { | |
if (attributeSet == null) { | |
return NOT_FOUND; | |
} | |
String stringId = attributeSet.getAttributeValue(NAMESPACE, TAG_TEXT); | |
if (stringId != null) { | |
stringId = stringId.replace("@", ""); | |
try { | |
return Integer.valueOf(stringId); | |
} catch (NumberFormatException e) { | |
return NOT_FOUND; | |
} | |
} | |
return NOT_FOUND; | |
} | |
} | |
public static class Translations { | |
private static final Map<Integer, String> enTranslations = new HashMap<>(); | |
private static final Map<Integer, String> frTranslations = new HashMap<>(); | |
private static final Map<Integer, String> DEFAULT_TRANSLATIONS = enTranslations; | |
static { | |
enTranslations.put(R.string.whatever, "Whatever (English)"); | |
frTranslations.put(R.string.whatever, "Whatever (French)"); | |
enTranslations.put(R.string.hello, "Hello (English)"); | |
frTranslations.put(R.string.hello, "Hello (French)"); | |
} | |
public String getTranslationFor(@StringRes int resId) { | |
if (Locale.getDefault().getISO3Language().equals(Locale.FRANCE.getISO3Language())) { | |
return frTranslations.get(resId); | |
} else if (Locale.getDefault().getISO3Language().equals(Locale.UK.getISO3Language())) { | |
return enTranslations.get(resId); | |
} else { | |
return DEFAULT_TRANSLATIONS.get(resId); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment