Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
Expand Down Expand Up @@ -120,6 +124,8 @@ public class E4Application implements IApplication {
private static final String SHOWLOCATION_ARG_NAME = "showLocation";
private static final String DEFAULT_THEME_ID = "org.eclipse.e4.ui.css.theme.e4_default";
public static final String HIGH_CONTRAST_THEME_ID = "org.eclipse.e4.ui.css.theme.high-contrast";
private static final String NL_STATE_FILENAME = "last_nl.properties"; //$NON-NLS-1$
private static final String NL_STATE_KEY = "eclipse.last.nl"; //$NON-NLS-1$

private String[] args;

Expand Down Expand Up @@ -371,6 +377,10 @@ private MApplication loadApplicationModel(IApplicationContext appContext, IEclip
// Persisted state
Boolean clearPersistedState = getArgValue(IWorkbench.CLEAR_PERSISTED_STATE, appContext, true)
.map(Boolean::parseBoolean).orElse(Boolean.FALSE);
// If not already clearing, check if NL locale changed since last run
if (!clearPersistedState) {
clearPersistedState = hasNlChanged(instanceLocation);
}
eclipseContext.set(IWorkbench.CLEAR_PERSISTED_STATE, clearPersistedState);

String resourceHandler = getArgValue(IWorkbench.MODEL_RESOURCE_HANDLER, appContext, false)
Expand All @@ -385,6 +395,59 @@ private MApplication loadApplicationModel(IApplicationContext appContext, IEclip
return (MApplication) resource.getContents().get(0);
}

/**
* Checks whether the Eclipse locale (-nl) has changed since the last launch.
* Persists the current locale in a properties file alongside workbench.xmi.
*
* @return true if the locale has changed since the last launch, false otherwise
*/
private boolean hasNlChanged(Location instanceLocation) {
if (instanceLocation == null || instanceLocation.getURL() == null) {
return false;
}

Path stateDir;
try {
stateDir = Path.of(instanceLocation.getURL().toURI()).resolve(".metadata") //$NON-NLS-1$
.resolve(".plugins") //$NON-NLS-1$
.resolve("org.eclipse.e4.workbench"); //$NON-NLS-1$
} catch (URISyntaxException e) {
return false;
}
Path nlFile = stateDir.resolve(NL_STATE_FILENAME);

String currentNL = Platform.getNL();
Properties props = new Properties();
boolean localeChanged = false;

// Read previously saved locale if file exists
if (Files.isRegularFile(nlFile)) {
try (InputStream in = Files.newInputStream(nlFile)) {
props.load(in);
String savedNL = props.getProperty(NL_STATE_KEY);
if (!currentNL.equals(savedNL)) {
localeChanged = true;
}
} catch (IOException e) {
// Cannot read - file will be rewritten below
localeChanged = true;
}
}
// Write current locale - covers first launch, locale change and recovery cases
props.setProperty(NL_STATE_KEY, currentNL);
try {
Files.createDirectories(stateDir); // ensure directory exists before writing
try (OutputStream out = Files.newOutputStream(nlFile)) {
props.store(out, "Eclipse last used NL locale"); //$NON-NLS-1$
}
} catch (IOException e) {
// Cannot write - non-fatal, skip clearing to be safe
return false;
}

return localeChanged;
}

private URI determineApplicationModelURI(IApplicationContext appContext) {
Optional<String> appModelPath = getArgValue(IWorkbench.XMI_URI_ARG, appContext, false);

Expand Down
Loading