Created
October 24, 2016 13:23
-
-
Save jorgevila/a6548779cf2ed51660d1064611ba6742 to your computer and use it in GitHub Desktop.
i18n context wrapper
This file contains 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 com.pdi.mca.go; | |
import android.content.Context; | |
import android.content.ContextWrapper; | |
import android.content.res.AssetManager; | |
import android.content.res.Configuration; | |
import android.content.res.Resources; | |
import android.support.annotation.NonNull; | |
import android.util.DisplayMetrics; | |
import com.pdi.mca.go.utils.i18n.I18NManager; | |
/** | |
* Custom ContextWrapper to modify certain Context related actions/behaviors. | |
*/ | |
public class PayTVContextWrapper extends ContextWrapper { | |
private PayTVResources mResources; | |
public PayTVContextWrapper(Context baseContext) { | |
super(baseContext); | |
Resources baseRes = baseContext.getResources(); | |
mResources = new PayTVResources(baseRes.getAssets(), baseRes.getDisplayMetrics(), | |
baseRes.getConfiguration()); | |
} | |
@Override | |
public Resources getResources() { | |
return mResources; | |
} | |
private class PayTVResources extends Resources { | |
public PayTVResources(AssetManager assets, DisplayMetrics metrics, Configuration config) { | |
super(assets, metrics, config); | |
} | |
@Override | |
public CharSequence getText(int id) throws NotFoundException { | |
String value = I18NManager.getString(getResources().getResourceEntryName(id)); | |
if (value != null) { | |
return value; | |
} else { | |
return super.getText(id); | |
} | |
} | |
@NonNull | |
@Override | |
public String getString(int id) throws NotFoundException { | |
String value = I18NManager.getString(getResources().getResourceEntryName(id)); | |
if (value != null) { | |
return value; | |
} else { | |
return super.getString(id); | |
} | |
} | |
@Override | |
public CharSequence getText(int id, CharSequence def) { | |
String value = I18NManager.getString(getResources().getResourceEntryName(id)); | |
if (value != null) { | |
return value; | |
} else { | |
return super.getText(id, def); | |
} | |
} | |
@Override | |
public CharSequence[] getTextArray(int id) throws NotFoundException { | |
String value = I18NManager.getString(getResources().getResourceEntryName(id)); | |
if (value != null) { | |
return new String[]{value}; | |
} else { | |
return super.getTextArray(id); | |
} | |
} | |
@Override | |
public String[] getStringArray(int id) throws NotFoundException { | |
String[] value = I18NManager.getStringArray(getResources().getResourceEntryName(id)); | |
if (value != null) { | |
return value; | |
} else { | |
return super.getStringArray(id); | |
} | |
} | |
@Override | |
public int getInteger(int id) throws NotFoundException { | |
int value = I18NManager.getInteger(getResources().getResourceEntryName(id)); | |
if (value != -1) { | |
return value; | |
} else { | |
return super.getInteger(id); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment