Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีดึงภาษาปัจจุบันที่เลือกในอุปกรณ์ Android

การดึงภาษาปัจจุบัน (Current Locale) ของอุปกรณ์ Android

ตัวอย่างนี้สาธิตวิธีใช้ Locale.getDefault() เพื่อดึงภาษาที่ผู้ใช้ตั้งค่าไว้ในระบบ แล้วนำมาแสดงใน TextView

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio เลือก File → New Project และกรอกข้อมูลที่จำเป็น

ขั้นตอนที่ 2: จัดทำ Layout (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp">
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal" />
</RelativeLayout>

ขั้นตอนที่ 3: เขียนโค้ดหลัก (src/MainActivity.java)

package app.com.sample;

import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        TextView textView = findViewById(R.id.textView);
        String currentLanguage = Locale.getDefault().getDisplayLanguage();
        String currentCountry = Locale.getDefault().getDisplayCountry();
        String languageCode = Locale.getDefault().getLanguage();
        String countryCode = Locale.getDefault().getCountry();
        
        textView.setText("ภาษาปัจจุบัน: " + currentLanguage + "\n"
            + "รหัสภาษา: " + languageCode + "\n"
            + "ประเทศ: " + currentCountry + "\n"
            + "รหัสประเทศ: " + countryCode);
    }
}

ขั้นตอนที่ 4: กำหนดค่าใน 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">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

การทดสอบ

รันแอปพลิเคชัน จะแสดงชื่อภาษา รหัสภาษา ชื่อประเทศ และรหัสประเทศตามการตั้งค่าระบบ

วิธีดึงภาษาปัจจุบันที่เลือกในอุปกรณ์ Android

เมธอดที่มีประโยชน์ของ Locale

เมธอด คืนค่า ตัวอย่าง
getDisplayLanguage() ชื่อภาษาใน Locale ปัจจุบัน "ไทย", "English"
getDisplayLanguage(Locale) ชื่อภาษาใน Locale ที่ระบุ Locale.ENGLISH → "Thai"
getLanguage() รหัสภาษา ISO 639 (lowercase) "th", "en"
getDisplayCountry() ชื่อประเทศใน Locale ปัจจุบัน "ไทยแลนด์", "United States"
getCountry() รหัสประเทศ ISO 3166 (uppercase) "TH", "US"
getDisplayName() ชื่อเต็ม (ภาษา + ประเทศ) "ไทย (ไทยแลนด์)"

การตรวจจับการเปลี่ยนภาษา (Configuration Changes)

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    Locale currentLocale = newConfig.locale;
    // อัปเดต UI ตามภาษาใหม่
}

ต้องเพิ่ม android:configChanges="locale|layoutDirection" ใน Manifest activity

สำหรับการรองรับหลายภาษา (Localization)

สร้างไฟล์ strings.xml ในโฟลเดอร์ values-รหัสภาษา:

  • res/values/strings.xml (ค่าเริ่มต้น - อังกฤษ)
  • res/values-th/strings.xml (ไทย)
  • res/values-zh/strings.xml (จีน)
ระบบจะเลือกไฟล์ที่ตรงกับ Locale ของอุปกรณ์โดยอัตโนมัติ