/**
 * Appcelerator Titanium Mobile
 * Copyright (c) 2010-2012 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Apache Public License
 * Please see the LICENSE included with this distribution for details.
 */
package ti.modules.titanium.locale;

import java.util.Locale;

import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiContext;
import org.appcelerator.titanium.util.TiPlatformHelper;
import org.appcelerator.titanium.util.TiRHelper;

import android.telephony.PhoneNumberUtils;

@Kroll.module
public class LocaleModule extends KrollModule
{
	private static final String TAG = "LocaleModule";

	public LocaleModule()
	{
		super();
	}

	public LocaleModule(TiContext tiContext)
	{
		this();
	}
	
	@Kroll.method @Kroll.getProperty
	public String getCurrentLanguage()
	{
		return Locale.getDefault().getLanguage();
	}
	
	@Kroll.method @Kroll.getProperty
	public String getCurrentCountry()
	{
		return Locale.getDefault().getCountry();
	}
	
	@Kroll.method @Kroll.getProperty
	public String getCurrentLocale()
	{
		return TiPlatformHelper.getInstance().getLocale();
	}
	
	@Kroll.method
	public String getCurrencyCode(String localeString) 
	{
		if (localeString == null) {
			return null;
		}
		Locale locale = TiPlatformHelper.getInstance().getLocale(localeString);
		return TiPlatformHelper.getInstance().getCurrencyCode(locale);
	}
	
	@Kroll.method
	public String getCurrencySymbol(String currencyCode)
	{
		return TiPlatformHelper.getInstance().getCurrencySymbol(currencyCode);
	}
	
	@Kroll.method
	public String getLocaleCurrencySymbol(String localeString)
	{
		if (localeString == null) {
			return null;
		}
		Locale locale = TiPlatformHelper.getInstance().getLocale(localeString);
		return TiPlatformHelper.getInstance().getCurrencySymbol(locale);
	}
	
	@SuppressWarnings("deprecation")
	@Kroll.method
	public String formatTelephoneNumber(String telephoneNumber)
	{
		return PhoneNumberUtils.formatNumber(telephoneNumber);
	}
	
	@Kroll.method @Kroll.setProperty
	public void setLanguage(String language) 
	{
		Log.w(TAG, "Locale.setLanguage not supported for Android.");
	}

	@Kroll.method  @Kroll.topLevel("L")
	public String getString(String key, @Kroll.argument(optional=true) String defaultValue)
	{
		try {
			int resid = TiRHelper.getResource("string." + key.replace(".","_"));
			if (resid != 0) {
				return TiApplication.getInstance().getString(resid);
			} else {
				return defaultValue;
			}
		} catch (TiRHelper.ResourceNotFoundException e) {
			Log.d(TAG, "Resource string with key '" + key + "' not found.  Returning default value.", Log.DEBUG_MODE);
			return defaultValue;
		} catch (Exception e) {
			Log.e(TAG, "Error trying to get resource string with key '" + key + "':", e);
			return defaultValue;
		}
	}

	@Override
	public String getApiName()
	{
		return "Ti.Locale";
	}
}

@@ -2206,68 +2206,579 @@
 %7B%0A%09%09
-Log.w(TAG, %22Locale.setLanguage not supported for Android.%22);
+try %7B%0A%09%09%09String%5B%5D parts = language.split(%22-%22);%0A%09%09%09Locale locale = null;%0A%09%09%09%0A%09%09%09if (parts.length %3E 1) %7B%0A%09%09%09%09locale = new Locale(parts%5B0%5D, parts%5B1%5D);%0A%09%09%09%7D else %7B%0A%09%09%09%09locale = new Locale(parts%5B0%5D);%0A%09%09%09%7D%0A%09%09%09 %0A%09%09%09Locale.setDefault(locale);%0A%09%09%09%0A%09%09%09Configuration config = new Configuration();%0A%09%09%09config.locale = locale;%0A%09%09%09%0A%09%09%09Context ctx =  TiApplication.getInstance().getBaseContext();%0A%09%09%09ctx.getResources().updateConfiguration(config, ctx.getResources().getDisplayMetrics());%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09Log.e(TAG, %22Error trying to set language '%22 + language + %22':%22, e);%0A%09%09%7D
 %0A%09%7D%0A

Add support to set language like iOS