如何在Android Studio上实现一个按钮来更改语言?

huangapple go评论142阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年8月14日 16:47:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63409491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定