簡體   English   中英

如何在 Android 中顯示南印度語言

[英]How to display south Indian languages in Android

我需要以多種語言顯示一個文本,尤其是卡納達語和泰盧固語,我正在使用最低要求的 API 14 (4.0) 進行開發

謝謝

嘗試這個...

我在這里分享了整個應用程序代碼。

項目結構

項目結構

activity_main_activity1.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2b579a"
android:orientation="vertical"
tools:context=".MainActivity1" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/kannada"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:gravity="start"
        android:text="@string/kannada"
        android:textColor="#FFFFFF"
        android:textSize="20sp" />

    <Button
        android:id="@+id/telugu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:gravity="end"
        android:text="@string/telugu"
        android:textColor="#FFFFFF"
        android:textSize="20sp" />

    <Button
        android:id="@+id/english"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:gravity="end"
        android:text="@string/english"
        android:textColor="#FFFFFF"
        android:textSize="20sp" />
</LinearLayout>

<TextView
    android:id="@+id/news"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:text="@string/note"
    android:textColor="#FFFFFF"
    android:textSize="20sp" />

 </LinearLayout>

值/字符串.xml

細繩

值/字符串.xml

值-kn/strings.xml

卡納達語

卡納達語的字符串。

值-kn/strings.xml

值-te/strings.xml

值-te/strings.xml

值-te/strings.xml

設置字體

語言

在這里下載字體。

主Activity1.java

package com.hirecraft.stackoverflowtest;

import java.util.Locale;

import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity1 extends Activity {

/**
 * Declaration
 */
Button kannada, telugu, english;
String currentLanguage;
TextView news;
Typeface kannadaFont, teluguFont;

/**
 * This class describes all device configuration information
 * that can impact the resources the application retrieves. This
 * includes both user-specified configuration options (locale
 * and scaling) as well as device configurations (such as input
 * modes, screen size and screen orientation).
 */
Configuration config;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_activity1);

    /**
     * Initialization
     */
    currentLanguage = "";
    kannada = (Button) findViewById(R.id.kannada);
    telugu = (Button) findViewById(R.id.telugu);
    english = (Button) findViewById(R.id.english);

    news = (TextView) findViewById(R.id.news);

    /**
     * Initialize the fonts.
     */
    kannadaFont = Typeface.createFromAsset(getAssets(), "fonts/akshar.ttf");
    teluguFont = Typeface.createFromAsset(getAssets(), "fonts/gautami.ttf");

    /**
     * Event for Kannada
     */
    kannada.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            /**
             * "kn" is the localization code for our Kannada language.
             */
            currentLanguage = "kn";
            Locale locale = new Locale(currentLanguage);
            Locale.setDefault(locale);

            /**
             * Print the current language
             */
            System.out.println("My current language: "
                    + Locale.getDefault());


            config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,
                    getBaseContext().getResources().getDisplayMetrics());

            news.setText(R.string.note);
            news.setTypeface(kannadaFont);
        }
    });


    telugu.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            /**
             * "te" is the localization code for our Telugu language.
             */
            currentLanguage = "te";
            Locale locale = new Locale(currentLanguage);
            Locale.setDefault(locale);

            /**
             * Print the current language
             */
            System.out.println("My current language: "
                    + Locale.getDefault());

            config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,
                    getBaseContext().getResources().getDisplayMetrics());

            news.setText(R.string.note);
            news.setTypeface(teluguFont);
        }
    });

    english.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            /**
             * "en" is the localization code for our default English language.
             */
            currentLanguage = "en";
            Locale locale = new Locale(currentLanguage);
            Locale.setDefault(locale);

            /**
             * Print the current language
             */
            System.out.println("My current language: "
                    + Locale.getDefault());

            config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,
                                               getBaseContext().getResources().getDisplayMetrics());

            news.setText(R.string.note);
        }
    });
}
      }

AndroidManifest.xml

AndroidManifest.xml

屏幕截圖:

1. 默認語言環境(英文)

默認語言環境

2. 卡納達語

卡納達語

3. 泰盧固語

泰盧固語

快樂編碼......

你需要兩件事:-

  1. res 中的不同值文件夾
  2. 自定義字體字體以支持您的區域語言。

如下所示:-

MyProject/
    res/
       values/
           strings.xml
       values-es/
           strings.xml
       values-fr/
           strings.xml

閱讀更多細節

更新了獲取可用印度語言環境的方法(2020 年)

使用 android 的內置 Locale 包來獲取可用的語言環境。 然后過濾印度語言,最后以相應的字體顯示它們,使用locale.getDisplayLanguage(locale)如下所示:

for(locale in Locale.getAvailableLocales())
 if ("IN" in locale.country)
  availableLanguages.add(Pair(locale.language,locale.getDisplayLanguage(locale)))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM