在url android studio中使用string translate

时间:2017-09-21 07:33:25

标签: android

我尝试构建天气应用程序使用凌空,我的代码已经正常工作没有任何问题。但我想添加语言到我的应用程序使用字符串,朗英语和阿拉伯语。我从wundgeound api得到天气信息。 wunground api包含许多语言。我试图得到en和ar。

代码

String url = "http://api.wunderground.com/api/xxxxxxxx/conditions/hourly/forecast10day/geolookup/"+R.string.lang+"/q/"+latitude+","+longitude+".json";

当我调试网址

http://api.wunderground.com/api/xxxxx/conditions/hourly/forecast10day/geolookup/2131230829/q/33.2379217,44.3381602.json

我得到的数字而不是语言ar或en,大多数都是

http://api.wunderground.com/api/xxxxx/conditions/hourly/forecast10day/geolookup/lang:AR or En/q/33.2379217,44.3381602.json

en

中的字符串res
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="about">About</string>
    <string name="desc">weather</string>
    <string name="forecast">weather forecast</string>
    <string name="satellite">satellite</string>
    <string name="news">news</string>
    <string name="action_settings">setting</string>
    <string name="app_name">weathery</string>
    <string name="navigation_drawer_open">open</string>
    <string name="navigation_drawer_close">close</string>
    <string name="loading">please wait</string>
    <string name="lang">lang:EN</string>

</resources>

阿拉伯语中的字符串res

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="about">عنا</string>
    <string name="desc">مصدرك الاولى لمعرفة اخر اخبار الطقس و المناخ للعراق</string>
    <string name="forecast">حالة الطقس</string>
    <string name="satellite">الاقمار صناعية</string>
    <string name="news">الاخبار</string>
    <string name="action_settings">ضبط</string>
    <string name="app_name">منظومة ارصاد العراق</string>
    <string name="navigation_drawer_open">فتح</string>
    <string name="navigation_drawer_close">غلق</string>
    <string name="loading">يرجى الانتظار</string>
    <string name="lang">lang:AR</string>

</resources>

1 个答案:

答案 0 :(得分:1)

你应该使用

String myLang = getResources().getString(R.string.lang);


String url = "http://api.wunderground.com/api/xxxxxxxx/conditions/hourly/forecast10day/geolookup/"+myLang+"/q/"+latitude+","+longitude+".json";