Skip to content

Commit

Permalink
Merge pull request #268 from alley-rs/0.1.X
Browse files Browse the repository at this point in the history
feat: support Engligh
  • Loading branch information
thep0y authored Nov 10, 2024
2 parents acf0f5c + bd4c895 commit 9d9011d
Show file tree
Hide file tree
Showing 27 changed files with 420 additions and 72 deletions.
10 changes: 10 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ tracing-subscriber = { version = "0", features = [
tracing = { version = "0", features = ["log", "release_max_level_info"] }
tracing-appender = '0'
rust-embed = "8"
sys-locale = "0"


[features]
Expand Down
33 changes: 33 additions & 0 deletions src-tauri/src/i18n/en_us.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use super::Translations;

pub(super) const EN_US: &Translations = &Translations {
window_title: "Fluxy",
dark_mode_tooltip: "Switch to Light Mode",
light_mode_tooltip: "Switch to Dark Mode",
about_button_tooltip: "About and Help",
about_dialog_github_tooltip: "Visit Official Website",
about_dialog_feedback_tooltip: "Provide Feedback or Get Help",
home_label_text: "Select Transfer Method",
home_button_text: "Back to Home",
home_receive_button_text: "Receive",
home_send_button_text: "Send",
qrcode_page_title: "Scan to Connect",
qrcode_page_url_label: "Or visit in another computer's browser",
qrcode_page_url_tooltip: "Copy Link to Clipboard",
qrcode_page_url_copied_message: "Link Copied",
qrcode_page_toast_message: "Please scan this QR code with your phone",
ok_button_text: "Confirm",
clear_button_text: "Clear File List",
send_page_title: "Send Files",
send_page_empty_drop_description: "Drag files here",
send_page_drop_description: "You can continue dragging more files",
list_item_file_size_label: "Size",
list_item_file_type_label: "Type",
send_page_list_item_tooltip: "Click to Preview File",
receive_page_empty_description: "Please upload files from your phone or another computer",
receive_page_list_item_tooltip: "Click to Open File with Default Program",
receive_page_dropdown_open_button_label: "Open",
receive_page_dropdown_pick_button_label: "Change",
receive_page_directory_path_label: "Save Directory",
receive_page_directory_path_tooltip: "Click to Open Directory in File Explorer",
};
61 changes: 61 additions & 0 deletions src-tauri/src/i18n/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use std::{collections::HashMap, sync::LazyLock};

use serde::Serialize;

mod en_us;
mod zh_cn;

pub static LOCALES: LazyLock<HashMap<&'static Locale, &Translations>> = LazyLock::new(|| {
[(&Locale::ZhCN, zh_cn::ZH_CN), (&Locale::EnUS, en_us::EN_US)]
.iter()
.copied()
.collect()
});

#[derive(PartialEq, Eq, Hash)]
pub enum Locale {
ZhCN,
EnUS,
}

impl From<String> for Locale {
fn from(value: String) -> Self {
match value.as_ref() {
"zh-CNa" => Self::ZhCN,
_ => Self::EnUS,
}
}
}

#[derive(PartialEq, Eq, Hash, Serialize)]
pub struct Translations {
pub window_title: &'static str,
pub dark_mode_tooltip: &'static str,
pub light_mode_tooltip: &'static str,
pub about_button_tooltip: &'static str,
pub about_dialog_github_tooltip: &'static str,
pub about_dialog_feedback_tooltip: &'static str,
pub home_label_text: &'static str,
pub home_button_text: &'static str,
pub home_send_button_text: &'static str,
pub home_receive_button_text: &'static str,
pub qrcode_page_title: &'static str,
pub qrcode_page_url_label: &'static str,
pub qrcode_page_url_tooltip: &'static str,
pub qrcode_page_url_copied_message: &'static str,
pub qrcode_page_toast_message: &'static str,
pub ok_button_text: &'static str,
pub clear_button_text: &'static str,
pub send_page_title: &'static str,
pub send_page_empty_drop_description: &'static str,
pub send_page_drop_description: &'static str,
pub list_item_file_size_label: &'static str,
pub list_item_file_type_label: &'static str,
pub send_page_list_item_tooltip: &'static str,
pub receive_page_empty_description: &'static str,
pub receive_page_list_item_tooltip: &'static str,
pub receive_page_dropdown_open_button_label: &'static str,
pub receive_page_dropdown_pick_button_label: &'static str,
pub receive_page_directory_path_label: &'static str,
pub receive_page_directory_path_tooltip: &'static str,
}
33 changes: 33 additions & 0 deletions src-tauri/src/i18n/zh_cn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use super::Translations;

