简体   繁体   English

如何在 Android Studio 上实现更改语言的按钮?

[英]How can I implement a button to change language on Android Studio?

I'm developing a multi-linguistic app, because it's oriented to a very diverse public.我正在开发一个多语言应用程序,因为它面向非常多样化的公众。 I tried to make it through a method I found googling, but while it seems to have some sense (sorry in advance, I'm not a native speaker nor a real programmer), I didn't get it to work.我试图通过一种我在谷歌上搜索到的方法来实现它,但虽然它似乎有一些意义(提前抱歉,我不是母语人士也不是真正的程序员),但我没有让它工作。 The method is方法是

package com.example.donafelicidad;

import android.content.res.Resources;
import android.content.res.Configuration;

import java.util.Locale;


public class LanguageHelper {

    public static void changeLocale(Resources res, String locale) {

        Configuration config;
        config = new Configuration(res.getConfiguration());

        switch (locale){
            case "es":
                config.locale = new Locale("es");
                break;
            case "qu":
                config.locale = new Locale("qu");
                break;
           }
        res.updateConfiguration(config, res.getDisplayMetrics());
    }
}

I'm applying this method with the buttons, referenced as CHNG_QU, and CHNG_ES:我将这个方法与按钮一起应用,引用为 CHNG_QU 和 CHNG_ES:

CHNG_QU.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LanguageHelper.changeLocale(getApplicationContext().getResources(), "qu");
                Intent intent = new Intent(MainActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });

        CHNG_ES.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LanguageHelper.changeLocale(getApplicationContext().getResources(), "es");
                Intent intent = new Intent(MainActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });

I changed manually the locale settings, and tried it.我手动更改了语言环境设置,并尝试了它。 It changed perfectly to the other language, so I guess that the Strings are well done.它完美地更改为另一种语言,所以我猜字符串做得很好。 Any feedback about how to handle this would be greatly appreciated, even if it's necessary to re-do the entire code.任何有关如何处理此问题的反馈将不胜感激,即使有必要重新执行整个代码。 Please use simple terms for a unexperienced guy who is just doing his best.请使用简单的术语来形容一个正在尽力而为的没有经验的人。

As a side note, I didn't use other threads of questions because most of what I found was deprecated.作为旁注,我没有使用其他问题线索,因为我发现的大部分内容已被弃用。

Lingver is a library made to force your app to use the locale resource files you want. Lingver是一个库,旨在强制您的应用程序使用您想要的语言环境资源文件。

In the documentation, I found this blogpost that implements it while explaining every step, I hope you find it useful.在文档中,我发现这篇博文在解释每一步的同时实现了它,希望对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM