Re: [android-developers] Bing translator api issue
http://stackoverflow.com/questions/35557993/microsoft-translation-api-caused-by-java-io-filenotfoundexception hello, my friend met this problem, do you have any idea about it ? On Monday, 25 January 2016, Marina Cuello wrote: > I don't really get what you want to do. I'm guessing you want to use only > one strings.xml and translate "on-the-fly" all the texts for this and the > rest of your activities? > The best way would be to just translate the strings to another strings.xml > and use the values/your-language bucket to store it. And then just change > language. I've found this Q&A doing a fast search: > http://stackoverflow.com/questions/12908289/how-to-change-language-of-app-when-user-selects-language > . > > If you need to use the Bing service because it's some exercise or proof of > concept or something like that, you could make a Dictionary class or > service that translates every string on the file and stores the translation > on some form of cache. > You could make it eager and load every string just once. In this Q&A you > can find how to get all the IDs so you can then retrieve the strings you'll > have to translate: http://stackoverflow.com/a/14389698/671431 > Or you could make it lazy and get one string at a time. In both cases, > there will be some performance penalty on your UI. One will be a long wait > everytime the user changes to a new language, and the other will make the > first load of every screen a bit slow. > And then there will be even more penalty because anyway you would need to > make a someView.setText(yourDictionary.getText(R.string.someString)) call > for every TextView and perhaps also setContentDescription on the > ImageViews. > > I don't think it's pretty. > > Marina > > > On Sun, Jan 24, 2016 at 10:06 AM, Yong Lin > wrote: > >> Below is my code, i wanted to use the bing translate api to translate my >> strings.xml . I have a spinner in my code, when the user select the >> language in my spinner, i need to translate my strings.xml to my language, >> may i know how to do it? >> >> >> package com.plantidentification; >> >> import android.app.Activity; >> import android.os.AsyncTask; >> import android.os.Bundle; >> import android.view.View; >> import android.widget.AdapterView; >> import android.widget.Spinner; >> import android.widget.Toast; >> import com.memetix.mst.language.Language; >> import com.memetix.mst.translate.Translate; >> >> /** >> * Created by YongLin on 1/23/2016. >> */ >> public class LanguageActivity extends Activity { >> Spinner spinnerctrl; >> String translatedText; >> >> >> public void onCreate(Bundle savedInstanceState) { >> super.onCreate(savedInstanceState); >> >> >> setTitle(R.string.title_activity_language); >> spinnerctrl = (Spinner) findViewById(R.id.spinner1); >> setContentView(R.layout.activity_language); >> >> spinnerctrl.setOnItemSelectedListener(new >> AdapterView.OnItemSelectedListener() { >> >> public void onItemSelected(AdapterView parent, View view, >>int pos, long id) { >> >> if (pos == 1) { >> >> Toast.makeText(parent.getContext(), >> getResources().getString(R.string.YouselectEn), >> Toast.LENGTH_SHORT) >> .show(); >> >> } else if (pos == 2) { >> >> Toast.makeText(parent.getContext(), >> getResources().getString(R.string.YouselectZH), >> Toast.LENGTH_SHORT) >> .show(); >> new MyAsyncTask() { >> protected void onPostExecute(Boolean result) { >> >> translatedText=getResources().getString(R.string.HELLO); >> } >> }.execute(); >> >> } else if (pos == 3) { >> >> Toast.makeText(parent.getContext(), >> getResources().getString(R.string.YouselectHI), >> Toast.LENGTH_SHORT) >> .show(); >> >> } >> else if (pos == 4) { >> >> Toast.makeText(parent.getContext(), >> getResources().getString(R.string.YouselectMs), >> Toast.LENGTH_SHORT) >> .show(); >> >> } >> >> >> } >>public void onNothingSelected(AdapterView arg0) { >> // TODO Auto-generated method stub >> Toast.makeText(getApplicationContext(), "Nothing to select", >> Toast.LENGTH_LONG).show(); >> } >> >> class MyAsyncTask extends AsyncTask { >> @Override >> protected Boolean doInBackground(String... arg0) { >> Translate.setClientId("plantidentification"); >> >> Translate.setClientSecret("lMFkUM+gG96NFrhhZV649Ix8NxR2SvqX
Re: [android-developers] Bing translator api issue
I don't really get what you want to do. I'm guessing you want to use only one strings.xml and translate "on-the-fly" all the texts for this and the rest of your activities? The best way would be to just translate the strings to another strings.xml and use the values/your-language bucket to store it. And then just change language. I've found this Q&A doing a fast search: http://stackoverflow.com/questions/12908289/how-to-change-language-of-app-when-user-selects-language . If you need to use the Bing service because it's some exercise or proof of concept or something like that, you could make a Dictionary class or service that translates every string on the file and stores the translation on some form of cache. You could make it eager and load every string just once. In this Q&A you can find how to get all the IDs so you can then retrieve the strings you'll have to translate: http://stackoverflow.com/a/14389698/671431 Or you could make it lazy and get one string at a time. In both cases, there will be some performance penalty on your UI. One will be a long wait everytime the user changes to a new language, and the other will make the first load of every screen a bit slow. And then there will be even more penalty because anyway you would need to make a someView.setText(yourDictionary.getText(R.string.someString)) call for every TextView and perhaps also setContentDescription on the ImageViews. I don't think it's pretty. Marina On Sun, Jan 24, 2016 at 10:06 AM, Yong Lin wrote: > Below is my code, i wanted to use the bing translate api to translate my > strings.xml . I have a spinner in my code, when the user select the > language in my spinner, i need to translate my strings.xml to my language, > may i know how to do it? > > > package com.plantidentification; > > import android.app.Activity; > import android.os.AsyncTask; > import android.os.Bundle; > import android.view.View; > import android.widget.AdapterView; > import android.widget.Spinner; > import android.widget.Toast; > import com.memetix.mst.language.Language; > import com.memetix.mst.translate.Translate; > > /** > * Created by YongLin on 1/23/2016. > */ > public class LanguageActivity extends Activity { > Spinner spinnerctrl; > String translatedText; > > > public void onCreate(Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > > > setTitle(R.string.title_activity_language); > spinnerctrl = (Spinner) findViewById(R.id.spinner1); > setContentView(R.layout.activity_language); > > spinnerctrl.setOnItemSelectedListener(new > AdapterView.OnItemSelectedListener() { > > public void onItemSelected(AdapterView parent, View view, >int pos, long id) { > > if (pos == 1) { > > Toast.makeText(parent.getContext(), > getResources().getString(R.string.YouselectEn), > Toast.LENGTH_SHORT) > .show(); > > } else if (pos == 2) { > > Toast.makeText(parent.getContext(), > getResources().getString(R.string.YouselectZH), > Toast.LENGTH_SHORT) > .show(); > new MyAsyncTask() { > protected void onPostExecute(Boolean result) { > > translatedText=getResources().getString(R.string.HELLO); > } > }.execute(); > > } else if (pos == 3) { > > Toast.makeText(parent.getContext(), > getResources().getString(R.string.YouselectHI), > Toast.LENGTH_SHORT) > .show(); > > } > else if (pos == 4) { > > Toast.makeText(parent.getContext(), > getResources().getString(R.string.YouselectMs), > Toast.LENGTH_SHORT) > .show(); > > } > > > } >public void onNothingSelected(AdapterView arg0) { > // TODO Auto-generated method stub > Toast.makeText(getApplicationContext(), "Nothing to select", > Toast.LENGTH_LONG).show(); > } > > class MyAsyncTask extends AsyncTask { > @Override > protected Boolean doInBackground(String... arg0) { > Translate.setClientId("plantidentification"); > > Translate.setClientSecret("lMFkUM+gG96NFrhhZV649Ix8NxR2SvqXJWpOTOgNq54="); > try { > translatedText = Translate.execute( > getResources().getString(R.string.HELLO), Language.ENGLISH, > Language.CHINESE_SIMPLIFIED); > } catch(Exception e) { > translatedText = e.toString(); > } > return true; >
[android-developers] Bing translator api issue
Below is my code, i wanted to use the bing translate api to translate my strings.xml . I have a spinner in my code, when the user select the language in my spinner, i need to translate my strings.xml to my language, may i know how to do it? package com.plantidentification; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.Spinner; import android.widget.Toast; import com.memetix.mst.language.Language; import com.memetix.mst.translate.Translate; /** * Created by YongLin on 1/23/2016. */ public class LanguageActivity extends Activity { Spinner spinnerctrl; String translatedText; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTitle(R.string.title_activity_language); spinnerctrl = (Spinner) findViewById(R.id.spinner1); setContentView(R.layout.activity_language); spinnerctrl.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { public void onItemSelected(AdapterView parent, View view, int pos, long id) { if (pos == 1) { Toast.makeText(parent.getContext(), getResources().getString(R.string.YouselectEn), Toast.LENGTH_SHORT) .show(); } else if (pos == 2) { Toast.makeText(parent.getContext(), getResources().getString(R.string.YouselectZH), Toast.LENGTH_SHORT) .show(); new MyAsyncTask() { protected void onPostExecute(Boolean result) { translatedText=getResources().getString(R.string.HELLO); } }.execute(); } else if (pos == 3) { Toast.makeText(parent.getContext(), getResources().getString(R.string.YouselectHI), Toast.LENGTH_SHORT) .show(); } else if (pos == 4) { Toast.makeText(parent.getContext(), getResources().getString(R.string.YouselectMs), Toast.LENGTH_SHORT) .show(); } } public void onNothingSelected(AdapterView arg0) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "Nothing to select", Toast.LENGTH_LONG).show(); } class MyAsyncTask extends AsyncTask { @Override protected Boolean doInBackground(String... arg0) { Translate.setClientId("plantidentification"); Translate.setClientSecret("lMFkUM+gG96NFrhhZV649Ix8NxR2SvqXJWpOTOgNq54="); try { translatedText = Translate.execute( getResources().getString(R.string.HELLO), Language.ENGLISH, Language.CHINESE_SIMPLIFIED); } catch(Exception e) { translatedText = e.toString(); } return true; } } }); } } -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. To post to this group, send email to android-developers@googlegroups.com. Visit this group at https://groups.google.com/group/android-developers. To view this discussion on the web visit https://groups.google.com/d/msgid/android-developers/a9249230-b520-4f19-bde0-5ddc9cf3817c%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.