pub(super) const ZH_CN: &Translations = &Translations {
window_title: "小路速传",
dark_mode_tooltip: "切换为亮色",
light_mode_tooltip: "切换为暗色",
about_button_tooltip: "关于和帮助",
about_dialog_github_tooltip: "访问官网",
about_dialog_feedback_tooltip: "反馈问题或寻求帮助",
home_label_text: "选择传输方式",
home_button_text: "回到主页",
home_receive_button_text: "接收",
home_send_button_text: "发送",
qrcode_page_title: "扫码连接",
qrcode_page_url_label: "或在另一台电脑中通过浏览器中访问",
qrcode_page_url_tooltip: "复制链接到剪贴板",
qrcode_page_url_copied_message: "已复制链接",
qrcode_page_toast_message: "请使用手机扫描此二维码",
ok_button_text: "确认",
clear_button_text: "清空文件列表",
send_page_title: "发送文件",
send_page_empty_drop_description: "将文件拖到此处",
send_page_drop_description: "可继续拖入文件",
list_item_file_size_label: "大小",
list_item_file_type_label: "类型",
send_page_list_item_tooltip: "单击预览文件",
receive_page_empty_description: "请在手机端或者另一台电脑中上传文件",
receive_page_list_item_tooltip: "单击使用默认程序打开此文件",
receive_page_dropdown_open_button_label: "打开",
receive_page_dropdown_pick_button_label: "修改",
receive_page_directory_path_label: "保存目录",
receive_page_directory_path_tooltip: "单击在文件管理器中打开此目录",
};
18 changes: 17 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

mod error;
mod i18n;
mod lazy;
#[cfg(target_os = "linux")]
mod linux;
Expand All @@ -22,12 +23,14 @@ use std::{

use qrcode_generator::QrCodeEcc;
use serde::Serialize;
use sys_locale::get_locale;
use tauri::{AppHandle, Manager, UpdaterEvent};
use time::macros::{format_description, offset};
use tokio::fs::File;
use tracing::Level;
use tracing_subscriber::fmt::time::OffsetTime;

use crate::i18n::{Locale, Translations, LOCALES};
use crate::lazy::LOCAL_IP;
#[cfg(target_os = "macos")]
use crate::menu::{handle_menu_event, new_menu};
Expand Down Expand Up @@ -190,6 +193,12 @@ async fn get_files_metadata(paths: Vec<PathBuf>) -> FluxyResult<Vec<SendFile>> {
Ok(files)
}

#[tauri::command]
fn get_locale_translations() -> &'static Translations {
let locale: Locale = get_locale().unwrap_or_else(|| String::from("en-US")).into();
LOCALES[&locale]
}

