Skip to content

Commit

Permalink
Add I18n settings
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Jun 3, 2024
1 parent a97e6f2 commit 0cf2de8
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 15 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

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

17 changes: 17 additions & 0 deletions assets/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,20 @@ Themes…: "Themes…"
Sign in: "Sign In"
Sign Out: "Sign Out"
Toggle User Menu: "Toggle User Menu"
File: "File"
Edit: "Edit"
New: "New"
New Window: "New Window"
Open…: "Open…"
Open Recent...: "Open Recent..."
Add Folder to Project…: "Add Folder to Project…"
Save: "Save"
Save As…: "Save As…"
Save All: "Save All"
Close Editor: "Close Editor"
Close Window: "Close Window"
Undo: "Undo"
Redo: "Redo"
Cut: "Cut"
Copy: "Copy"
Paste: "Paste"
17 changes: 17 additions & 0 deletions assets/locales/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,20 @@ Themes…: 主题…
Sign in: 登录
Sign Out: 登出
Toggle User Menu: 切换用户菜单
File: 文件
Edit: 编辑
New: 新建
New Window: "新建窗口"
Open…: 打开…
Open Recent...: 打开最近…
Add Folder to Project…: 添加文件夹到项目…
Save: "保存"
Save As…: "另存为…"
Save All: "全部保存"
Close Editor: "关闭编辑器"
Close Window: "关闭窗口"
Undo: "撤销"
Redo: "重做"
Cut: "剪切"
Copy: "复制"
Paste: "粘贴"
8 changes: 4 additions & 4 deletions crates/gpui/src/platform/mac/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl MacPlatform {

for menu_config in menus {
let menu = NSMenu::new(nil).autorelease();
menu.setTitle_(ns_string(menu_config.name));
menu.setTitle_(ns_string(&menu_config.name));
menu.setDelegate_(delegate);

for item_config in menu_config.items {
Expand Down Expand Up @@ -311,7 +311,7 @@ impl MacPlatform {

item = NSMenuItem::alloc(nil)
.initWithTitle_action_keyEquivalent_(
ns_string(name),
ns_string(&name),
selector,
ns_string(key_to_native(&keystroke.key).as_ref()),
)
Expand Down Expand Up @@ -342,7 +342,7 @@ impl MacPlatform {
} else {
item = NSMenuItem::alloc(nil)
.initWithTitle_action_keyEquivalent_(
ns_string(name),
ns_string(&name),
selector,
ns_string(""),
)
Expand All @@ -362,7 +362,7 @@ impl MacPlatform {
submenu.addItem_(Self::create_menu_item(item, delegate, actions, keymap));
}
item.setSubmenu_(submenu);
item.setTitle_(ns_string(name));
item.setTitle_(ns_string(&name));
item
}
}
Expand Down
8 changes: 7 additions & 1 deletion crates/i18n/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ license = "GPL-3.0-or-later"
workspace = true

[dependencies]
anyhow.workspace = true
assets.workspace = true
rust-i18n.workspace = true
serde_yaml = "0.9"
util.workspace = true
gpui.workspace = true
settings.workspace = true
serde.workspace = true
schemars.workspace = true

serde_yaml = "0.9"
23 changes: 21 additions & 2 deletions crates/i18n/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use ::settings::Settings;
use ::settings::SettingsStore;
use gpui::AppContext;
use settings::I18nSettings;
use std::collections::HashMap;
use util::ResultExt;

mod settings;

rust_i18n::i18n!(fallback = "en");

type Translations = HashMap<String, HashMap<String, String>>;
Expand All @@ -23,12 +29,10 @@ impl Backend {
.to_string_lossy()
.to_string();

dbg!(f.clone());
if let Some(asset) = assets::Assets::get(&f) {
if let Some(data) =
serde_yaml::from_slice::<HashMap<String, String>>(&asset.data).log_err()
{
dbg!("install trs", locale.clone());
trs.insert(locale, data);
}
}
Expand Down Expand Up @@ -62,5 +66,20 @@ macro_rules! init {
};
}

pub fn init(cx: &mut AppContext) {
settings::I18nSettings::register(cx);

let previus_locale = I18nSettings::get_global(cx).locale.clone();
cx.observe_global::<SettingsStore>(move |cx| {
let locale = I18nSettings::get_global(cx).locale.clone();
if previus_locale != locale {
dbg!("locale changed to {}", locale);
// TODO: Dispatch Notification to tell use to restart Zed to apply new locale
}
})
.detach();
}

pub use rust_i18n::available_locales;
pub use rust_i18n::set_locale;
pub use rust_i18n::t;
53 changes: 53 additions & 0 deletions crates/i18n/src/settings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use anyhow::Result;
use gpui::AppContext;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use settings::SettingsSources;

#[derive(Clone)]
pub struct I18nSettings {
/// Set the UI language, default is "en".
pub locale: String,
}

impl I18nSettings {
/// Switches to the locale and reloads the translations.
pub fn switch_locale(&mut self, locale: &str, _cx: &mut AppContext) -> Option<String> {
let mut new_locale = None;

if crate::available_locales!().iter().any(|l| *l == locale) {
self.locale = locale.to_string();
new_locale = Some(locale.to_string());
}

new_locale
}
}
/// Settings for rendering text in UI and text buffers.
#[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema)]
pub struct I18nSettingsContent {
/// The default font size for text in the UI.
#[serde(default)]
pub locale: Option<String>,
}

impl settings::Settings for I18nSettings {
const KEY: Option<&'static str> = None;

type FileContent = I18nSettingsContent;

fn load(sources: SettingsSources<Self::FileContent>, cx: &mut AppContext) -> Result<Self> {
let defaults = sources.default;
let mut this = Self {
locale: defaults.locale.clone().unwrap(),
};

for value in sources.user.into_iter().chain(sources.release_channel) {
if let Some(locale) = &value.locale {
this.locale = locale.to_string();
}
}

Ok(this)
}
}
2 changes: 1 addition & 1 deletion crates/zed/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fn init_ui(app_state: Arc<AppState>, cx: &mut AppContext) -> Result<()> {

SystemAppearance::init(cx);
load_embedded_fonts(cx);

i18n::init(cx);
theme::init(theme::LoadThemes::All(Box::new(Assets)), cx);
app_state.languages.set_theme(cx.theme().clone());
command_palette::init(cx);
Expand Down
14 changes: 7 additions & 7 deletions crates/zed/src/zed/app_menus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@ pub fn app_menus() -> Vec<Menu<'static>> {
Menu {
name: t!("File"),
items: vec![
MenuItem::action("New", workspace::NewFile),
MenuItem::action("New Window", workspace::NewWindow),
MenuItem::action(t!("New"), workspace::NewFile),
MenuItem::action(t!("New Window"), workspace::NewWindow),
MenuItem::separator(),
MenuItem::action("Open…", workspace::Open),
MenuItem::action(t!("Open…"), workspace::Open),
MenuItem::action(
t!("Open Recent..."),
recent_projects::OpenRecent {
create_new_window: true,
},
),
MenuItem::separator(),
MenuItem::action("Add Folder to Project…", workspace::AddFolderToProject),
MenuItem::action("Save", workspace::Save { save_intent: None }),
MenuItem::action("Save As…", workspace::SaveAs),
MenuItem::action("Save All", workspace::SaveAll { save_intent: None }),
MenuItem::action(t!("Add Folder to Project…"), workspace::AddFolderToProject),
MenuItem::action(t!("Save"), workspace::Save { save_intent: None }),
MenuItem::action(t!("Save As…"), workspace::SaveAs),
MenuItem::action(t!("Save All"), workspace::SaveAll { save_intent: None }),
MenuItem::action(
t!("Close Editor"),
workspace::CloseActiveItem { save_intent: None },
Expand Down

0 comments on commit 0cf2de8

Please sign in to comment.