🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / Android RMOS / Android Çoklu Dil Destek Yapısı Multi Language

1-) Android RMOS - Android Çoklu Dil Destek Yapısı Multi Language

 

bayraklar : https://github.com/madebybowtie/FlagKit/blob/master/Assets/Flags.md

 

 

 

 

 

LocaleHelper.java

 

package com.rmos.rmosguestx;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.preference.PreferenceManager;
import java.util.Locale;
public class LocaleHelper {
    
private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";

    
// the method is used to set the language at runtime
    
public static Context setLocale(Context context, String language) {
        
persist(context, language);

        
// updating the language for devices above android nougat
        
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            
return updateResources(context, language);
        }
        
// for devices having lower version of android os
        
return updateResourcesLegacy(context, language);
    }

    
private static void persist(Context context, String language) {
        
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        
SharedPreferences.Editor editor = preferences.edit();
        
editor.putString(SELECTED_LANGUAGE, language);
        
editor.apply();
    }
    
// the method is used update the language of application by creating
    // object of inbuilt Locale class and passing language argument to it
    
@TargetApi(Build.VERSION_CODES.N)
    
private static Context updateResources(Context context, String language) {
        
Locale locale = new Locale(language);
        
Locale.setDefault(locale);
        
Configuration configuration = context.getResources().getConfiguration();
        
configuration.setLocale(locale);
        
configuration.setLayoutDirection(locale);
        
return context.createConfigurationContext(configuration);
    }

    
@SuppressWarnings("deprecation")
    
private static Context updateResourcesLegacy(Context context, String language) {
        
Locale locale = new Locale(language);
        
Locale.setDefault(locale);
        
Resources resources = context.getResources();
        
Configuration configuration = resources.getConfiguration();
        
configuration.locale = locale;
        
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            
configuration.setLayoutDirection(locale);
        }
        
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
        
return context;
    }}

istediğin dilde değerini almak için kullanımı :

 

Context context = LocaleHelper.setLocale(Login.this, "tr"); // en
 
Resources   resources = context.getResources();
 
Toast.makeText(getApplicationContext(), ""+resources.getString(R.string.mesajlar), Toast.LENGTH_SHORT).show();

 

istediğin dile çevirmek için 1.yol

 

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public static void setLocale(Activity activity, String languageCode) {
    
Locale locale = new Locale(languageCode);
    
Locale.setDefault(locale);
    
Resources resources = activity.getResources();
    
Configuration config = resources.getConfiguration();
    
config.setLocale(locale);
    
resources.updateConfiguration(config, resources.getDisplayMetrics());
}

 

@Override
protected void onCreate(Bundle savedInstanceState) {
    
super.onCreate(savedInstanceState);
    
setLocale(Login.this,"en"); // ilk bunu yazarsın sonra layoutu eklersin

    setContentView(R.layout.activity_login);

 

btnGiris.setOnClickListener(new View.OnClickListener() { // recreate(); ile de yaparsın
            
@Override
            
public void onClick(View view) {
                Genel.setLocale(Login.this,"en");
                recreate();
            }
        });

 

}

 

 

istediğin dile çevirmek için  2. yol

@Override
protected void attachBaseContext(Context base) {// bu yolda "en" i sharedpreferences'e kaydedirsin oradan alırsın .
    
super.attachBaseContext(LocaleHelper.setLocale(base,"en"));
}

 

 

 

 

Locale.getDefault().getLanguage()       ---> en      

Locale.getDefault().getISO3Language()   ---> eng

Locale.getDefault().getCountry()        ---> US

Locale.getDefault().getISO3Country()    ---> USA

Locale.getDefault().getDisplayCountry() ---> United States

Locale.getDefault().getDisplayName()    ---> English (United States)

Locale.getDefault().toString()          ---> en_US

Locale.getDefault().getDisplayLanguage()---> English

Locale.getDefault().toLanguageTag()     ---> en-US

 

 

 

********BAYRAK DİZAYN*******

 

activity_main.xml

 

<LinearLayout
    
android:id="@+id/linearLayoutBayrakLogin"
    
android:layout_width="wrap_content"
    
android:layout_height="wrap_content"
    
android:layout_alignParentRight="true"
    
android:layout_marginTop="15dp"
    
android:layout_marginRight="15dp"
    
android:gravity="center"
    
android:orientation="horizontal">



    <
ImageView
        
android:id="@+id/imageBayrakLogin"
        