#[tauri::command]
fn is_linux() -> bool {
cfg!(target_os = "linux")
Expand Down Expand Up @@ -244,6 +253,11 @@ async fn main() -> FluxyResult<()> {
#[cfg(not(debug_assertions))]
builder.json().init();

let locale = get_locale().unwrap_or_else(|| String::from("en-US"));
debug!("current locale: {}", locale);
let locale: Locale = locale.into();
let translations = LOCALES[&locale];

#[cfg(target_os = "linux")]
{
let scale_factor = crate::linux::get_scale_factor()?;
Expand All @@ -262,6 +276,7 @@ async fn main() -> FluxyResult<()> {
let mut builder = tauri::Builder::default()
.setup(|app| {
if let Some(w) = app.get_window("main") {
w.set_title(translations.window_title).unwrap();
if MAIN_WINDOW.set(w).is_err() {
error!(message = "设置主窗口失败");
app.handle().exit(1);
Expand All @@ -277,7 +292,8 @@ async fn main() -> FluxyResult<()> {
get_files_metadata,
get_send_files_url_qr_code,
is_linux,
show_main_window
show_main_window,
get_locale_translations
]);

// windows 和 linux 的菜单在窗口内, 无法自动切换暗色, 所以不使用菜单
Expand Down
5 changes: 1 addition & 4 deletions src-tauri/src/server/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ impl ServerError {
) -> Self {
let msg: Option<&str> = error.into();
let advice: Option<&str> = advice.into();
let advice = match advice {
None => None,
Some(s) => Some(s.to_owned()),
};
let advice = advice.map(|s| s.to_owned());

match msg {
None => Self::Internal,
Expand Down
21 changes: 15 additions & 6 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Match, Switch, createSignal, onMount } from "solid-js";
import { Match, Switch, createResource, createSignal, onMount } from "solid-js";
import { TbArrowsTransferUp, TbArrowsTransferDown } from "solid-icons/tb";
import { BiRegularSun, BiSolidMoon } from "solid-icons/bi";
import {
Expand All @@ -15,7 +15,7 @@ import "~/App.scss";
import useDark from "alley-components/lib/hooks/useDark";
import About from "./about";
import { AppContext } from "./context";
import { showMainWindow } from "./api";
import { getLocaleTranslations, showMainWindow } from "./api";

enum Mode {
Send = 1,
Expand All @@ -27,6 +27,7 @@ const App = () => {

const [mode, setMode] = createSignal<Mode | null>(null);
const [showAbout, setShowAbout] = createSignal<boolean>(false);
const [translations] = createResource(getLocaleTranslations);

const goHome = () => setMode(null);

Expand All @@ -36,10 +37,18 @@ const App = () => {
<AppContext.Provider
value={{
goHome,
translations,
about: { show: showAbout, onShow: () => setShowAbout(true) },
}}
>
<LazyTooltip text={`切换为${isDark() ? "亮" : "暗"}色`} placement="left">
<LazyTooltip
text={
isDark()
? translations()!.dark_mode_tooltip
: translations()!.light_mode_tooltip
}
placement="left"
>
<LazySwitch
class="dark-switch"
checked={isDark()}
Expand All @@ -56,15 +65,15 @@ const App = () => {
<Switch
fallback={
<div id="index">
<div>选择传输方式</div>
<div>{translations()?.home_label_text}</div>

{suspense(
<LazyButton
class="fill"
icon={<TbArrowsTransferDown />}
onClick={() => setMode(Mode.Receive)}
>
接收
{translations()?.home_receive_button_text}
</LazyButton>,
)}

Expand All @@ -74,7 +83,7 @@ const App = () => {
icon={<TbArrowsTransferUp />}
onClick={() => setMode(Mode.Send)}
>
发送
{translations()?.home_send_button_text}
</LazyButton>,
)}

Expand Down
14 changes: 11 additions & 3 deletions src/about.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import {
LazyTypographyTitle,
} from "./lazy";
import useDark from "alley-components/lib/hooks/useDark";
import { createSignal, onMount } from "solid-js";
import { createSignal, onMount, useContext } from "solid-js";
import { app } from "@tauri-apps/api";
import { AiFillGithub } from "solid-icons/ai";
import { RiCommunicationFeedbackLine } from "solid-icons/ri";
import { open } from "@tauri-apps/api/shell";
import "./about.scss";
import { AppContext } from "./context";

const About = () => {
const { translations } = useContext(AppContext)!;
const [name, setName] = createSignal("");
const [version, setVersion] = createSignal("");

Expand All @@ -39,7 +41,10 @@ const About = () => {
</LazySpace>

<LazySpace>
<LazyTooltip text="访问官网" placement="top">
<LazyTooltip
text={translations()!.about_dialog_github_tooltip}
placement="top"
>
<LazyButton
icon={<AiFillGithub />}
type="plain"
Expand All @@ -48,7 +53,10 @@ const About = () => {
/>
</LazyTooltip>

<LazyTooltip text="反馈问题或寻求帮助" placement="top">
<LazyTooltip
text={translations()!.about_dialog_feedback_tooltip}
placement="top"
>
<LazyButton
icon={<RiCommunicationFeedbackLine />}
type="plain"
Expand Down
3 changes: 3 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ export const getSendFilesUrlQrCode = async (files: SendFile[]) =>
export const isLinux = async () => await invoke<boolean>("is_linux");

export const showMainWindow = async () => invoke<void>("show_main_window");

export const getLocaleTranslations = async () =>
invoke<Translations>("get_locale_translations");
3 changes: 2 additions & 1 deletion src/components/aboutButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import { LazyFloatButton } from "~/lazy";
const AboutButton = () => {
const {
about: { onShow },
translations,
} = useContext(AppContext)!;

return (
<LazyFloatButton
icon={<AiOutlineQuestion />}
tooltip="关于和帮助"
tooltip={translations()!.about_button_tooltip}
onClick={onShow}
/>
);
Expand Down
Loading

0 comments on commit 9d9011d

Please sign in to comment.