1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-07-16 00:14:34 +02:00

Desktop: Fixes #5927: Update menu item labels when the language changes

This commit is contained in:
Laurent Cozic
2021-12-31 07:50:32 +01:00
parent e813d15ef2
commit f451633a51
3 changed files with 19 additions and 7 deletions

View File

@ -89,6 +89,7 @@ interface Props {
['spellChecker.language']: string; ['spellChecker.language']: string;
plugins: PluginStates; plugins: PluginStates;
customCss: string; customCss: string;
locale: string;
} }
const commandNames: string[] = menuCommandNames(); const commandNames: string[] = menuCommandNames();
@ -249,7 +250,11 @@ function useMenu(props: Props) {
const keymapService = KeymapService.instance(); const keymapService = KeymapService.instance();
const pluginCommandNames = props.pluginMenuItems.map((view: any) => view.commandName); const pluginCommandNames = props.pluginMenuItems.map((view: any) => view.commandName);
const menuItemDic = menuUtils.commandsToMenuItems(commandNames.concat(pluginCommandNames), (commandName: string) => onMenuItemClickRef.current(commandName)); const menuItemDic = menuUtils.commandsToMenuItems(
commandNames.concat(pluginCommandNames),
(commandName: string) => onMenuItemClickRef.current(commandName),
props.locale
);
const quitMenuItem = { const quitMenuItem = {
label: _('Quit'), label: _('Quit'),
@ -830,7 +835,7 @@ function useMenu(props: Props) {
clearTimeout(timeoutId); clearTimeout(timeoutId);
timeoutId = null; timeoutId = null;
}; };
}, [props.routeName, props.pluginMenuItems, props.pluginMenus, keymapLastChangeTime, modulesLastChangeTime, props['spellChecker.language'], props['spellChecker.enabled'], props.plugins, props.customCss]); }, [props.routeName, props.pluginMenuItems, props.pluginMenus, keymapLastChangeTime, modulesLastChangeTime, props['spellChecker.language'], props['spellChecker.enabled'], props.plugins, props.customCss, props.locale]);
useMenuStates(menu, props); useMenuStates(menu, props);
@ -872,6 +877,7 @@ const mapStateToProps = (state: AppState) => {
return { return {
menuItemProps: menuUtils.commandsToMenuItemProps(commandNames.concat(pluginCommandNames(state.pluginService.plugins)), whenClauseContext), menuItemProps: menuUtils.commandsToMenuItemProps(commandNames.concat(pluginCommandNames(state.pluginService.plugins)), whenClauseContext),
locale: state.settings.locale,
routeName: state.route.routeName, routeName: state.route.routeName,
selectedFolderId: state.selectedFolderId, selectedFolderId: state.selectedFolderId,
layoutButtonSequence: state.settings.layoutButtonSequence, layoutButtonSequence: state.settings.layoutButtonSequence,

View File

@ -163,6 +163,7 @@ describe('services_CommandService', function() {
it('should create menu items from commands', (async () => { it('should create menu items from commands', (async () => {
const service = newService(); const service = newService();
const utils = new MenuUtils(service); const utils = new MenuUtils(service);
const locale = 'fr_FR';
registerCommand(service, createCommand('test1', { registerCommand(service, createCommand('test1', {
execute: () => {}, execute: () => {},
@ -178,7 +179,7 @@ describe('services_CommandService', function() {
clickedCommands.push(commandName); clickedCommands.push(commandName);
}; };
const menuItems = utils.commandsToMenuItems(['test1', 'test2'], onClick); const menuItems = utils.commandsToMenuItems(['test1', 'test2'], onClick, locale);
menuItems.test1.click(); menuItems.test1.click();
menuItems.test2.click(); menuItems.test2.click();
@ -186,7 +187,10 @@ describe('services_CommandService', function() {
expect(clickedCommands.join('_')).toBe('test1_test2'); expect(clickedCommands.join('_')).toBe('test1_test2');
// Also check that the same commands always return strictly the same menu // Also check that the same commands always return strictly the same menu
expect(utils.commandsToMenuItems(['test1', 'test2'], onClick)).toBe(utils.commandsToMenuItems(['test1', 'test2'], onClick)); expect(utils.commandsToMenuItems(['test1', 'test2'], onClick, locale)).toBe(utils.commandsToMenuItems(['test1', 'test2'], onClick, locale));
// And check that if the locale changes, new menu items are returned
expect(utils.commandsToMenuItems(['test1', 'test2'], onClick, locale)).not.toBe(utils.commandsToMenuItems(['test1', 'test2'], onClick, 'en_GB'));
})); }));
it('should give menu item props from state', (async () => { it('should give menu item props from state', (async () => {

View File

@ -97,8 +97,8 @@ export default class MenuUtils {
}); });
} }
public commandsToMenuItems(commandNames: string[], onClick: Function): MenuItems { public commandsToMenuItems(commandNames: string[], onClick: Function, locale: string): MenuItems {
const key: string = `${this.keymapService.lastSaveTime}_${commandNames.join('_')}`; const key: string = `${this.keymapService.lastSaveTime}_${commandNames.join('_')}_${locale}`;
if (this.menuItemCache_[key]) return this.menuItemCache_[key]; if (this.menuItemCache_[key]) return this.menuItemCache_[key];
const output: MenuItems = {}; const output: MenuItems = {};
@ -107,7 +107,9 @@ export default class MenuUtils {
output[commandName] = this.commandToMenuItem(commandName, onClick); output[commandName] = this.commandToMenuItem(commandName, onClick);
} }
this.menuItemCache_[key] = output; this.menuItemCache_ = {
[key]: output,
};
return output; return output;
} }