iced_code_editor/i18n.rs
1//! Internationalization support for the code editor.
2//!
3//! This module provides translation support for UI text in the search dialog.
4//!
5//! # Using `Translations`
6//!
7//! The translations are available in YAML files in the `locales` directory,
8//! loaded through the `rust-i18n` crate. Application code does not call
9//! `rust-i18n`'s `t!` macro directly — it is not part of this crate's public
10//! API. Instead, look up translated strings through [`Translations`]:
11//!
12//! ```
13//! use iced_code_editor::{Language, Translations};
14//!
15//! let translations = Translations::new(Language::English);
16//! assert_eq!(translations.search_placeholder(), "Search...");
17//! ```
18
19/// Supported languages for the code editor UI.
20///
21/// # Examples
22///
23/// ```
24/// use iced_code_editor::Language;
25///
26/// let lang = Language::English;
27/// assert_eq!(lang, Language::default());
28/// ```
29#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
30pub enum Language {
31 /// English language
32 #[default]
33 English,
34 /// French language
35 French,
36 /// Spanish language
37 Spanish,
38 /// German language
39 German,
40 /// Italian language
41 Italian,
42 /// Portuguese (Brazilian) language
43 PortugueseBR,
44 /// Portuguese (European) language
45 PortuguesePT,
46 /// Simplified Chinese language
47 ChineseSimplified,
48}
49
50impl Language {
51 /// Returns the locale code for this language.
52 ///
53 /// # Examples
54 ///
55 /// ```
56 /// use iced_code_editor::Language;
57 ///
58 /// assert_eq!(Language::English.to_locale(), "en");
59 /// assert_eq!(Language::French.to_locale(), "fr");
60 /// assert_eq!(Language::Spanish.to_locale(), "es");
61 /// assert_eq!(Language::German.to_locale(), "de");
62 /// assert_eq!(Language::Italian.to_locale(), "it");
63 /// assert_eq!(Language::PortugueseBR.to_locale(), "pt-BR");
64 /// assert_eq!(Language::PortuguesePT.to_locale(), "pt-PT");
65 /// ```
66 #[must_use]
67 pub const fn to_locale(self) -> &'static str {
68 match self {
69 Self::English => "en",
70 Self::French => "fr",
71 Self::Spanish => "es",
72 Self::German => "de",
73 Self::Italian => "it",
74 Self::PortugueseBR => "pt-BR",
75 Self::PortuguesePT => "pt-PT",
76 Self::ChineseSimplified => "zh-CN",
77 }
78 }
79}
80
81/// Provides translated text strings for UI elements.
82///
83/// This struct contains all UI text translations used in the search dialog,
84/// including placeholders, tooltips, and labels.
85///
86/// # Examples
87///
88/// ```
89/// use iced_code_editor::{Language, Translations};
90///
91/// let translations = Translations::new(Language::French);
92/// assert_eq!(translations.search_placeholder(), "Rechercher...");
93/// ```
94#[derive(Debug, Clone, Copy, Default)]
95pub struct Translations {
96 language: Language,
97}
98
99impl Translations {
100 /// Creates a new `Translations` instance with the specified language.
101 ///
102 /// This sets the global rust-i18n locale to the specified language.
103 ///
104 /// # Examples
105 ///
106 /// ```
107 /// use iced_code_editor::{Language, Translations};
108 ///
109 /// let translations = Translations::new(Language::Spanish);
110 /// assert_eq!(translations.language(), Language::Spanish);
111 /// ```
112 #[must_use]
113 pub fn new(language: Language) -> Self {
114 rust_i18n::set_locale(language.to_locale());
115 Self { language }
116 }
117
118 /// Returns the current language.
119 ///
120 /// # Examples
121 ///
122 /// ```
123 /// use iced_code_editor::{Language, Translations};
124 ///
125 /// let translations = Translations::new(Language::French);
126 /// assert_eq!(translations.language(), Language::French);
127 /// ```
128 #[must_use]
129 pub const fn language(&self) -> Language {
130 self.language
131 }
132
133 /// Sets the language for translations.
134 ///
135 /// This updates the global rust-i18n locale.
136 ///
137 /// # Examples
138 ///
139 /// ```
140 /// use iced_code_editor::{Language, Translations};
141 ///
142 /// let mut translations = Translations::new(Language::English);
143 /// translations.set_language(Language::Spanish);
144 /// assert_eq!(translations.language(), Language::Spanish);
145 /// ```
146 pub fn set_language(&mut self, language: Language) {
147 self.language = language;
148 rust_i18n::set_locale(language.to_locale());
149 }
150
151 /// Returns the placeholder text for the search input field.
152 ///
153 /// # Examples
154 ///
155 /// ```
156 /// use iced_code_editor::{Language, Translations};
157 ///
158 /// let en = Translations::new(Language::English);
159 /// assert_eq!(en.search_placeholder(), "Search...");
160 ///
161 /// let fr = Translations::new(Language::French);
162 /// assert_eq!(fr.search_placeholder(), "Rechercher...");
163 /// ```
164 #[must_use]
165 pub fn search_placeholder(&self) -> String {
166 rust_i18n::t!("search.placeholder", locale = self.language.to_locale())
167 .into_owned()
168 }
169
170 /// Returns the placeholder text for the replace input field.
171 ///
172 /// # Examples
173 ///
174 /// ```
175 /// use iced_code_editor::{Language, Translations};
176 ///
177 /// let es = Translations::new(Language::Spanish);
178 /// assert_eq!(es.replace_placeholder(), "Reemplazar...");
179 /// ```
180 #[must_use]
181 pub fn replace_placeholder(&self) -> String {
182 rust_i18n::t!("replace.placeholder", locale = self.language.to_locale())
183 .into_owned()
184 }
185
186 /// Returns the label text for the case sensitive checkbox.
187 ///
188 /// # Examples
189 ///
190 /// ```
191 /// use iced_code_editor::{Language, Translations};
192 ///
193 /// let fr = Translations::new(Language::French);
194 /// assert_eq!(fr.case_sensitive_label(), "Sensible à la casse");
195 /// ```
196 #[must_use]
197 pub fn case_sensitive_label(&self) -> String {
198 rust_i18n::t!(
199 "settings.case_sensitive_label",
200 locale = self.language.to_locale()
201 )
202 .into_owned()
203 }
204
205 /// Returns the tooltip text for the previous match button.
206 ///
207 /// # Examples
208 ///
209 /// ```
210 /// use iced_code_editor::{Language, Translations};
211 ///
212 /// let en = Translations::new(Language::English);
213 /// assert_eq!(en.previous_match_tooltip(), "Previous match (Shift+F3)");
214 /// ```
215 #[must_use]
216 pub fn previous_match_tooltip(&self) -> String {
217 rust_i18n::t!(
218 "search.previous_match_tooltip",
219 locale = self.language.to_locale()
220 )
221 .into_owned()
222 }
223
224 /// Returns the tooltip text for the next match button.
225 ///
226 /// # Examples
227 ///
228 /// ```
229 /// use iced_code_editor::{Language, Translations};
230 ///
231 /// let es = Translations::new(Language::Spanish);
232 /// assert_eq!(es.next_match_tooltip(), "Siguiente coincidencia (F3 / Enter)");
233 /// ```
234 #[must_use]
235 pub fn next_match_tooltip(&self) -> String {
236 rust_i18n::t!(
237 "search.next_match_tooltip",
238 locale = self.language.to_locale()
239 )
240 .into_owned()
241 }
242
243 /// Returns the tooltip text for the close search dialog button.
244 ///
245 /// # Examples
246 ///
247 /// ```
248 /// use iced_code_editor::{Language, Translations};
249 ///
250 /// let fr = Translations::new(Language::French);
251 /// assert_eq!(fr.close_search_tooltip(), "Fermer la recherche (Échap)");
252 /// ```
253 #[must_use]
254 pub fn close_search_tooltip(&self) -> String {
255 rust_i18n::t!(
256 "search.close_tooltip",
257 locale = self.language.to_locale()
258 )
259 .into_owned()
260 }
261
262 /// Returns the tooltip text for the replace current match button.
263 ///
264 /// # Examples
265 ///
266 /// ```
267 /// use iced_code_editor::{Language, Translations};
268 ///
269 /// let en = Translations::new(Language::English);
270 /// assert_eq!(en.replace_current_tooltip(), "Replace current match");
271 /// ```
272 #[must_use]
273 pub fn replace_current_tooltip(&self) -> String {
274 rust_i18n::t!(
275 "replace.current_tooltip",
276 locale = self.language.to_locale()
277 )
278 .into_owned()
279 }
280
281 /// Returns the tooltip text for the replace all matches button.
282 ///
283 /// # Examples
284 ///
285 /// ```
286 /// use iced_code_editor::{Language, Translations};
287 ///
288 /// let es = Translations::new(Language::Spanish);
289 /// assert_eq!(es.replace_all_tooltip(), "Reemplazar todo");
290 /// ```
291 #[must_use]
292 pub fn replace_all_tooltip(&self) -> String {
293 rust_i18n::t!("replace.all_tooltip", locale = self.language.to_locale())
294 .into_owned()
295 }
296
297 /// Returns the context-menu label for undo.
298 ///
299 /// # Examples
300 ///
301 /// ```
302 /// use iced_code_editor::{Language, Translations};
303 ///
304 /// let en = Translations::new(Language::English);
305 /// assert_eq!(en.context_menu_undo(), "Undo");
306 ///
307 /// let fr = Translations::new(Language::French);
308 /// assert_eq!(fr.context_menu_undo(), "Annuler");
309 /// ```
310 #[must_use]
311 pub fn context_menu_undo(&self) -> String {
312 rust_i18n::t!("context_menu.undo", locale = self.language.to_locale())
313 .into_owned()
314 }
315
316 /// Returns the context-menu label for redo.
317 ///
318 /// # Examples
319 ///
320 /// ```
321 /// use iced_code_editor::{Language, Translations};
322 ///
323 /// let en = Translations::new(Language::English);
324 /// assert_eq!(en.context_menu_redo(), "Redo");
325 ///
326 /// let fr = Translations::new(Language::French);
327 /// assert_eq!(fr.context_menu_redo(), "Rétablir");
328 /// ```
329 #[must_use]
330 pub fn context_menu_redo(&self) -> String {
331 rust_i18n::t!("context_menu.redo", locale = self.language.to_locale())
332 .into_owned()
333 }
334
335 /// Returns the context-menu label for cut.
336 ///
337 /// # Examples
338 ///
339 /// ```
340 /// use iced_code_editor::{Language, Translations};
341 ///
342 /// let en = Translations::new(Language::English);
343 /// assert_eq!(en.context_menu_cut(), "Cut");
344 ///
345 /// let de = Translations::new(Language::German);
346 /// assert_eq!(de.context_menu_cut(), "Ausschneiden");
347 /// ```
348 #[must_use]
349 pub fn context_menu_cut(&self) -> String {
350 rust_i18n::t!("context_menu.cut", locale = self.language.to_locale())
351 .into_owned()
352 }
353
354 /// Returns the context-menu label for copy.
355 ///
356 /// # Examples
357 ///
358 /// ```
359 /// use iced_code_editor::{Language, Translations};
360 ///
361 /// let en = Translations::new(Language::English);
362 /// assert_eq!(en.context_menu_copy(), "Copy");
363 ///
364 /// let it = Translations::new(Language::Italian);
365 /// assert_eq!(it.context_menu_copy(), "Copia");
366 /// ```
367 #[must_use]
368 pub fn context_menu_copy(&self) -> String {
369 rust_i18n::t!("context_menu.copy", locale = self.language.to_locale())
370 .into_owned()
371 }
372
373 /// Returns the context-menu label for paste.
374 ///
375 /// # Examples
376 ///
377 /// ```
378 /// use iced_code_editor::{Language, Translations};
379 ///
380 /// let en = Translations::new(Language::English);
381 /// assert_eq!(en.context_menu_paste(), "Paste");
382 ///
383 /// let es = Translations::new(Language::Spanish);
384 /// assert_eq!(es.context_menu_paste(), "Pegar");
385 /// ```
386 #[must_use]
387 pub fn context_menu_paste(&self) -> String {
388 rust_i18n::t!("context_menu.paste", locale = self.language.to_locale())
389 .into_owned()
390 }
391
392 /// Returns the context-menu label for select all.
393 ///
394 /// # Examples
395 ///
396 /// ```
397 /// use iced_code_editor::{Language, Translations};
398 ///
399 /// let en = Translations::new(Language::English);
400 /// assert_eq!(en.context_menu_select_all(), "Select All");
401 ///
402 /// let fr = Translations::new(Language::French);
403 /// assert_eq!(fr.context_menu_select_all(), "Tout sélectionner");
404 /// ```
405 #[must_use]
406 pub fn context_menu_select_all(&self) -> String {
407 rust_i18n::t!(
408 "context_menu.select_all",
409 locale = self.language.to_locale()
410 )
411 .into_owned()
412 }
413
414 /// Returns the macOS context-menu label for revealing a file.
415 ///
416 /// Prefer [`Self::context_menu_reveal_in_file_manager`], which picks the
417 /// right one of the three platform wordings for you.
418 ///
419 /// # Examples
420 ///
421 /// ```
422 /// use iced_code_editor::{Language, Translations};
423 ///
424 /// let en = Translations::new(Language::English);
425 /// assert_eq!(en.context_menu_reveal_in_finder(), "Reveal in Finder");
426 /// ```
427 #[must_use]
428 pub fn context_menu_reveal_in_finder(&self) -> String {
429 rust_i18n::t!(
430 "context_menu.reveal_in_finder",
431 locale = self.language.to_locale()
432 )
433 .into_owned()
434 }
435
436 /// Returns the Windows context-menu label for revealing a file.
437 ///
438 /// Prefer [`Self::context_menu_reveal_in_file_manager`], which picks the
439 /// right one of the three platform wordings for you.
440 ///
441 /// # Examples
442 ///
443 /// ```
444 /// use iced_code_editor::{Language, Translations};
445 ///
446 /// let en = Translations::new(Language::English);
447 /// assert_eq!(
448 /// en.context_menu_reveal_in_file_explorer(),
449 /// "Reveal in File Explorer"
450 /// );
451 /// ```
452 #[must_use]
453 pub fn context_menu_reveal_in_file_explorer(&self) -> String {
454 rust_i18n::t!(
455 "context_menu.reveal_in_file_explorer",
456 locale = self.language.to_locale()
457 )
458 .into_owned()
459 }
460
461 /// Returns the Linux context-menu label for opening a file's parent folder.
462 ///
463 /// Worded as "open the containing folder" rather than "reveal", matching
464 /// how Linux file managers describe the action.
465 ///
466 /// # Examples
467 ///
468 /// ```
469 /// use iced_code_editor::{Language, Translations};
470 ///
471 /// let en = Translations::new(Language::English);
472 /// assert_eq!(
473 /// en.context_menu_open_containing_folder(),
474 /// "Open Containing Folder"
475 /// );
476 /// ```
477 #[must_use]
478 pub fn context_menu_open_containing_folder(&self) -> String {
479 rust_i18n::t!(
480 "context_menu.open_containing_folder",
481 locale = self.language.to_locale()
482 )
483 .into_owned()
484 }
485
486 /// Returns the platform-appropriate label for revealing a file.
487 ///
488 /// Resolves to "Reveal in Finder" on macOS, "Reveal in File Explorer" on
489 /// Windows, and "Open Containing Folder" elsewhere, so the menu matches
490 /// the wording users already know from their own file manager.
491 ///
492 /// # Examples
493 ///
494 /// ```
495 /// use iced_code_editor::{Language, Translations};
496 ///
497 /// let en = Translations::new(Language::English);
498 /// let label = en.context_menu_reveal_in_file_manager();
499 ///
500 /// // Whichever platform this runs on, it matches that platform's wording.
501 /// #[cfg(target_os = "macos")]
502 /// assert_eq!(label, en.context_menu_reveal_in_finder());
503 /// #[cfg(target_os = "windows")]
504 /// assert_eq!(label, en.context_menu_reveal_in_file_explorer());
505 /// #[cfg(not(any(target_os = "macos", target_os = "windows")))]
506 /// assert_eq!(label, en.context_menu_open_containing_folder());
507 /// ```
508 #[must_use]
509 pub fn context_menu_reveal_in_file_manager(&self) -> String {
510 if cfg!(target_os = "macos") {
511 self.context_menu_reveal_in_finder()
512 } else if cfg!(target_os = "windows") {
513 self.context_menu_reveal_in_file_explorer()
514 } else {
515 self.context_menu_open_containing_folder()
516 }
517 }
518 /// Returns the command-palette label for the filter input's placeholder.
519 ///
520 /// # Examples
521 ///
522 /// ```
523 /// use iced_code_editor::{Language, Translations};
524 ///
525 /// let en = Translations::new(Language::English);
526 /// assert_eq!(en.command_palette_placeholder(), "Type a command...");
527 /// ```
528 #[must_use]
529 pub fn command_palette_placeholder(&self) -> String {
530 rust_i18n::t!(
531 "command_palette.placeholder",
532 locale = self.language.to_locale()
533 )
534 .into_owned()
535 }
536
537 /// Returns the command-palette label for the empty-result message.
538 ///
539 /// # Examples
540 ///
541 /// ```
542 /// use iced_code_editor::{Language, Translations};
543 ///
544 /// let en = Translations::new(Language::English);
545 /// assert_eq!(en.command_palette_no_results(), "No matching command");
546 /// ```
547 #[must_use]
548 pub fn command_palette_no_results(&self) -> String {
549 rust_i18n::t!(
550 "command_palette.no_results",
551 locale = self.language.to_locale()
552 )
553 .into_owned()
554 }
555
556 /// Returns the command-palette label for saving the document.
557 ///
558 /// # Examples
559 ///
560 /// ```
561 /// use iced_code_editor::{Language, Translations};
562 ///
563 /// let en = Translations::new(Language::English);
564 /// assert_eq!(en.command_palette_save(), "Save");
565 /// ```
566 #[must_use]
567 pub fn command_palette_save(&self) -> String {
568 rust_i18n::t!(
569 "command_palette.save",
570 locale = self.language.to_locale()
571 )
572 .into_owned()
573 }
574
575 /// Returns the command-palette label for toggling the line comment.
576 ///
577 /// # Examples
578 ///
579 /// ```
580 /// use iced_code_editor::{Language, Translations};
581 ///
582 /// let en = Translations::new(Language::English);
583 /// assert_eq!(en.command_palette_toggle_comment(), "Toggle Line Comment");
584 /// ```
585 #[must_use]
586 pub fn command_palette_toggle_comment(&self) -> String {
587 rust_i18n::t!(
588 "command_palette.toggle_comment",
589 locale = self.language.to_locale()
590 )
591 .into_owned()
592 }
593
594 /// Returns the command-palette label for moving the current line up.
595 ///
596 /// # Examples
597 ///
598 /// ```
599 /// use iced_code_editor::{Language, Translations};
600 ///
601 /// let en = Translations::new(Language::English);
602 /// assert_eq!(en.command_palette_move_line_up(), "Move Line Up");
603 /// ```
604 #[must_use]
605 pub fn command_palette_move_line_up(&self) -> String {
606 rust_i18n::t!(
607 "command_palette.move_line_up",
608 locale = self.language.to_locale()
609 )
610 .into_owned()
611 }
612
613 /// Returns the command-palette label for moving the current line down.
614 ///
615 /// # Examples
616 ///
617 /// ```
618 /// use iced_code_editor::{Language, Translations};
619 ///
620 /// let en = Translations::new(Language::English);
621 /// assert_eq!(en.command_palette_move_line_down(), "Move Line Down");
622 /// ```
623 #[must_use]
624 pub fn command_palette_move_line_down(&self) -> String {
625 rust_i18n::t!(
626 "command_palette.move_line_down",
627 locale = self.language.to_locale()
628 )
629 .into_owned()
630 }
631
632 /// Returns the command-palette label for duplicating the current line above.
633 ///
634 /// # Examples
635 ///
636 /// ```
637 /// use iced_code_editor::{Language, Translations};
638 ///
639 /// let en = Translations::new(Language::English);
640 /// assert_eq!(en.command_palette_duplicate_line_up(), "Duplicate Line Up");
641 /// ```
642 #[must_use]
643 pub fn command_palette_duplicate_line_up(&self) -> String {
644 rust_i18n::t!(
645 "command_palette.duplicate_line_up",
646 locale = self.language.to_locale()
647 )
648 .into_owned()
649 }
650
651 /// Returns the command-palette label for duplicating the current line below.
652 ///
653 /// # Examples
654 ///
655 /// ```
656 /// use iced_code_editor::{Language, Translations};
657 ///
658 /// let en = Translations::new(Language::English);
659 /// assert_eq!(en.command_palette_duplicate_line_down(), "Duplicate Line Down");
660 /// ```
661 #[must_use]
662 pub fn command_palette_duplicate_line_down(&self) -> String {
663 rust_i18n::t!(
664 "command_palette.duplicate_line_down",
665 locale = self.language.to_locale()
666 )
667 .into_owned()
668 }
669
670 /// Returns the command-palette label for jumping to a line.
671 ///
672 /// # Examples
673 ///
674 /// ```
675 /// use iced_code_editor::{Language, Translations};
676 ///
677 /// let en = Translations::new(Language::English);
678 /// assert_eq!(en.command_palette_goto_line(), "Go to Line");
679 /// ```
680 #[must_use]
681 pub fn command_palette_goto_line(&self) -> String {
682 rust_i18n::t!(
683 "command_palette.goto_line",
684 locale = self.language.to_locale()
685 )
686 .into_owned()
687 }
688
689 /// Returns the command-palette label for adding a cursor on the line above.
690 ///
691 /// # Examples
692 ///
693 /// ```
694 /// use iced_code_editor::{Language, Translations};
695 ///
696 /// let en = Translations::new(Language::English);
697 /// assert_eq!(en.command_palette_add_cursor_above(), "Add Cursor Above");
698 /// ```
699 #[must_use]
700 pub fn command_palette_add_cursor_above(&self) -> String {
701 rust_i18n::t!(
702 "command_palette.add_cursor_above",
703 locale = self.language.to_locale()
704 )
705 .into_owned()
706 }
707
708 /// Returns the command-palette label for adding a cursor on the line below.
709 ///
710 /// # Examples
711 ///
712 /// ```
713 /// use iced_code_editor::{Language, Translations};
714 ///
715 /// let en = Translations::new(Language::English);
716 /// assert_eq!(en.command_palette_add_cursor_below(), "Add Cursor Below");
717 /// ```
718 #[must_use]
719 pub fn command_palette_add_cursor_below(&self) -> String {
720 rust_i18n::t!(
721 "command_palette.add_cursor_below",
722 locale = self.language.to_locale()
723 )
724 .into_owned()
725 }
726
727 /// Returns the command-palette label for selecting the next occurrence.
728 ///
729 /// # Examples
730 ///
731 /// ```
732 /// use iced_code_editor::{Language, Translations};
733 ///
734 /// let en = Translations::new(Language::English);
735 /// assert_eq!(en.command_palette_select_next_occurrence(), "Select Next Occurrence");
736 /// ```
737 #[must_use]
738 pub fn command_palette_select_next_occurrence(&self) -> String {
739 rust_i18n::t!(
740 "command_palette.select_next_occurrence",
741 locale = self.language.to_locale()
742 )
743 .into_owned()
744 }
745
746 /// Returns the command-palette label for toggling Vim mode.
747 ///
748 /// # Examples
749 ///
750 /// ```
751 /// use iced_code_editor::{Language, Translations};
752 ///
753 /// let en = Translations::new(Language::English);
754 /// assert_eq!(en.command_palette_toggle_vim_mode(), "Toggle Vim Mode");
755 /// ```
756 #[must_use]
757 pub fn command_palette_toggle_vim_mode(&self) -> String {
758 rust_i18n::t!(
759 "command_palette.toggle_vim_mode",
760 locale = self.language.to_locale()
761 )
762 .into_owned()
763 }
764
765 /// Returns the command-palette label for opening the search dialog.
766 ///
767 /// # Examples
768 ///
769 /// ```
770 /// use iced_code_editor::{Language, Translations};
771 ///
772 /// let en = Translations::new(Language::English);
773 /// assert_eq!(en.command_palette_find(), "Find");
774 /// ```
775 #[must_use]
776 pub fn command_palette_find(&self) -> String {
777 rust_i18n::t!(
778 "command_palette.find",
779 locale = self.language.to_locale()
780 )
781 .into_owned()
782 }
783
784 /// Returns the command-palette label for opening the search-and-replace dialog.
785 ///
786 /// # Examples
787 ///
788 /// ```
789 /// use iced_code_editor::{Language, Translations};
790 ///
791 /// let en = Translations::new(Language::English);
792 /// assert_eq!(en.command_palette_replace(), "Replace");
793 /// ```
794 #[must_use]
795 pub fn command_palette_replace(&self) -> String {
796 rust_i18n::t!(
797 "command_palette.replace",
798 locale = self.language.to_locale()
799 )
800 .into_owned()
801 }
802
803 /// Returns the command-palette label for folding the block at the cursor.
804 ///
805 /// # Examples
806 ///
807 /// ```
808 /// use iced_code_editor::{Language, Translations};
809 ///
810 /// let en = Translations::new(Language::English);
811 /// assert_eq!(en.command_palette_fold_at_cursor(), "Toggle Fold at Cursor");
812 /// ```
813 #[must_use]
814 pub fn command_palette_fold_at_cursor(&self) -> String {
815 rust_i18n::t!(
816 "command_palette.fold_at_cursor",
817 locale = self.language.to_locale()
818 )
819 .into_owned()
820 }
821
822 /// Returns the command-palette label for folding every block.
823 ///
824 /// # Examples
825 ///
826 /// ```
827 /// use iced_code_editor::{Language, Translations};
828 ///
829 /// let en = Translations::new(Language::English);
830 /// assert_eq!(en.command_palette_fold_all(), "Fold All");
831 /// ```
832 #[must_use]
833 pub fn command_palette_fold_all(&self) -> String {
834 rust_i18n::t!(
835 "command_palette.fold_all",
836 locale = self.language.to_locale()
837 )
838 .into_owned()
839 }
840
841 /// Returns the command-palette label for unfolding every block.
842 ///
843 /// # Examples
844 ///
845 /// ```
846 /// use iced_code_editor::{Language, Translations};
847 ///
848 /// let en = Translations::new(Language::English);
849 /// assert_eq!(en.command_palette_unfold_all(), "Unfold All");
850 /// ```
851 #[must_use]
852 pub fn command_palette_unfold_all(&self) -> String {
853 rust_i18n::t!(
854 "command_palette.unfold_all",
855 locale = self.language.to_locale()
856 )
857 .into_owned()
858 }
859}
860
861#[cfg(test)]
862mod tests {
863 use std::collections::BTreeSet;
864
865 use super::*;
866
867 /// Every locale file, paired with the name used in failure messages.
868 ///
869 /// English comes first: it is the reference every other file is compared
870 /// against, and the fallback `rust_i18n` resolves a missing key through.
871 const LOCALE_FILES: [(&str, &str); 8] = [
872 ("en", include_str!("../locales/en.yml")),
873 ("fr", include_str!("../locales/fr.yml")),
874 ("es", include_str!("../locales/es.yml")),
875 ("de", include_str!("../locales/de.yml")),
876 ("it", include_str!("../locales/it.yml")),
877 ("pt-BR", include_str!("../locales/pt-BR.yml")),
878 ("pt-PT", include_str!("../locales/pt-PT.yml")),
879 ("zh-CN", include_str!("../locales/zh-CN.yml")),
880 ];
881
882 /// Collects the `section.key` paths a locale file defines.
883 ///
884 /// Deliberately a five-line reader rather than a YAML dependency: the
885 /// files are a flat list of `section:` headers with two-space-indented
886 /// entries under them, and reading them structurally is the whole point —
887 /// going through `rust_i18n` is exactly what cannot see a missing key.
888 fn locale_keys(yaml: &str) -> BTreeSet<String> {
889 let mut keys = BTreeSet::new();
890 let mut section = String::new();
891
892 for line in yaml.lines() {
893 let line = line.trim_end();
894 if line.trim_start().is_empty()
895 || line.trim_start().starts_with('#')
896 {
897 continue;
898 }
899
900 match line.strip_prefix(" ") {
901 // Indented: an entry belonging to the current section.
902 Some(entry) => {
903 if let Some((name, _)) = entry.split_once(':') {
904 keys.insert(format!("{section}.{}", name.trim()));
905 }
906 }
907 // Flush left: a new section header.
908 None => {
909 if let Some((name, _)) = line.split_once(':') {
910 section = name.trim().to_string();
911 }
912 }
913 }
914 }
915
916 keys
917 }
918
919 #[test]
920 fn test_locale_keys_reads_the_section_and_entry_layout() {
921 // Guards the guard: a reader that silently returned nothing would make
922 // every comparison below pass on an empty set.
923 let keys = locale_keys(
924 "# a comment\nsearch:\n placeholder: \"Search...\"\n\nreplace:\n all_tooltip: \"Replace all matches\"\n",
925 );
926
927 assert_eq!(
928 keys.iter().map(String::as_str).collect::<Vec<_>>(),
929 ["replace.all_tooltip", "search.placeholder"]
930 );
931 }
932
933 #[test]
934 fn test_every_locale_defines_exactly_the_keys_english_defines() {
935 // The structural check the runtime ones cannot perform: with
936 // `fallback = "en"`, a key present in `en.yml` and missing from
937 // `fr.yml` resolves to the *English* string, so no assertion made
938 // through `Translations` can tell the two apart. Comparing the files
939 // themselves is what catches a translator skipping an entry.
940 let (_, english_source) = LOCALE_FILES[0];
941 let english = locale_keys(english_source);
942 assert!(
943 english.contains("command_palette.fold_all"),
944 "en.yml did not parse as expected"
945 );
946
947 for (name, source) in LOCALE_FILES {
948 let keys = locale_keys(source);
949 let missing: Vec<&String> = english.difference(&keys).collect();
950 let extra: Vec<&String> = keys.difference(&english).collect();
951
952 assert!(missing.is_empty(), "{name}.yml is missing {missing:?}");
953 assert!(
954 extra.is_empty(),
955 "{name}.yml defines {extra:?}, which en.yml does not"
956 );
957 }
958 }
959
960 #[test]
961 fn test_default_language() {
962 let translations = Translations::default();
963 assert_eq!(translations.language(), Language::English);
964 }
965
966 #[test]
967 fn test_new_with_language() {
968 let translations = Translations::new(Language::French);
969 assert_eq!(translations.language(), Language::French);
970 }
971
972 #[test]
973 fn test_set_language() {
974 let mut translations = Translations::new(Language::English);
975 translations.set_language(Language::Spanish);
976 assert_eq!(translations.language(), Language::Spanish);
977 }
978
979 #[test]
980 fn test_english_translations() {
981 let t = Translations::new(Language::English);
982 assert_eq!(t.search_placeholder(), "Search...");
983 assert_eq!(t.replace_placeholder(), "Replace...");
984 assert_eq!(t.case_sensitive_label(), "Case sensitive");
985 assert_eq!(t.previous_match_tooltip(), "Previous match (Shift+F3)");
986 assert_eq!(t.next_match_tooltip(), "Next match (F3 / Enter)");
987 assert_eq!(t.close_search_tooltip(), "Close search dialog (Esc)");
988 assert_eq!(t.replace_current_tooltip(), "Replace current match");
989 assert_eq!(t.replace_all_tooltip(), "Replace all matches");
990 }
991
992 #[test]
993 fn test_french_translations() {
994 let t = Translations::new(Language::French);
995 assert_eq!(t.search_placeholder(), "Rechercher...");
996 assert_eq!(t.replace_placeholder(), "Remplacer...");
997 assert_eq!(t.case_sensitive_label(), "Sensible à la casse");
998 assert_eq!(t.previous_match_tooltip(), "Résultat précédent (Maj+F3)");
999 assert_eq!(t.next_match_tooltip(), "Résultat suivant (F3 / Entrée)");
1000 assert_eq!(t.close_search_tooltip(), "Fermer la recherche (Échap)");
1001 assert_eq!(
1002 t.replace_current_tooltip(),
1003 "Remplacer l'occurrence actuelle"
1004 );
1005 assert_eq!(t.replace_all_tooltip(), "Tout remplacer");
1006 }
1007
1008 #[test]
1009 fn test_spanish_translations() {
1010 let t = Translations::new(Language::Spanish);
1011 assert_eq!(t.search_placeholder(), "Buscar...");
1012 assert_eq!(t.replace_placeholder(), "Reemplazar...");
1013 assert_eq!(t.case_sensitive_label(), "Distinguir mayúsculas");
1014 assert_eq!(
1015 t.previous_match_tooltip(),
1016 "Coincidencia anterior (Mayús+F3)"
1017 );
1018 assert_eq!(
1019 t.next_match_tooltip(),
1020 "Siguiente coincidencia (F3 / Enter)"
1021 );
1022 assert_eq!(t.close_search_tooltip(), "Cerrar búsqueda (Esc)");
1023 assert_eq!(
1024 t.replace_current_tooltip(),
1025 "Reemplazar coincidencia actual"
1026 );
1027 assert_eq!(t.replace_all_tooltip(), "Reemplazar todo");
1028 }
1029
1030 #[test]
1031 fn test_german_translations() {
1032 let t = Translations::new(Language::German);
1033 assert_eq!(t.search_placeholder(), "Suchen...");
1034 assert_eq!(t.replace_placeholder(), "Ersetzen...");
1035 assert_eq!(t.case_sensitive_label(), "Groß-/Kleinschreibung");
1036 assert_eq!(
1037 t.previous_match_tooltip(),
1038 "Vorheriger Treffer (Umschalt+F3)"
1039 );
1040 assert_eq!(t.next_match_tooltip(), "Nächster Treffer (F3 / Enter)");
1041 assert_eq!(t.close_search_tooltip(), "Suchdialog schließen (Esc)");
1042 assert_eq!(t.replace_current_tooltip(), "Aktuellen Treffer ersetzen");
1043 assert_eq!(t.replace_all_tooltip(), "Alle ersetzen");
1044 }
1045
1046 #[test]
1047 fn test_italian_translations() {
1048 let t = Translations::new(Language::Italian);
1049 assert_eq!(t.search_placeholder(), "Cerca...");
1050 assert_eq!(t.replace_placeholder(), "Sostituisci...");
1051 assert_eq!(t.case_sensitive_label(), "Distingui maiuscole");
1052 assert_eq!(
1053 t.previous_match_tooltip(),
1054 "Risultato precedente (Maiusc+F3)"
1055 );
1056 assert_eq!(t.next_match_tooltip(), "Risultato successivo (F3 / Invio)");
1057 assert_eq!(
1058 t.close_search_tooltip(),
1059 "Chiudi finestra di ricerca (Esc)"
1060 );
1061 assert_eq!(
1062 t.replace_current_tooltip(),
1063 "Sostituisci risultato corrente"
1064 );
1065 assert_eq!(t.replace_all_tooltip(), "Sostituisci tutto");
1066 }
1067
1068 #[test]
1069 fn test_portuguese_br_translations() {
1070 let t = Translations::new(Language::PortugueseBR);
1071 assert_eq!(t.search_placeholder(), "Pesquisar...");
1072 assert_eq!(t.replace_placeholder(), "Substituir...");
1073 assert_eq!(t.case_sensitive_label(), "Diferenciar maiúsculas");
1074 assert_eq!(
1075 t.previous_match_tooltip(),
1076 "Correspondência anterior (Shift+F3)"
1077 );
1078 assert_eq!(
1079 t.next_match_tooltip(),
1080 "Próxima correspondência (F3 / Enter)"
1081 );
1082 assert_eq!(
1083 t.close_search_tooltip(),
1084 "Fechar diálogo de pesquisa (Esc)"
1085 );
1086 assert_eq!(
1087 t.replace_current_tooltip(),
1088 "Substituir correspondência atual"
1089 );
1090 assert_eq!(t.replace_all_tooltip(), "Substituir tudo");
1091 }
1092
1093 #[test]
1094 fn test_portuguese_pt_translations() {
1095 let t = Translations::new(Language::PortuguesePT);
1096 assert_eq!(t.search_placeholder(), "Pesquisar...");
1097 assert_eq!(t.replace_placeholder(), "Substituir...");
1098 assert_eq!(t.case_sensitive_label(), "Diferenciar maiúsculas");
1099 assert_eq!(
1100 t.previous_match_tooltip(),
1101 "Correspondência anterior (Shift+F3)"
1102 );
1103 assert_eq!(
1104 t.next_match_tooltip(),
1105 "Próxima correspondência (F3 / Enter)"
1106 );
1107 assert_eq!(
1108 t.close_search_tooltip(),
1109 "Fechar diálogo de pesquisa (Esc)"
1110 );
1111 assert_eq!(
1112 t.replace_current_tooltip(),
1113 "Substituir correspondência actual"
1114 );
1115 assert_eq!(t.replace_all_tooltip(), "Substituir tudo");
1116 }
1117
1118 #[test]
1119 fn test_language_switching() {
1120 let mut t = Translations::new(Language::English);
1121 assert_eq!(t.search_placeholder(), "Search...");
1122
1123 t.set_language(Language::French);
1124 assert_eq!(t.search_placeholder(), "Rechercher...");
1125
1126 t.set_language(Language::Spanish);
1127 assert_eq!(t.search_placeholder(), "Buscar...");
1128
1129 t.set_language(Language::German);
1130 assert_eq!(t.search_placeholder(), "Suchen...");
1131
1132 t.set_language(Language::Italian);
1133 assert_eq!(t.search_placeholder(), "Cerca...");
1134
1135 t.set_language(Language::PortugueseBR);
1136 assert_eq!(t.search_placeholder(), "Pesquisar...");
1137
1138 t.set_language(Language::PortuguesePT);
1139 assert_eq!(t.search_placeholder(), "Pesquisar...");
1140
1141 t.set_language(Language::ChineseSimplified);
1142 assert_eq!(t.search_placeholder(), "搜索...");
1143 }
1144
1145 #[test]
1146 fn test_context_menu_translations_cover_all_locales() {
1147 let cases = [
1148 (
1149 Language::English,
1150 [
1151 "Undo",
1152 "Redo",
1153 "Cut",
1154 "Copy",
1155 "Paste",
1156 "Select All",
1157 "Reveal in Finder",
1158 "Reveal in File Explorer",
1159 "Open Containing Folder",
1160 ],
1161 ),
1162 (
1163 Language::French,
1164 [
1165 "Annuler",
1166 "Rétablir",
1167 "Couper",
1168 "Copier",
1169 "Coller",
1170 "Tout sélectionner",
1171 "Révéler dans le Finder",
1172 "Afficher dans l'Explorateur de fichiers",
1173 "Ouvrir le dossier contenant",
1174 ],
1175 ),
1176 (
1177 Language::Spanish,
1178 [
1179 "Deshacer",
1180 "Rehacer",
1181 "Cortar",
1182 "Copiar",
1183 "Pegar",
1184 "Seleccionar todo",
1185 "Mostrar en Finder",
1186 "Mostrar en el Explorador de archivos",
1187 "Abrir carpeta contenedora",
1188 ],
1189 ),
1190 (
1191 Language::German,
1192 [
1193 "Rückgängig",
1194 "Wiederholen",
1195 "Ausschneiden",
1196 "Kopieren",
1197 "Einfügen",
1198 "Alle auswählen",
1199 "Im Finder anzeigen",
1200 "Im Datei-Explorer anzeigen",
1201 "Übergeordneten Ordner öffnen",
1202 ],
1203 ),
1204 (
1205 Language::Italian,
1206 [
1207 "Annulla azione",
1208 "Ripeti",
1209 "Taglia",
1210 "Copia",
1211 "Incolla",
1212 "Seleziona tutto",
1213 "Visualizza in Finder",
1214 "Visualizza in Esplora file",
1215 "Apri cartella superiore",
1216 ],
1217 ),
1218 (
1219 Language::PortugueseBR,
1220 [
1221 "Desfazer",
1222 "Refazer",
1223 "Recortar",
1224 "Copiar",
1225 "Colar",
1226 "Selecionar Tudo",
1227 "Revelar no Finder",
1228 "Revelar no Explorador de Arquivos",
1229 "Abrir a Pasta Que Contém",
1230 ],
1231 ),
1232 (
1233 Language::PortuguesePT,
1234 [
1235 "Anular",
1236 "Refazer",
1237 "Cortar",
1238 "Copiar",
1239 "Colar",
1240 "Selecionar tudo",
1241 "Mostrar no Finder",
1242 "Mostrar no Explorador de Ficheiros",
1243 "Abrir pasta contentora",
1244 ],
1245 ),
1246 (
1247 Language::ChineseSimplified,
1248 [
1249 "撤消",
1250 "恢复",
1251 "剪切",
1252 "复制",
1253 "粘贴",
1254 "选择全部",
1255 "在访达中显示",
1256 "在文件资源管理器中显示",
1257 "打开所在的文件夹",
1258 ],
1259 ),
1260 ];
1261
1262 for (language, expected) in cases {
1263 let translations = Translations::new(language);
1264 assert_eq!(translations.context_menu_undo(), expected[0]);
1265 assert_eq!(translations.context_menu_redo(), expected[1]);
1266 assert_eq!(translations.context_menu_cut(), expected[2]);
1267 assert_eq!(translations.context_menu_copy(), expected[3]);
1268 assert_eq!(translations.context_menu_paste(), expected[4]);
1269 assert_eq!(translations.context_menu_select_all(), expected[5]);
1270 assert_eq!(
1271 translations.context_menu_reveal_in_finder(),
1272 expected[6]
1273 );
1274 assert_eq!(
1275 translations.context_menu_reveal_in_file_explorer(),
1276 expected[7]
1277 );
1278 assert_eq!(
1279 translations.context_menu_open_containing_folder(),
1280 expected[8]
1281 );
1282 }
1283 }
1284
1285 #[test]
1286 fn test_every_command_palette_accessor_resolves_to_a_defined_key() {
1287 // What this can catch: a typo in an accessor's `t!` key path. rust-i18n
1288 // echoes the path back when it resolves to nothing, so the label comes
1289 // out starting with "command_palette.".
1290 //
1291 // What it cannot: a key missing from one locale. `fallback = "en"`
1292 // makes that resolve to the English string, which is neither empty nor
1293 // a key path. Locale completeness is checked structurally instead, by
1294 // `test_every_locale_defines_exactly_the_keys_english_defines`; the
1295 // loop over all eight languages here is what proves every accessor is
1296 // reachable in each of them, not that each has its own wording.
1297 let languages = [
1298 Language::English,
1299 Language::French,
1300 Language::Spanish,
1301 Language::German,
1302 Language::Italian,
1303 Language::PortugueseBR,
1304 Language::PortuguesePT,
1305 Language::ChineseSimplified,
1306 ];
1307
1308 for language in languages {
1309 let t = Translations::new(language);
1310 let labels = [
1311 t.command_palette_placeholder(),
1312 t.command_palette_no_results(),
1313 t.command_palette_save(),
1314 t.command_palette_toggle_comment(),
1315 t.command_palette_move_line_up(),
1316 t.command_palette_move_line_down(),
1317 t.command_palette_duplicate_line_up(),
1318 t.command_palette_duplicate_line_down(),
1319 t.command_palette_goto_line(),
1320 t.command_palette_add_cursor_above(),
1321 t.command_palette_add_cursor_below(),
1322 t.command_palette_select_next_occurrence(),
1323 t.command_palette_toggle_vim_mode(),
1324 t.command_palette_find(),
1325 t.command_palette_replace(),
1326 t.command_palette_fold_at_cursor(),
1327 t.command_palette_fold_all(),
1328 t.command_palette_unfold_all(),
1329 ];
1330
1331 for label in labels {
1332 assert!(!label.is_empty(), "empty label for {language:?}");
1333 assert!(
1334 !label.starts_with("command_palette."),
1335 "missing translation {label} for {language:?}"
1336 );
1337 }
1338 }
1339 }
1340
1341 #[test]
1342 fn test_command_palette_labels_are_localized() {
1343 let cases = [
1344 (Language::English, "Fold All", "Go to Line"),
1345 (Language::French, "Tout replier", "Aller à la ligne"),
1346 (Language::Spanish, "Plegar todo", "Ir a la línea"),
1347 (Language::German, "Alles falten", "Gehe zu Zeile"),
1348 (Language::Italian, "Comprimi tutto", "Vai alla riga"),
1349 (Language::PortugueseBR, "Dobrar tudo", "Ir para a linha"),
1350 (Language::PortuguesePT, "Dobrar tudo", "Ir para a linha"),
1351 (Language::ChineseSimplified, "全部折叠", "转到行"),
1352 ];
1353
1354 for (language, fold_all, goto_line) in cases {
1355 let t = Translations::new(language);
1356 assert_eq!(t.command_palette_fold_all(), fold_all);
1357 assert_eq!(t.command_palette_goto_line(), goto_line);
1358 }
1359 }
1360}