android:layout_width="wrap_content"
        
android:layout_height="wrap_content"
         
/>

    <
ImageView
        
android:layout_width="wrap_content"
        
android:layout_height="wrap_content"
        
android:src="@drawable/asagi_ok" />

</
LinearLayout>

 

drawable-> asagi_ok.xml

 

<vector android:height="24dp" android:tint="#A9A9A9"
    
android:viewportHeight="24" android:viewportWidth="24"
    
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <
path android:fillColor="@android:color/white" android:pathData="M7.41,8.59L12,13.17l4.59,-4.58L18,10l-6,6 -6,-6 1.41,-1.41z"/>
</
vector>

bayrak_menu.xml

 

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    
xmlns:app="http://schemas.android.com/apk/res-auto"
    
>

    <
item
        
android:id="@+id/menuTurkce"
        
android:title="Türkçe"
        
android:icon="@drawable/turkce"
        
/>
    <
item
        
android:id="@+id/menuIngilizce"
        
android:title="English"
        
android:icon="@drawable/ingilizce"
        
/>
    <
item
        
android:id="@+id/menuAlmanca"
        
android:title="Deutsche"
        
android:icon="@drawable/almanca"
        
/>
    <
item
        
android:id="@+id/menuRusca"
        
android:title="Pусский"
        
android:icon="@drawable/rusca"
        
/>
</
menu>

 

 

MainActiviy.java

 

public void bayrakClick() {
    
LinearLayout linearLayoutBayrakLogin = findViewById(R.id.linearLayoutBayrakLogin);
    
linearLayoutBayrakLogin.setOnClickListener(new View.OnClickListener() {
        
@Override
        
public void onClick(View view) {
            
PopupMenu popup = new PopupMenu(Login.this, linearLayoutBayrakLogin);
            
popup.getMenuInflater().inflate(R.menu.bayrak_menu, popup.getMenu());

            
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
                
@Override
                
public boolean onMenuItemClick(MenuItem menuItem) {
                    
switch (menuItem.getItemId()) {
                        
case R.id.menuAlmanca:
                            
Genel.setLocale(Login.this,Genel.de);
                            recreate();
                            
break;
                        
case R.id.menuIngilizce:
                            
Genel.setLocale(Login.this,Genel.en);
                            recreate();
                            
break;
                        
case R.id.menuRusca:
                            
Genel.setLocale(Login.this,Genel.ru);
                            recreate();
                            
break;
                        
case R.id.menuTurkce:
                            
Genel.setLocale(Login.this,Genel.tr);
                            recreate();
                            
break;
                    }
                    
return true;
                }
            });

            setForceShowIcon(
popup);
            
popup.show();
        }
    });

}

public void setForceShowIcon(PopupMenu popupMenu) {
    
try {
        
Field[] fields = popupMenu.getClass().getDeclaredFields();
        
for (Field field : fields) {
            
if ("mPopup".equals(field.getName())) {
                
field.setAccessible(true);
                
Object menuPopupHelper = field.get(popupMenu);
                
Class<?> classPopupHelper = Class.forName(menuPopupHelper
                        
.getClass().getName());
                
Method setForceIcons = classPopupHelper.getMethod(
                        
"setForceShowIcon", boolean.class);
                
setForceIcons.invoke(menuPopupHelper, true);
                
break;
            }
        }
    }
catch (Throwable e) {
        e.printStackTrace();
    }
}

 

 

AYRICA VARSAYILAN DİL YÜKLEME

 

public void dilYukle() {
    
String dil = Locale.getDefault().getLanguage();
    
// Toast.makeText(getApplicationContext(), ""+dil, Toast.LENGTH_SHORT).show();
    
switch (dil) {
        
case "en":
            resimYukle(
"ingilizce");
            
break;
        
case "tr":
            resimYukle(
"turkce");
            
break;
        
case "ru":
            resimYukle(
"rusca");
            
break;
        
case "de":
            resimYukle(
"almanca");
            
break;
    }

}


public void resimYukle(String bayrakLogo) {
    
try {
        
int id = kendi.getResources().getIdentifier("com.rmos.rmosguestx:drawable/" + bayrakLogo, null, null);
        
imageBayrakLogin.setImageResource(id);
    }
catch (Exception ex) {
        
Toast.makeText(getApplicationContext(), "HATA " + ex.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

 

 

         

 

drawable de -> ingilizce.png var misal

 2022 Ocak 10 Pazartesi
 378