Locale detection code optimization and beautification