I m writing one application which display all menu and all text of application in english and spanish. can any one tell me how to solve this problem.one thing i can do is to convert all text in spanish when suser select spanish language .is there any inbuilt android functionality that convert all text to selected language..
- 1developer.android.com/guide/topics/resources/localization.htmldenis.solonenko– denis.solonenko2011-12-30 07:08:34 +00:00Commented Dec 30, 2011 at 7:08
- possible duplicate of How to implement Android l10n ?J-16 SDiZ– J-16 SDiZ2011-12-30 07:11:34 +00:00Commented Dec 30, 2011 at 7:11
6 Answers
create folder values-es (for Spanish)
create strings.xml file for Spanish language
I suppose you have two button in screen to change the language, add following code on button click
String language ="es"; Locale locale = null; if (language.equalsIgnoreCase("en")) { locale = new Locale("en"); } else if (language.equalsIgnoreCase("es")) { locale = new Locale("es"); } Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; getBaseContext().getResources().updateConfiguration(config, null);If you want to change the language on the fly then you cane call oncreate(null)
Thank You,
Comments
Create different resource folder to handle localization. eg for spanish cretae folder named as value-esand value-en for english keep Strings.xml file in both of the folder with all the strings with same name only there value will differ from spanish to english.
You can access the string in the same way as you are doing generally using getString(R.string.name); 
Comments
if you store all of your strings in the strings.xml (int the values folder), you can create language modifiers. And when you reference strings instead of:
String name = "name";
You would:
String name = getString(R.string.name);
Here is the documentation: http://developer.android.com/guide/topics/resources/string-resource.html
Comments
Localization:
In that Developer's need to create different values folders for different locale objects.each values folder contains its own strings.xml files ( for strings ),colors.xml files ( for colors ). In that Values folder developer must provide the particular language data (Welcome message like that in different languages what ever he want) pick appropriate resource when the localization is changed (using emulator settings Home -->Menu --> Settings --> Language & Keyboard --> Select language)
Comments
Class for localization:
package com.org.net; import android.util.Log; import java.util.Map; import java.util.HashMap; enum Languages { lnEn, lnRu; } public class $ { private static volatile $ instance; private static $ getInstance() { $ localInstance = instance; if ( localInstance == null ) { synchronized ( $.class ) { localInstance = instance; if ( localInstance == null ) { instance = localInstance = new $(); } } } return localInstance; } public static String $( String key ) { String[] value = getInstance().map.get( key ); if ( value != null ) { return value[ getInstance().lang.ordinal() ]; } else { Log.e( "translate", "not found translate: " + "map.put( \"" + key + "\", new String[] {\"" + key + "\"} );\n" ); return key; } } public static void defaultLang() { String lang = Locale.getDefault().getLanguage(); if ( lang.equals( "ru" ) ) { $.setLang( Languages.lnRu ); } else { $.setLang( Languages.lnEn ); } } public static void setLang( Languages lang ) { getInstance().lang = lang; } private Languages lang; private Map<String, String[]> map; private $() { lang = Languages.lnEn; map = new HashMap<String, String[]>(); map.put( "Hello", new String[] { "Hello", "Привет", } ); map.put( "Translate it", new String[] { "Translate it", "Переведи это", } ); boolean success = true; for ( Map.Entry<String, String[]> entry : map.entrySet() ) { String[] value = entry.getValue(); if ( value.length != Languages.values().length ) { Log.e( "translate", "not found translate: " + entry.getKey() ); success = false; } } if ( ! success ) { throw new RuntimeException( "not found translate" ); } } } Use:
// $.defaultLang() or $.setLang( Languages.lnRu ) for select language button.setText( $.$( "Hello" ) ); Utility for search string in code (php):
<? $h1 = opendir( 'code/' ); if ( $h1 === false ) { return; } $files = array(); while ( false !== ( $file = readdir( $h1 ) ) ) { if ( $file != "." && $file != ".." ) { if ( $file == "$.java" ) { continue; } $files[] = 'code/'.$file; } } closedir( $h1 ); sort( $files ); $word_count = 0; $inquotes_str = ""; $translate_str = ""; $map_fill_str = ""; foreach ( $files as $file ) { $contents = file_get_contents( $file ); $matches = null; preg_match_all( '/(["\'])(?:\\\\\1|(?!\1).)+\1/', $contents, $matches ); foreach( $matches[0] as $line ) { $inquotes_str .= "{$line}\n"; } $matches = null; preg_match_all( '/\$\.\$\(\s*".*?"\s*\)/', $contents, $matches ); foreach( $matches[0] as $line ) { preg_match( '/".*"/', $line, $line ); $line = $line[0]; $translate_str .= "{$line}\n"; $word_count += str_word_count( $line ); $map_fill_str .= <<< EOF map.put( {$line}, new String[] { {$line}, } ); EOF; } } file_put_contents( "inquotes_str.txt", $inquotes_str ); file_put_contents( "translate_str.txt", $translate_str ); file_put_contents( "map_fill_str.txt", $map_fill_str ); echo "{$word_count}\n"; ?>