android - 如何以应用程序的默认语言设置 google api 对话框的文本

标签 android google-api locale google-places-api google-signin

我已将应用程序的默认语言设置为印地语(即系统默认语言以外的语言)并使用谷歌的 API(PlaceAutocomplete、GoogleSignIn)。

两者都以英语(系统默认语言)显示文本,而不是印地语或任何应用程序的默认语言。

有什么办法,我可以强制他们以应用程序的默认语言加载,或者我可以传递我的默认应用程序的语言。

谢谢。这真的很有帮助。寻找任何更新。

最佳答案

你不能设置系统默认语言。如果你想提供多种语言。那么从Locale你可以设置语言。默认情况下它是英语,如果你改变了然后让它像这样。请检查以下代码。

SharedPreferences 中选择语言并保存语言值。每当启动应用程序后,检查语言首选项的值,然后选择它们。

确保每个字符串都以这种形式添加到值文件夹中。

对于默认的英语,然后将所有字符串放在 values 中。对于印地语,您需要设置 values-hi。对于其他语言,将字符串与国家代码放在一起也会发生同样的事情。国家代码来自 here .

 SharedPreferences sharedPreferences = getSharedPreferences(Common.MYPREFERENCE_LANGUAGE, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            if (parent.getSelectedItem().equals("English")) {
                Utils.updateLanguage(activity, "en");
                editor.putString("language", "en");

            } else {
                Utils.updateLanguage(activity, "hi");
                editor.putString("language", "hi");
            }
            editor.apply();

public static void updateLanguage(Context context,String lang) {

    String mlanguage = getlanguage(lang);
    PurplkiteLogs.logError("", " language update " + mlanguage);
    Locale locale = null;
    Configuration config;

    try {
        if (mlanguage.equals("en")) {
            locale = Locale.ENGLISH;

        } else if (mlanguage.equals("hi")) {
             locale = setLocale(context,"hi");

        } else {
            locale = new Locale(mlanguage);
        }

        Locale.setDefault(locale);
        config = new Configuration();
        config.locale = locale;
        context.getResources().updateConfiguration(config,
                context.getResources().getDisplayMetrics());
    } catch (Exception e) {

    } finally {
        mlanguage = null;
        config = null;
        locale = null;
        context = null;
    }
}

private static String getlanguage(String lang) {
    String mlang = null;
    if (lang != null) {
        if (lang.trim().equalsIgnoreCase("hi")) {
            mlang = "hi";
        } else {
            mlang = "en";
        }
    }
    return mlang;
}

public static Locale setLocale(Context context ,String lang ) {
    Locale myLocale = new Locale(lang);
    Resources res = context.getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
    return conf.locale;
}

谢谢,希望这能帮助您解决问题并理清概念。

关于android - 如何以应用程序的默认语言设置 google api 对话框的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39449629/

相关文章:

android - onPause()中的AsyncTask怎么办?

python - 需要帮助格式化 Google API 的日期时间时区

java - 使用 JAX RS 遵循 HTTP 请求中请求的区域设置

playframework - 在play框架2.x中,如何添加全局 Action /过滤器?

url - 如何在GWT应用程序上获取语言环境信息

android - 如何构建一个不受打瞌睡影响的 radio 应用程序?

Android/如何最好地定义主题/值

android - Card ScrollView 中的底部滚动条

python - Gmail API 是否支持使用 OAuth 服务帐户?

.net - 如何使用 ASP.net Google 分析 api 版本 3(Google.Apis.Analytics.v3.dll) 发送 Google 分析报告查询?