英文:
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.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 an inexperienced 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.
英文:
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.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.
答案1
得分: 2
Lingver 是一个用来强制您的应用使用您想要的语言环境资源文件的库。
在文档中,我找到了这篇博文,它实现了这一功能并解释了每一步,希望您会找到它有用。
英文:
Lingver is a library made to force your app to use the locale resource files you want.
In the documentation, I found this blogpost that implements it while explaining every step, I hope you find it useful.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论