2016-09-02 35 views
0

我正在製作一種應用程序,它使用兩種語言英語和烏爾都語。我有英文字符串的文件,我有字符串名稱When_undressing_arxml中的字符串標籤在api上創建了一些問題16

<string name="When_undressing_ar">بِسْمِ اللهِ</string> 

,並有獨立的烏爾都語串XML進行烏爾都語語言,它具有字符串標籤作爲

<string name="لباس_اتارتے_وقت_کیا_پڑھیں_ar">بِسْمِ اللهِ</string> 

,並通過調用字符串歌廳這些值

private static String getDuwaElement(String nameame, Context context) { 
int duwaId = context.getResources().getIdentifier(name, "string", context.getPackageName()); 

return context.getString(duwaId); 
} 

當語言設置爲英語時,名稱爲「When_undressing_ar」。而名爲「لباس_اتارتے_وقت_کیا_پڑھیں_AR」當語言設置爲烏爾都語

這一切工作正常,給我所有的資源在兩種語言,

然而,當我試圖在API 22這個程序,它給不同的是資源未找到 。這個效果很好,直到api 16。請幫助是什麼原因造成這個問題,它提供了資源沒有發現異常

+0

你不應該翻譯的字符串即名鍵= 「When_undressing_ar」。例如textview.setText(R.string.when_undressing_ar);如果設備語言是urdu,語言設置爲en和urdu資源將獲得en資源 –

+0

我知道,我已經使用了它應該在urdu中的邏輯。我的問題是爲什麼這是給我的問題在更高的API然後16 – FaisalAhmed

+0

什麼是你的urdu值的文件夾的名稱? –

回答

2

全部工作演示

MainActivity

public class MainActivity extends AppCompatActivity { 

    Context context; 
    TextView textView; 
    SharedPreferences preferences; 
    String[] SuppertedLangCodes = {"en", "ur"}; 
    String key = "lang"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     context = this; 
     preferences = getSharedPreferences("myprefs", MODE_PRIVATE); 

     textView = (TextView) findViewById(R.id.foodName); 
     textView.setText(getResources().getText(R.string.txt_name)); 
     textView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (preferences.getString("lang", "").toString().equalsIgnoreCase("") || preferences.getString("lang", "").toString().equalsIgnoreCase(SuppertedLangCodes[0])) { 

        applyLanguage(context, SuppertedLangCodes[1]); 
        preferences.edit().putString(key, SuppertedLangCodes[1]).apply(); 
       } else { 

        applyLanguage(context, SuppertedLangCodes[0]); 
        preferences.edit().putString(key, SuppertedLangCodes[0]).apply(); 
       } 
      } 
     }); 
    } 

    public void applyLanguage(Context context, String language) { 
     android.content.res.Configuration config = new android.content.res.Configuration(); 
     // Since API level 17 or below cause issues with RTL, lets keep them LTR 
     if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) { 
      config.locale = new Locale("en"); 
     } else { 
      config.locale = new Locale(language); 
     } 
     context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics()); 
     recreate(); 
    } 
} 

activity_main:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/base" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="10dp" 
    android:background="@color/white" 
    android:orientation="horizontal" 
    android:weightSum="10"> 


    <TextView 
     android:id="@+id/foodName" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:gravity="left" 
     android:inputType="textCapWords" 
     android:textColor="@color/colorPrimaryDark" 
     android:textColorHint="@color/colorPrimaryDark" 
     android:textSize="18sp" 
     android:layout_marginLeft="20dp" /> 

</RelativeLayout> 

的strings.xml正常

<resources> 
    <string name="app_name">Gallery Test</string> 
    <string name="txt_title_photo_gallery">Photo Gallery</string> 
    <string name="txt_name">what to pray while undressing</string> 

</resources> 

從烏爾文件夾的strings.xml

<?xml version="1.0" encoding="utf-8"?> 
    <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> 

     <!--<string name="txt_name" translatable="false">pk.wiseapps.texter.sms.ps.GSM_SMS</string>--> 
     <string name="txt_name">لباس_اتارتے_وقت_کیا_پڑھیں</string> 

    </resources> 

enter image description here

enter image description here

+0

我在這個地方給出了烏爾都的名字「txt_name」。我想我應該改變自己的邏輯,並用烏爾都語和英語給英文命名 – FaisalAhmed