কম্পিউটার টিউটোরিয়াল

অ্যান্ড্রয়েডে ব্যবহারকারীর ভাষা পছন্দ অনুযায়ী অ্যাপের ভাষা পরিবর্তনের পদ্ধতি

রানটাইমে অ্যাপের লোকেল (Locale) পরিবর্তন করা

এই উদাহরণটি দেখায় কীভাবে ব্যবহারকারী Spinner-এ থেকে ভাষা সিলেক্ট করলে অ্যাপের ভাষা ডাইনামিক্যালি পরিবর্তন করা যায়। এটি মাল্টি-ল্যাঙ্গুয়েজ সাপোর্টের জন্য অত্যন্ত গুরুত্বপূর্ণ।

ধাপ ১: নতুন প্রজেক্ট তৈরি করুন

Android Studio-তে File → New Project থেকে প্রজেক্ট তৈরি করুন।

ধাপ ২: স্ট্রিং রিসোর্স ফাইলগুলো তৈরি করুন

res/values/strings.xml (ডিফল্ট - ইংরেজি):

<resources>
    <string name="app_name">Language Demo</string>
    <string name="thank_you">Thank you</string>
    <string name="select_language">Select Language</string>
</resources>

res/values-hi/strings.xml (হিন্দি):

<resources>
    <string name="app_name">Language Demo</string>
    <string name="thank_you">धन्यवाद</string>
    <string name="select_language">भाषा चुनें</string>
</resources>

res/values-fr/strings.xml (ফরাসি):

<resources>
    <string name="app_name">Language Demo</string>
    <string name="thank_you">Je vous remercie</string>
    <string name="select_language">Sélectionner la langue</string>
</resources>

res/values-es/strings.xml (স্পেনিশ):

<resources>
    <string name="app_name">Language Demo</string>
    <string name="thank_you">Gracias</string>
    <string name="select_language">Seleccionar idioma</string>
</resources>

ধাপ ৩: লেআউট XML (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity">
    
    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
    
    <TextView
        android:id="@+id/string"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/spinner"
        android:layout_centerInParent="true"
        android:layout_marginTop="24dp"
        android:text="@string/thank_you"
        android:textSize="24sp"
        android:textStyle="bold" />
</RelativeLayout>

ধাপ ৪: MainActivity.java বাস্তবায়ন

import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {
    Spinner spinner;
    TextView textView;
    String currentLanguage = "en";
    private static final String EXTRA_LANGUAGE = "current_language";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // ইন্টেন্ট থেকে ভাষা পান (রিস্টার্টের পর)
        currentLanguage = getIntent().getStringExtra(EXTRA_LANGUAGE);
        if (currentLanguage == null) currentLanguage = "en";
        
        textView = findViewById(R.id.string);
        spinner = findViewById(R.id.spinner);
        
        // স্পিনার ডেটা
        List languages = new ArrayList<>();
        languages.add(getString(R.string.select_language));
        languages.add("English");
        languages.add("Español");
        languages.add("Français");
        languages.add("हिन्दी");
        
        ArrayAdapter adapter = new ArrayAdapter<>(
            this, android.R.layout.simple_spinner_item, languages);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        
        // বর্তমান ভাষা অনুযায়ী স্পিনার সিলেকশন সেট করুন
        setSpinnerSelection();
        
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView parent, View view, int position, long id) {
                if (position == 0) return; // "Select Language" প্লেসহোল্ডার
                
                String localeCode;
                switch (position) {
                    case 1: localeCode = "en"; break;
                    case 2: localeCode = "es"; break;
                    case 3: localeCode = "fr"; break;
                    case 4: localeCode = "hi"; break;
                    default: return;
                }
                
                if (!localeCode.equals(currentLanguage)) {
                    setLocale(localeCode);
                } else {
                    Toast.makeText(MainActivity.this, "Language already selected!", Toast.LENGTH_SHORT).show();
                }
            }
            
            @Override
            public void onNothingSelected(AdapterView parent) {}
        });
    }

    private void setSpinnerSelection() {
        int position = 0;
        switch (currentLanguage) {
            case "en": position = 1; break;
            case "es": position = 2; break;
            case "fr": position = 3; break;
            case "hi": position = 4; break;
        }
        spinner.setSelection(position);
    }

    private void setLocale(String localeCode) {
        Locale locale = new Locale(localeCode);
        Locale.setDefault(locale);
        
        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
            conf.setLocale(locale);
        } else {
            conf.locale = locale;
        }
        
        res.updateConfiguration(conf, dm);
        
        // অ্যাক্টিভিটি রিস্টার্ট করুন
        Intent refresh = new Intent(this, MainActivity.class);
        refresh.putExtra(EXTRA_LANGUAGE, localeCode);
        refresh.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(refresh);
        finish();
    }

    @Override
    public void onBackPressed() {
        // অ্যাপ সম্পূর্ণ বন্ধ করুন
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
        System.exit(0);
    }
}

গুরুত্বপূর্ণ নোট: Android 7.0+ (API 24+) এ Locale সেটিং

Android 7.0 (Nougat) থেকে `Configuration.setLocale()` ডিপ্রিকেটেড। নতুন API:

// API 24+ এর জন্য
Configuration configuration = new Configuration(resources.getConfiguration());
configuration.setLocale(locale);
configuration.setLayoutDirection(locale);
resources.updateConfiguration(configuration, resources.getDisplayMetrics());

// ContextWrapper ব্যবহার করে অ্যাপ-ওয়াইড লোকেল (সেরা প্র্যাকটিস)
public class LocaleHelper {
    public static Context setLocale(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);
        
        Configuration config = new Configuration(context.getResources().getConfiguration());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            config.setLocale(locale);
        } else {
            config.locale = locale;
        }
        
        return context.createConfigurationContext(config);
    }
}

// Application ক্লাসে.attachBaseContext() ওভাররাইড করুন
@Override
protected void attachBaseContext(Context base) {
    String lang = PreferenceManager.getDefaultSharedPreferences(this).getString("lang", "en");
    super.attachBaseContext(LocaleHelper.setLocale(base, lang));
}

ধাপ ৫: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
            android:configChanges="locale|layoutDirection">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

অ্যাপ্লিকেশন টেস্ট করুন

অ্যাপ রান করুন, Spinner-এ থেকে ভাষা সিলেক্ট করুন। অ্যাপ রিস্টার্ট হবে এবং নির্বাচিত ভাষায় UI টেক্সট প্রদর্শিত হবে। "Thank you" টেক্সট ভাষা অনুযায়ী পরিবর্তন হবে।

  1. অ্যান্ড্রয়েড অ্যাপে সিম্পল টেক্সট ফাইল পড়ার পদ্ধতি

  2. অ্যান্ড্রয়েডে ফ্র্যাগমেন্ট থেকে এক্টিভিটি মেথড কল করা

  3. Android-এ ৫ সেকেন্ড ব্যবহারকারীর নিষ্ক্রিয়তা শনাক্ত করা

  4. অ্যান্ড্রয়েডে অ্যাপ আইকন কাস্টমাইজ করা: সহজ ধাপে ধাপে গাইড