| 1 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
| 2 | // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 |
| 3 | |
| 4 | |
| 5 | //bundle-translations |
| 6 | |
| 7 | export component TestCase inherits Window { |
| 8 | property <int> int_value: 42; |
| 9 | |
| 10 | out property <string> t1: @tr("Hello World{{}}." ); |
| 11 | out property <string> t2: @tr("Hello {}." , "World" ); |
| 12 | out property <string> t3: @tr("{} Hello {}" , int_value, "World" ); |
| 13 | out property <string> t4: @tr("{1} Hello {0}🌍" , @tr("World" ), int_value + 1); |
| 14 | out property <string> t5: @tr("Untranslated string" ); |
| 15 | |
| 16 | property <string> c1: @tr("Context" => "xx{0}xx" , @tr("CC" => "aa" )); |
| 17 | |
| 18 | function make_plural1(xx: int, yy: string) -> string { return @tr("there is one file in my {}" | "there are {n} files in my {}" % xx, yy); } |
| 19 | function make_plural2(xx: int) -> string { return @tr("Ctx=>" => "xx{n}xx" | "yy{n}yy" % xx); } |
| 20 | |
| 21 | out property <string> result1: make_plural1(1, @tr("Plop" )) + "\n" + make_plural1(2, @tr("Flop🎃" )) + "\n" + make_plural1(10, t1); |
| 22 | out property <string> result2: make_plural2(1) + "\n" + make_plural2(-999) + "\n" + make_plural2(0) + "\n" + make_plural2(42); |
| 23 | |
| 24 | out property <bool> test: t1 == "Hello World{}." && t2 == "Hello World." && t3 == "42 Hello World" && t4 == "43 Hello World🌍" && t5 == "Untranslated string" |
| 25 | && c1 == "xxaaxx" |
| 26 | && result1 == "there is one file in my Plop\nthere are 2 files in my Flop🎃\nthere are 10 files in my Hello World{}." |
| 27 | && result2 == "xx1xx\nyy-999yy\nyy0yy\nyy42yy" ; |
| 28 | } |
| 29 | /* |
| 30 | ```cpp |
| 31 | auto handle = TestCase::create(); |
| 32 | const TestCase &instance = *handle; |
| 33 | auto result1 = "there is one file in my Plop\nthere are 2 files in my Flop🎃\nthere are 10 files in my Hello World{}."; |
| 34 | auto result2 = "xx1xx\nyy-999yy\nyy0yy\nyy42yy"; |
| 35 | assert_eq(instance.get_result1(), result1); |
| 36 | assert_eq(instance.get_result2(), result2); |
| 37 | assert_eq(instance.get_t5(), "Untranslated string"); |
| 38 | assert(instance.get_test()); |
| 39 | |
| 40 | assert(!slint::select_bundled_translation("abc")); |
| 41 | assert_eq(instance.get_result1(), result1); |
| 42 | assert_eq(instance.get_result2(), result2); |
| 43 | assert_eq(instance.get_t5(), "Untranslated string"); |
| 44 | assert(instance.get_test()); |
| 45 | |
| 46 | assert(slint::select_bundled_translation("up")); |
| 47 | std::string result1_upper = result1; |
| 48 | std::transform(result1_upper.begin(), result1_upper.end(), result1_upper.begin(), ::toupper); |
| 49 | std::string result2_upper = result2; |
| 50 | std::transform(result2_upper.begin(), result2_upper.end(), result2_upper.begin(), ::toupper); |
| 51 | assert_eq(std::string_view(instance.get_result1()), result1_upper); |
| 52 | assert_eq(std::string_view(instance.get_result2()), result2_upper); |
| 53 | assert_eq(instance.get_t5(), "Untranslated string"); |
| 54 | assert(!instance.get_test()); |
| 55 | |
| 56 | assert(!slint::select_bundled_translation("def")); |
| 57 | assert_eq(std::string_view(instance.get_result1()), result1_upper); |
| 58 | assert_eq(std::string_view(instance.get_result2()), result2_upper); |
| 59 | assert_eq(instance.get_t5(), "Untranslated string"); |
| 60 | assert(!instance.get_test()); |
| 61 | |
| 62 | assert(slint::select_bundled_translation("")); |
| 63 | assert_eq(instance.get_result1(), result1); |
| 64 | assert_eq(instance.get_result2(), result2); |
| 65 | assert_eq(instance.get_t5(), "Untranslated string"); |
| 66 | assert(instance.get_test()); |
| 67 | |
| 68 | assert(slint::select_bundled_translation("fr")); |
| 69 | assert_eq(instance.get_result1(), "Il y a 1 fichier dans mon Plouf\nIl y a 2 fichiers dans mon Floup🍓\nIl y a 10 fichiers dans mon Bonjour Monde{}."); |
| 70 | assert_eq(instance.get_result2(), "rr1rr\nrr-999rr\nrr0rr\nss42ss"); |
| 71 | assert_eq(instance.get_t5(), "Untranslated string"); |
| 72 | assert(!instance.get_test()); |
| 73 | ``` |
| 74 | |
| 75 | |
| 76 | ```rust |
| 77 | let instance = TestCase::new().unwrap(); |
| 78 | let result1 = "there is one file in my Plop\nthere are 2 files in my Flop🎃\nthere are 10 files in my Hello World{}."; |
| 79 | let result2 = "xx1xx\nyy-999yy\nyy0yy\nyy42yy"; |
| 80 | assert_eq!(instance.get_result1(), result1); |
| 81 | assert_eq!(instance.get_result2(), result2); |
| 82 | assert_eq!(instance.get_t5(), "Untranslated string"); |
| 83 | assert!(instance.get_test()); |
| 84 | |
| 85 | assert!(slint::select_bundled_translation("abc").is_err()); |
| 86 | assert_eq!(instance.get_result1(), result1); |
| 87 | assert_eq!(instance.get_result2(), result2); |
| 88 | assert_eq!(instance.get_t5(), "Untranslated string"); |
| 89 | assert!(instance.get_test()); |
| 90 | |
| 91 | assert!(slint::select_bundled_translation("up").is_ok()); |
| 92 | assert_eq!(instance.get_result1(), result1.to_uppercase()); |
| 93 | assert_eq!(instance.get_result2(), result2.to_uppercase()); |
| 94 | assert_eq!(instance.get_t5(), "Untranslated string"); |
| 95 | assert!(!instance.get_test()); |
| 96 | |
| 97 | assert!(slint::select_bundled_translation("def").is_err()); |
| 98 | assert_eq!(instance.get_result1(), result1.to_uppercase()); |
| 99 | assert_eq!(instance.get_result2(), result2.to_uppercase()); |
| 100 | assert_eq!(instance.get_t5(), "Untranslated string"); |
| 101 | assert!(!instance.get_test()); |
| 102 | |
| 103 | assert!(slint::select_bundled_translation("").is_ok()); |
| 104 | assert_eq!(instance.get_result1(), result1); |
| 105 | assert_eq!(instance.get_result2(), result2); |
| 106 | assert_eq!(instance.get_t5(), "Untranslated string"); |
| 107 | assert!(instance.get_test()); |
| 108 | |
| 109 | assert!(slint::select_bundled_translation("fr").is_ok()); |
| 110 | assert_eq!(instance.get_result1(), "Il y a 1 fichier dans mon Plouf\nIl y a 2 fichiers dans mon Floup🍓\nIl y a 10 fichiers dans mon Bonjour Monde{}."); |
| 111 | assert_eq!(instance.get_result2(), "rr1rr\nrr-999rr\nrr0rr\nss42ss"); |
| 112 | assert_eq!(instance.get_t5(), "Untranslated string"); |
| 113 | assert!(!instance.get_test()); |
| 114 | |
| 115 | ``` |
| 116 | |
| 117 | */ |
| 118 | |