Android 在我的应用程序中支持多种语言

Android 在我的应用程序中支持多种语言,android,localization,Android,Localization,我想为我的申请提供英语、印地语、乌尔都语的支持。 为此,我创建了两个values strings文件夹,如values hi,values ur,然后是default strings.xml,用于英语。当我单击Urdu时,我需要为所有标签加载Urdu strings文件,并使UI RTL支持。我使用某种ContextWrapper解决了类似的问题,以在任何活动中强制使用该语言 ContextWrapper代码为: public class ContextWrapper extends andro

我想为我的申请提供英语、印地语、乌尔都语的支持。

为此,我创建了两个values strings文件夹,如values hi,values ur,然后是default strings.xml,用于英语。当我单击Urdu时,我需要为所有标签加载Urdu strings文件,并使UI RTL支持。

我使用某种ContextWrapper解决了类似的问题,以在任何活动中强制使用该语言

ContextWrapper代码为:

public class ContextWrapper extends android.content.ContextWrapper {

public ContextWrapper(Context base) {
    super(base);
}

@SuppressWarnings("deprecation")
public static ContextWrapper wrap(Context context, Locale newLocale) {

    Resources res = context.getResources();
    Configuration configuration = res.getConfiguration();

    if (BuildUtils.isAtLeast24Api()) {
        configuration.setLocale(newLocale);

        LocaleList localeList = new LocaleList(newLocale);
        LocaleList.setDefault(localeList);
        configuration.setLocales(localeList);

        context = context.createConfigurationContext(configuration);

    } else if (BuildUtils.isAtLeast17Api()) {
        configuration.setLocale(newLocale);
        context = context.createConfigurationContext(configuration);

    } else {
        configuration.locale = newLocale;
        res.updateConfiguration(configuration, res.getDisplayMetrics());
    }

    return new ContextWrapper(context);
}
}

您必须在每个要强制使用该语言的活动中继承attachBaseContext生命周期回调,例如

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(ContextWrapper.wrap(base, Locale.ENGLISH));
}

显然,如果你想在当前活动中更改语言,你必须重新加载它。

问题是什么?如何加载乌尔都语的字符串文件?并将本地语言更新为乌尔都语会自动完成。你只需要在设备设置中切换到乌尔都语,如果目的是加载乌尔都语,而手机的其他部分则不是,那么它就变得不那么简单了。在加载资源时,可以使用一些技巧。要将手机更改为乌尔都语,请转到您的设置、语言选项并选择乌尔都语作为默认语言。如果没有,请将固件更改为具有。如果你不能做到这一点,请尝试谷歌play的一些语言更改应用程序,如果它们可以在你的手机上使用(它们与较旧的android手机一起使用,用于测试类似的内容)。我不会手动更改设备设置,我想从我的项目上单击按钮