Merge "Refactor RunInLocale a bit"
commit
a980fb39a5
|
@ -30,25 +30,23 @@ public abstract class RunInLocale<T> {
|
||||||
* Execute {@link #job(Resources)} method in specified system locale exclusively.
|
* Execute {@link #job(Resources)} method in specified system locale exclusively.
|
||||||
*
|
*
|
||||||
* @param res the resources to use.
|
* @param res the resources to use.
|
||||||
* @param newLocale the locale to change to.
|
* @param newLocale the locale to change to. Run in system locale if null.
|
||||||
* @return the value returned from {@link #job(Resources)}.
|
* @return the value returned from {@link #job(Resources)}.
|
||||||
*/
|
*/
|
||||||
public T runInLocale(final Resources res, final Locale newLocale) {
|
public T runInLocale(final Resources res, final Locale newLocale) {
|
||||||
synchronized (sLockForRunInLocale) {
|
synchronized (sLockForRunInLocale) {
|
||||||
final Configuration conf = res.getConfiguration();
|
final Configuration savedConf = res.getConfiguration();
|
||||||
final Locale oldLocale = conf.locale;
|
if (newLocale == null || newLocale.equals(savedConf.locale)) {
|
||||||
final boolean needsChange = (newLocale != null && !newLocale.equals(oldLocale));
|
return job(res);
|
||||||
|
}
|
||||||
|
final Configuration newConf = new Configuration();
|
||||||
|
newConf.setTo(savedConf);
|
||||||
|
newConf.setLocale(newLocale);
|
||||||
try {
|
try {
|
||||||
if (needsChange) {
|
res.updateConfiguration(newConf, null);
|
||||||
conf.locale = newLocale;
|
|
||||||
res.updateConfiguration(conf, null);
|
|
||||||
}
|
|
||||||
return job(res);
|
return job(res);
|
||||||
} finally {
|
} finally {
|
||||||
if (needsChange) {
|
res.updateConfiguration(savedConf, null);
|
||||||
conf.locale = oldLocale;
|
|
||||||
res.updateConfiguration(conf, null);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue