如何使用多语言支持书法奥利奥 [英] How to use Calligraphy with multi-language support Oreo
问题描述
我正在开发一个需要支持多种语言的应用程序,如果语言是RTL,我必须应用自定义字体。对于我创建了我的类的要求,扩展了应用程序
。一切都很完美,直到我有 Oreo 版本设备(在我启用Marshmellow设备之前)。在奥利奥,如果我们想要更改语言,我们必须创建一个自定义 ContextWrapper
类,这里有问题。
I'm developing an app that need to support multiple languages and if the language is RTL I have to apply a custom font. For the requirement I have created my Class that extends Application
. Everything was perfect till I got Oreo version device (Before I have Marshmellow enabled device). In Oreo if we want to change the language we have to create a custom ContextWrapper
class, here problem come in.
- 要使用
书法
我们需要覆盖
attachBaseContext
方法。 - 要更改语言,我们需要
覆盖
attachBaseContext
- To use
Calligraphy
we need toOverride
theattachBaseContext
method. And - To change language we need to
Override
theattachBaseContext
too
我试图在 Overrided中调用
方法两次一次用于书法,其他用于语言代码,如下所示。 super.attachBaseContext
I tried to call super.attachBaseContext
in the Overrided
method twice One for the Calligraphy and Other for the Language code like below.
@Override
protected void attachBaseContext(Context newBase) {
// create or get your new Locale object here.
String lang = Preferences
.getSharedPreferenceString(appContext, LANGUAGE_KEY, "ar");
Context context = MyContextWrapper.wrap(newBase, lang);
super.attachBaseContext(context);
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
它给出了IllegalStateException,因为我们可以附加一次基本上下文。
It give IllegalStateException as we can attach base context once.
- 如果我使用
super.attachBaseContext(context);
语言更改有效但书法没有。 - 如果我使用
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
书法有效但语言没有改变。
- If I use
super.attachBaseContext(context);
the language change works But Calligraphy does not. - If I use
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
Calligraphy works But Language change does not.
在这种情况下,我怎样才能完成所有工作(书法+多语言)。我已经查看了很多帖子/教程,但我现在已经坚持了三天。
In such case how can I make all working (Calligraphy + Multi-language). I have viewed many posts/tutorials but I'm stuck in this for three days now.
请帮我完成这个。谢谢
编辑参考
- CalligraphyContextWrapper.java
- ContextWrapper 类接受的答案链接。
- CalligraphyContextWrapper.java
- ContextWrapper Class Accepted answer in link.
寻找能够使用书法和更改语言功能的自定义字体的解决方案。或者提供一种方法,以便我可以将语言和应用字体更改为整个应用程序。
Looking for a solution to be able to use custom font with Calligraphy and Change Language Functionality. Alternatively provide a way so that I may be able to change Language and Apply Font to the whole app.
注意:解决方案必须兼容使用API 17到最新的27.我正在使用 AppCompat
。
Note: The solution must be compatible with API 17 to the latest 27. I'm using AppCompat
.
推荐答案
我为oreo做了
在您的活动中:
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(SLocaleHelper.onAttach(newBase)));
}
在您的应用程序主类:
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(SLocaleHelper.onAttach(base, "sv"));
MultiDex.install(this);
}
位置助手类别:
package com.......;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.preference.PreferenceManager;
import java.util.Locale;
public class SLocaleHelper {
private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";
public static Context onAttach(Context context) {
String lang = getPersistedData(context, Locale.getDefault().getLanguage());
return setLocale(context, lang);
}
public static Context onAttach(Context context, String defaultLanguage) {
String lang = getPersistedData(context, defaultLanguage);
return setLocale(context, lang);
}
public static String getLanguage(Context context) {
return getPersistedData(context, Locale.getDefault().getLanguage());
}
public static Context setLocale(Context context, String language) {
persist(context, language);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return updateResources(context, language);
}
return updateResourcesLegacy(context, language);
}
private static String getPersistedData(Context context, String defaultLanguage) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
}
private static void persist(Context context, String language) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(SELECTED_LANGUAGE, language);
editor.apply();
}
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
configuration.setLayoutDirection(locale);
return context.createConfigurationContext(configuration);
}
@SuppressWarnings("deprecation")
private static Context updateResourcesLegacy(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLayoutDirection(locale);
}
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return context;
}
}
这篇关于如何使用多语言支持书法奥利奥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!