简体   繁体   English

如何在 Android 中显示南印度语言

[英]How to display south Indian languages in Android

I have a requirement to display one text in multiple languages especially Kannada and Telugu I am developing with minimum required API 14 (4.0)我需要以多种语言显示一个文本,尤其是卡纳达语和泰卢固语,我正在使用最低要求的 API 14 (4.0) 进行开发

Thankyou谢谢

Try this...尝试这个...

I shared here the whole application code.我在这里分享了整个应用程序代码。

Project structure项目结构

项目结构

activity_main_activity1.xml 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>

values/strings.xml值/字符串.xml

细绳

值/字符串.xml

values-kn/strings.xml值-kn/strings.xml

卡纳达语

Strings for kannada language.卡纳达语的字符串。

值-kn/strings.xml

values-te/strings.xml值-te/strings.xml

值-te/strings.xml

值-te/strings.xml

Set font设置字体

语言

download fonts here . 在这里下载字体。

MainActivity1.java主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

AndroidManifest.xml

Screen shots:屏幕截图:

1. Default locale(English) 1. 默认语言环境(英文)

默认语言环境

2. Kannada 2. 卡纳达语

卡纳达语

3. Telugu 3. 泰卢固语

泰卢固语

Happy coding.....快乐编码......

you need two things for that :-你需要两件事:-

  1. different values folder in res. res 中的不同值文件夹
  2. Custom typeface font to support your regional language.自定义字体字体以支持您的区域语言。

like as follows :-如下所示:-

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

more detail read it阅读更多细节

Updated method to get available indian locales (2020)更新了获取可用印度语言环境的方法(2020 年)

Use android's inbuilt Locale package to get available locales.使用 android 的内置 Locale 包来获取可用的语言环境。 Then filter the indian languages and finally to display them in their corresponding font, use locale.getDisplayLanguage(locale) as shown below:然后过滤印度语言,最后以相应的字体显示它们,使用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