Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Language Switcher #72

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
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
51 changes: 51 additions & 0 deletions src/components/language-picker.tsx

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add Vietnamese language?

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { SelectControl } from '@wordpress/components';
import { useState } from 'react';
import { getIpcApi } from '../lib/get-ipc-api';

// TODO: use supportedLocales from locale.ts
export const supportedLocales = {
ar: 'العربية',
de: 'Deutsch',
en: 'English',
es: 'Español',
fr: 'Français',
he: 'עברית',
id: 'Bahasa Indonesia',
it: 'Italiano',
ja: '日本語',
ko: '한국어',
nl: 'Nederlands',
pl: 'Polski',
'pt-br': 'Português (Brasil)',
ru: 'Русский',
sv: 'Svenska',
tr: 'Türkçe',
'zh-cn': '简体中文',
'zh-tw': '繁體中文',
};

export const LanguagePicker = () => {
const [ locale, setLocale ] = useState( '' );

const handleLocaleChange = ( value: string ) => {
console.log( `Switching to locale: ${ value }` );
setLocale( value );

// Save locale string to saveUserLocale
getIpcApi().saveUserLocale( value );
};

return (
<div className="flex gap-5 flex-col">
<h2 className="a8c-subtitle-small">Language</h2>
<SelectControl
value={ locale || 'en' }
onChange={ handleLocaleChange }
options={ Object.entries( supportedLocales ).map( ( [ locale, name ] ) => ( {
value: locale,
label: name as string,
} ) ) }
/>
</div>
);
};
7 changes: 7 additions & 0 deletions src/components/tests/user-settings.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ describe( 'UserSettings', () => {
callback();
}
} );

// Mock window.matchMedia
window.matchMedia = jest.fn().mockReturnValue( {
matches: false,
addListener: jest.fn(),
removeListener: jest.fn(),
} );
} );

it( 'logs in when not authenticated', async () => {
Expand Down
2 changes: 2 additions & 0 deletions src/components/user-settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { cx } from '../lib/cx';
import { getIpcApi } from '../lib/get-ipc-api';
import Button from './button';
import { Gravatar } from './gravatar';
import { LanguagePicker } from './language-picker';
import Modal from './modal';
import offlineIcon from './offline-icon';
import ProgressBar from './progress-bar';
Expand Down Expand Up @@ -196,6 +197,7 @@ export default function UserSettings() {
<div className="gap-6 flex flex-col">
<UserInfo onLogout={ logout } user={ user } />
<div className="border border-[#F0F0F0] w-full"></div>
<LanguagePicker />
<SnapshotInfo
isDeleting={ loadingDeletingAllSnapshots }
isDisabled={
Expand Down
8 changes: 8 additions & 0 deletions src/ipc-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,14 @@ export async function saveOnboarding(
} );
}

export async function saveUserLocale( _event: IpcMainInvokeEvent, userLocale: string ) {
const userData = await loadUserData();
await saveUserData( {
...userData,
userLocale,
} );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this approach will be a bit limiting until userData could be saved and loaded across both the main and renderer threads.

}

export async function getThumbnailData( _event: IpcMainInvokeEvent, id: string ) {
const path = getSiteThumbnailPath( id );
return getImageData( path );
Expand Down
21 changes: 21 additions & 0 deletions src/lib/locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@ import type { LocaleData } from '@wordpress/i18n';

export const DEFAULT_LOCALE = 'en';

export const namedLocales = {
ar: 'العربية',
de: 'Deutsch',
en: 'English',
es: 'Español',
fr: 'Français',
he: 'עברית',
id: 'Bahasa Indonesia',
it: 'Italiano',
ja: '日本語',
ko: '한국어',
nl: 'Nederlands',
pl: 'Polski',
'pt-br': 'Português (Brasil)',
ru: 'Русский',
sv: 'Svenska',
tr: 'Türkçe',
'zh-cn': '简体中文',
'zh-tw': '繁體中文',
};

const supportedLocales = [
'ar',
'de',
Expand Down
1 change: 1 addition & 0 deletions src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const api: IpcApi = {
getOnboardingData: () => ipcRenderer.invoke( 'getOnboardingData' ),
saveOnboarding: ( onboardingCompleted: boolean ) =>
ipcRenderer.invoke( 'saveOnboarding', onboardingCompleted ),
saveUserLocale: ( locale: string ) => ipcRenderer.invoke( 'saveUserLocale', locale ),
openTerminalAtPath: ( targetPath: string ) =>
ipcRenderer.invoke( 'openTerminalAtPath', targetPath ),
showMessageBox: ( options: Electron.MessageBoxOptions ) =>
Expand Down
1 change: 1 addition & 0 deletions src/storage/storage-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface UserData {
devToolsOpen?: boolean;
authToken?: StoredToken;
onboardingCompleted?: boolean;
userLocale?: string;
lastBumpStats?: {
[ group: string ]: {
[ stat: string ]: number;
Expand Down
Loading