tr-0.1.11/.cargo_vcs_info.json0000644000000001400000000000100115340ustar { "git": { "sha1": "0070206fb355917595e76229932ff96da25c9565" }, "path_in_vcs": "tr" }tr-0.1.11/Cargo.lock0000644000000547010000000000100075230ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 4 [[package]] name = "aho-corasick" version = "1.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" dependencies = [ "memchr", ] [[package]] name = "anes" version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" [[package]] name = "anstyle" version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "862ed96ca487e809f1c8e5a8447f6ee2cf102f846893800b20cebdf541fc6bbd" [[package]] name = "autocfg" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" [[package]] name = "block" version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a" [[package]] name = "bumpalo" version = "3.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf" [[package]] name = "byteorder" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "cast" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" [[package]] name = "cc" version = "1.2.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0fc897dc1e865cc67c0e05a836d9d3f1df3cbe442aa4a9473b18e12624a4951" dependencies = [ "shlex", ] [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "ciborium" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e" dependencies = [ "ciborium-io", "ciborium-ll", "serde", ] [[package]] name = "ciborium-io" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757" [[package]] name = "ciborium-ll" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9" dependencies = [ "ciborium-io", "half", ] [[package]] name = "clap" version = "4.5.39" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd60e63e9be68e5fb56422e397cf9baddded06dae1d2e523401542383bc72a9f" dependencies = [ "clap_builder", ] [[package]] name = "clap_builder" version = "4.5.39" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89cc6392a1f72bbeb820d71f32108f61fdaf18bc526e1d23954168a67759ef51" dependencies = [ "anstyle", "clap_lex", ] [[package]] name = "clap_lex" version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" [[package]] name = "criterion" version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3bf7af66b0989381bd0be551bd7cc91912a655a58c6918420c9527b1fd8b4679" dependencies = [ "anes", "cast", "ciborium", "clap", "criterion-plot", "itertools 0.13.0", "num-traits", "oorandom", "plotters", "rayon", "regex", "serde", "serde_json", "tinytemplate", "walkdir", ] [[package]] name = "criterion-plot" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1" dependencies = [ "cast", "itertools 0.10.5", ] [[package]] name = "crossbeam-deque" version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51" dependencies = [ "crossbeam-epoch", "crossbeam-utils", ] [[package]] name = "crossbeam-epoch" version = "0.9.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" dependencies = [ "crossbeam-utils", ] [[package]] name = "crossbeam-utils" version = "0.8.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" [[package]] name = "crunchy" version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43da5946c66ffcc7745f48db692ffbb10a83bfe0afd96235c5c2a4fb23994929" [[package]] name = "either" version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" [[package]] name = "encoding" version = "0.2.33" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b0d943856b990d12d3b55b359144ff341533e516d94098b1d3fc1ac666d36ec" dependencies = [ "encoding-index-japanese", "encoding-index-korean", "encoding-index-simpchinese", "encoding-index-singlebyte", "encoding-index-tradchinese", ] [[package]] name = "encoding-index-japanese" version = "1.20141219.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04e8b2ff42e9a05335dbf8b5c6f7567e5591d0d916ccef4e0b1710d32a0d0c91" dependencies = [ "encoding_index_tests", ] [[package]] name = "encoding-index-korean" version = "1.20141219.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4dc33fb8e6bcba213fe2f14275f0963fd16f0a02c878e3095ecfdf5bee529d81" dependencies = [ "encoding_index_tests", ] [[package]] name = "encoding-index-simpchinese" version = "1.20141219.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d87a7194909b9118fc707194baa434a4e3b0fb6a5a757c73c3adb07aa25031f7" dependencies = [ "encoding_index_tests", ] [[package]] name = "encoding-index-singlebyte" version = "1.20141219.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3351d5acffb224af9ca265f435b859c7c01537c0849754d3db3fdf2bfe2ae84a" dependencies = [ "encoding_index_tests", ] [[package]] name = "encoding-index-tradchinese" version = "1.20141219.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd0e20d5688ce3cab59eb3ef3a2083a5c77bf496cb798dc6fcdb75f323890c18" dependencies = [ "encoding_index_tests", ] [[package]] name = "encoding_index_tests" version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a246d82be1c9d791c5dfde9a2bd045fc3cbba3fa2b11ad558f27d01712f00569" [[package]] name = "gettext" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ebb594e753d5997e4be036e5a8cf048ab9414352870fb45c779557bbc9ba971" dependencies = [ "byteorder", "encoding", ] [[package]] name = "gettext-rs" version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a44e92f7dc08430aca7ed55de161253a22276dfd69c5526e5c5e95d1f7cf338a" dependencies = [ "gettext-sys", "locale_config", ] [[package]] name = "gettext-sys" version = "0.22.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb45773f5b8945f12aecd04558f545964f943dacda1b1155b3d738f5469ef661" dependencies = [ "cc", "temp-dir", ] [[package]] name = "half" version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "459196ed295495a68f7d7fe1d84f6c4b7ff0e21fe3017b2f283c6fac3ad803c9" dependencies = [ "cfg-if", "crunchy", ] [[package]] name = "heck" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" [[package]] name = "itertools" version = "0.10.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" dependencies = [ "either", ] [[package]] name = "itertools" version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" dependencies = [ "either", ] [[package]] name = "itoa" version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" [[package]] name = "js-sys" version = "0.3.77" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f" dependencies = [ "once_cell", "wasm-bindgen", ] [[package]] name = "lazy_static" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" version = "0.2.172" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa" [[package]] name = "locale_config" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08d2c35b16f4483f6c26f0e4e9550717a2f6575bcd6f12a53ff0c490a94a6934" dependencies = [ "lazy_static", "objc", "objc-foundation", "regex", "winapi", ] [[package]] name = "log" version = "0.4.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" [[package]] name = "malloc_buf" version = "0.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb" dependencies = [ "libc", ] [[package]] name = "memchr" version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] name = "natord" version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "308d96db8debc727c3fd9744aac51751243420e46edf401010908da7f8d5e57c" [[package]] name = "num-traits" version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", ] [[package]] name = "objc" version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "915b1b472bc21c53464d6c8461c9d3af805ba1ef837e1cac254428f4a77177b1" dependencies = [ "malloc_buf", ] [[package]] name = "objc-foundation" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1add1b659e36c9607c7aab864a76c7a4c2760cd0cd2e120f3fb8b952c7e22bf9" dependencies = [ "block", "objc", "objc_id", ] [[package]] name = "objc_id" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c92d4ddb4bd7b50d730c215ff871754d0da6b2178849f8a2a2ab69712d0c073b" dependencies = [ "objc", ] [[package]] name = "once_cell" version = "1.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" [[package]] name = "oorandom" version = "11.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e" [[package]] name = "plotters" version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5aeb6f403d7a4911efb1e33402027fc44f29b5bf6def3effcc22d7bb75f2b747" dependencies = [ "num-traits", "plotters-backend", "plotters-svg", "wasm-bindgen", "web-sys", ] [[package]] name = "plotters-backend" version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df42e13c12958a16b3f7f4386b9ab1f3e7933914ecea48da7139435263a4172a" [[package]] name = "plotters-svg" version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51bae2ac328883f7acdfea3d66a7c35751187f870bc81f94563733a154d7a670" dependencies = [ "plotters-backend", ] [[package]] name = "proc-macro2" version = "1.0.95" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" dependencies = [ "unicode-ident", ] [[package]] name = "quote" version = "1.0.40" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" dependencies = [ "proc-macro2", ] [[package]] name = "rayon" version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa" dependencies = [ "either", "rayon-core", ] [[package]] name = "rayon-core" version = "1.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" dependencies = [ "crossbeam-deque", "crossbeam-utils", ] [[package]] name = "regex" version = "1.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" dependencies = [ "aho-corasick", "memchr", "regex-automata", "regex-syntax", ] [[package]] name = "regex-automata" version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" dependencies = [ "aho-corasick", "memchr", "regex-syntax", ] [[package]] name = "regex-syntax" version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" [[package]] name = "rspolib" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f37ad43928575b1f1883fbd0eae846d6bc3c480347b64e724d3f23d38273968" dependencies = [ "lazy_static", "natord", "snafu", "unicode-linebreak", "unicode-width", ] [[package]] name = "rustversion" version = "1.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a0d197bd2c9dc6e53b84da9556a69ba4cdfab8619eb41a8bd1cc2027a0f6b1d" [[package]] name = "ryu" version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" [[package]] name = "same-file" version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" dependencies = [ "winapi-util", ] [[package]] name = "serde" version = "1.0.219" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" version = "1.0.219" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "serde_json" version = "1.0.140" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373" dependencies = [ "itoa", "memchr", "ryu", "serde", ] [[package]] name = "shlex" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" [[package]] name = "snafu" version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "320b01e011bf8d5d7a4a4a4be966d9160968935849c83b918827f6a435e7f627" dependencies = [ "snafu-derive", ] [[package]] name = "snafu-derive" version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1961e2ef424c1424204d3a5d6975f934f56b6d50ff5732382d84ebf460e147f7" dependencies = [ "heck", "proc-macro2", "quote", "syn", ] [[package]] name = "syn" version = "2.0.101" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ce2b7fc941b3a24138a0a7cf8e858bfc6a992e7978a068a5c760deb0ed43caf" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "temp-dir" version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "83176759e9416cf81ee66cb6508dbfe9c96f20b8b56265a39917551c23c70964" [[package]] name = "tinytemplate" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc" dependencies = [ "serde", "serde_json", ] [[package]] name = "tr" version = "0.1.11" dependencies = [ "criterion", "gettext", "gettext-rs", "rspolib", ] [[package]] name = "unicode-ident" version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" [[package]] name = "unicode-linebreak" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b09c83c3c29d37506a3e260c08c03743a6bb66a9cd432c6934ab501a190571f" [[package]] name = "unicode-width" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a1a07cc7db3810833284e8d372ccdc6da29741639ecc70c9ec107df0fa6154c" [[package]] name = "walkdir" version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" dependencies = [ "same-file", "winapi-util", ] [[package]] name = "wasm-bindgen" version = "0.2.100" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5" dependencies = [ "cfg-if", "once_cell", "rustversion", "wasm-bindgen-macro", ] [[package]] name = "wasm-bindgen-backend" version = "0.2.100" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6" dependencies = [ "bumpalo", "log", "proc-macro2", "quote", "syn", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-macro" version = "0.2.100" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407" dependencies = [ "quote", "wasm-bindgen-macro-support", ] [[package]] name = "wasm-bindgen-macro-support" version = "0.2.100" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" dependencies = [ "proc-macro2", "quote", "syn", "wasm-bindgen-backend", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" version = "0.2.100" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d" dependencies = [ "unicode-ident", ] [[package]] name = "web-sys" version = "0.3.77" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "33b6dd2ef9186f1f2072e409e99cd22a975331a6b3591b12c764e0e55c60d5d2" dependencies = [ "js-sys", "wasm-bindgen", ] [[package]] name = "winapi" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" dependencies = [ "winapi-i686-pc-windows-gnu", "winapi-x86_64-pc-windows-gnu", ] [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-util" version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" dependencies = [ "windows-sys", ] [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "windows-sys" version = "0.59.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" dependencies = [ "windows-targets", ] [[package]] name = "windows-targets" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ "windows_aarch64_gnullvm", "windows_aarch64_msvc", "windows_i686_gnu", "windows_i686_gnullvm", "windows_i686_msvc", "windows_x86_64_gnu", "windows_x86_64_gnullvm", "windows_x86_64_msvc", ] [[package]] name = "windows_aarch64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" [[package]] name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" [[package]] name = "windows_i686_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" [[package]] name = "windows_i686_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" [[package]] name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" [[package]] name = "windows_x86_64_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" [[package]] name = "windows_x86_64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" [[package]] name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" tr-0.1.11/Cargo.toml0000644000000031650000000000100075440ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" rust-version = "1.81" name = "tr" version = "0.1.11" authors = ["Olivier Goffart "] build = false autolib = false autobins = false autoexamples = false autotests = false autobenches = false description = "tr! macro for localisation" documentation = "https://docs.rs/tr" readme = "README.md" keywords = [ "internationalization", "translation", "l10n", "i18n", "gettext", ] categories = [ "internationalization", "localization", ] license = "MIT" repository = "https://github.com/woboq/tr" [package.metadata.docs.rs] features = [ "mo-translator", "po-translator", "gettext-rs", ] [features] default = ["gettext-rs"] mo-translator = ["dep:rspolib"] po-translator = ["dep:rspolib"] [lib] name = "tr" path = "src/lib.rs" [[test]] name = "uppercase" path = "tests/uppercase.rs" [[bench]] name = "my_bench" path = "benches/my_bench.rs" harness = false [dependencies.gettext] version = "0.4" optional = true [dependencies.gettext-rs] version = "0.7" features = ["gettext-system"] optional = true [dependencies.rspolib] version = "0.1.1" optional = true [dev-dependencies.criterion] version = "0.6" tr-0.1.11/Cargo.toml.orig000064400000000000000000000015731046102023000132260ustar 00000000000000[package] name = "tr" version = "0.1.11" authors = ["Olivier Goffart "] description = "tr! macro for localisation" license = "MIT" readme = "../README.md" repository = "https://github.com/woboq/tr" documentation = "https://docs.rs/tr" keywords = ["internationalization", "translation", "l10n", "i18n", "gettext"] categories = ["internationalization", "localization"] edition = "2021" rust-version = "1.81" [features] default = ["gettext-rs"] mo-translator = ["dep:rspolib"] po-translator = ["dep:rspolib"] [dependencies] gettext-rs = { version = "0.7", optional = true, features = ["gettext-system"] } gettext = { version = "0.4", optional = true } rspolib = { version = "0.1.1", optional = true } [dev-dependencies] criterion = "0.6" [[bench]] name = "my_bench" harness = false [package.metadata.docs.rs] features = ["mo-translator", "po-translator", "gettext-rs"] tr-0.1.11/README.md000064400000000000000000000120761046102023000116160ustar 00000000000000# Localisation of rust applications [![docs.rs](https://docs.rs/tr/badge.svg)](https://docs.rs/tr) This repository provides tools for localizing Rust applications, making it easier to translate your software to different languages. There are two crates * `tr` is a runtime library wrapping gettext (currently), in order to provide a convenient way to localize an application. * `xtr` is a binary similar to GNU's `xgettext` which extract string from a rust crate. It can extract strings of crate using the `tr` macro from this sibling crate, or using other gettext based localisation crates such as [`gettext-rs`](https://crates.io/crates/gettext-rs), [`gettext`](https://crates.io/crates/gettext), [`rocket_i18n`](https://github.com/BaptisteGelez/rocket_i18n) # How to translate a rust application 1. Annotate the strings in your source code with the write macro/functions. You can use * The `tr!` macro from this `tr` crate (still work in progress), or * The gettext function from the `gettext` or the `gettext-rs` crate 2. Run the `xtr` program over your crate to extract the string in a .pot file 3. Use the GNU gettext tools to merge, translate, and generate the .mo files # About `tr!` * The name comes from Qt's `tr()` function. It is a short name since it will be placed on most string literal. * The macro can do rust-style formatting. This makes it possible to re-order the arguments in the translations. * `Hello {}` or `Hello {0}` or Hello `Hello {name}` works. * Currently, the default backend uses the [`gettext-rs`](https://crates.io/crates/gettext-rs) crate, but this could be changed to [`gettext`](https://crates.io/crates/gettext) in the future. * Plurals are handled by gettext, which support the different plurals forms of several languages. ## Future plans * Validity of the formatting in the original or translation is not done yet, but could be done in the future * More advanced formatting that would allow for gender or case can be done as an extension to the formatting rules. Since the macro takes the arguments directly, it will be possible to extend the formatting engine with a [scripting system](https://techbase.kde.org/Localization/Concepts/Transcript) or something like ICU MessageFormat. * Formatting date/number in a localized fashion. ## Example ```Rust #[macro_use] extern crate tr; fn main() { // use the tr_init macro to tell gettext where to look for translations tr_init!("/usr/share/locale/"); let folder = if let Some(folder) = std::env::args().nth(1) { folder } else { println!("{}", tr!("Please give folder name")); return; }; match std::fs::read_dir(&folder) { Err(e) => { println!("{}", tr!("Could not read directory '{}'\nError: {}", folder, e)); } Ok(r) => { // Singlular/plural formating println!("{}", tr!( "The directory {} has one file" | "The directory {} has {n} files" % r.count(), folder )); } } } ``` # About `xtr` `xtr` is a tool that extract translated strings from the source code of a rust crate. The tool is supposed to be compatible with any gettext based functions. But support for the special syntax of the tr! macro has been added. ## Usage ``` xtr src/main.rs -o example.pot ``` This will extract strings from all the crate's modules and create a file `example.pot`. You can now use the gettext tools to translate this file. ## Differences with `xgettext` `xtr` is basically to be used in place of `xgettext` for Rust code. `xgettext` does not currently support the rust language. We can get decent result using the C language, but: * `xgettext` will not work properly if the code contains comments or string escaping that is not compatible with Rust's rules. (Rules for comments, or string escaping are different in Rust and in C. Think about raw literal, embedded comments, lifetime, ...) `xtr` uses the lexer from the `proc_macro2` crate so it parse rust code. * `xgettext` cannot be told to extract string out of a macro, while `xtr` will ignore the `!` token. So `gettext(...)` or `gettext!(...)` will work. * `xgettext` cannot handle the rust rules within the string literal. `xtr` will have no problem with rust's raw literal or rust's escape sequence. * `xtr` can also parse the `mod` keyword, and easily parse all the files in a crate. * Finally, `xtr` can also parse the more advanced syntax within the `tr!` macro. # Licence * The `tr` crate is licensed under the [MIT](https://opensource.org/licenses/MIT) license. * The `xtr` program is a binary used only for development and is in the [GNU Affero General Public License (AGPL)](https://www.gnu.org/licenses/agpl-3.0.en.html). # Contribution Contributions are welcome. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, should be licensed under the MIT license. ## Request for feedback Please fill your suggestions as issues. Or help by commenting on https://github.com/woboq/tr/issues/1 tr-0.1.11/benches/my_bench.rs000064400000000000000000000025021046102023000140710ustar 00000000000000use criterion::{criterion_group, criterion_main, Criterion}; use std::hint::black_box; use tr::tr; pub fn short_literal(c: &mut Criterion) { c.bench_function("short_literal", |b| { b.iter(|| { tr!("Hello"); }) }); } pub fn long_literal(c: &mut Criterion) { c.bench_function("long_literal", |b| b.iter(|| { tr!("Hello, world! This is a longer sentence but without argument markers. That is all for now, thank you for reading."); })); } pub fn short_argument(c: &mut Criterion) { c.bench_function("short_argument", |b| { b.iter(|| { tr!("Hello {}!", black_box("world")); }) }); } pub fn long_argument(c: &mut Criterion) { c.bench_function("long_argument", |b| { b.iter(|| { tr!( "Hello {} and {} and {} and {} and {} and {} and {} and finally {}!", black_box("Mercury"), black_box("Venus"), black_box("Earth"), black_box("Mars"), black_box("Jupiter"), black_box("Saturn"), black_box("Uranus"), black_box("Neptune"), ); }) }); } criterion_group!( benches, short_literal, long_literal, short_argument, long_argument ); criterion_main!(benches); tr-0.1.11/src/lib.rs000064400000000000000000000505371046102023000122460ustar 00000000000000/* Copyright (C) 2018 Olivier Goffart Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] //! # Internationalisation helper //! //! This crate maily expose a macro that wraps gettext in a convinient ways. //! See the documentation of the [tr! macro](tr!). //! //! To translate a rust crate, simply wrap your string within the [`tr!` macro](tr!). //! One can then use the `xtr` binary to extract all the translatable from a crate in a `.po` //! file. GNU gettext tools can be used to process and translate these strings. //! //! The tr! macro also support support rust-like formating. //! //! Example: //! //! ``` //! #[macro_use] //! extern crate tr; //! fn main() { //! // use the tr_init macro to tell gettext where to look for translations //! # #[cfg(feature = "gettext-rs")] //! tr_init!("/usr/share/locale/"); //! let folder = if let Some(folder) = std::env::args().nth(1) { //! folder //! } else { //! println!("{}", tr!("Please give folder name")); //! return; //! }; //! match std::fs::read_dir(&folder) { //! Err(e) => { //! println!("{}", tr!("Could not read directory '{}'\nError: {}", //! folder, e)); //! } //! Ok(r) => { //! // Singular/plural formating //! println!("{}", tr!( //! "The directory {} has one file" | "The directory {} has {n} files" % r.count(), //! folder //! )); //! } //! } //! } //! ``` //! //! # Optional Features //! //! You can change which crate is used as a backend for the translation by setting the features //! //! - **`gettext-rs`** *(enabled by default)* - This crate wraps the gettext C library //! - **`gettext`** - A rust re-implementation of gettext. That crate does not take care of loading the //! right .mo files, so one must use the [`set_translator!] macro with a //! `gettext::Catalog` object //! //! Additionally, this crate permits loading from `.po` or `.mo` files directly via the [`PoTranslator`] and //! [`MoTranslator`] types, guarded beind the respective **`mo-translator`** and **`po-translator`** features. #[cfg(any(feature = "po-translator", feature = "mo-translator"))] mod rspolib_translator; #[cfg(any(feature = "po-translator", feature = "mo-translator"))] pub use rspolib_translator::MoPoTranslatorLoadError; #[cfg(feature = "mo-translator")] pub use rspolib_translator::MoTranslator; #[cfg(feature = "po-translator")] pub use rspolib_translator::PoTranslator; use std::borrow::Cow; #[doc(hidden)] pub mod runtime_format { //! poor man's dynamic formater. //! //! This module create a simple dynamic formater which replaces '{}' or '{n}' with the //! argument. //! //! This does not use the runtime_fmt crate because it needs nightly compiler //! //! TODO: better error reporting and support for more replacement option /// Converts the result of the runtime_format! macro into the final String pub fn display_string(format_str: &str, args: &[(&str, &dyn ::std::fmt::Display)]) -> String { use ::std::fmt::Write; let fmt_len = format_str.len(); let mut res = String::with_capacity(2 * fmt_len); let mut arg_idx = 0; let mut pos = 0; while let Some(mut p) = format_str[pos..].find(['{', '}']) { if fmt_len - pos < p + 1 { break; } p += pos; // Skip escaped } if format_str.get(p..=p) == Some("}") { res.push_str(&format_str[pos..=p]); if format_str.get(p + 1..=p + 1) == Some("}") { pos = p + 2; } else { // FIXME! this is an error, it should be reported ('}' must be escaped) pos = p + 1; } continue; } // Skip escaped { if format_str.get(p + 1..=p + 1) == Some("{") { res.push_str(&format_str[pos..=p]); pos = p + 2; continue; } // Find the argument let end = if let Some(end) = format_str[p..].find('}') { end + p } else { // FIXME! this is an error, it should be reported res.push_str(&format_str[pos..=p]); pos = p + 1; continue; }; let argument = format_str[p + 1..end].trim(); let pa = if p == end - 1 { arg_idx += 1; arg_idx - 1 } else if let Ok(n) = argument.parse::() { n } else if let Some(p) = args.iter().position(|x| x.0 == argument) { p } else { // FIXME! this is an error, it should be reported res.push_str(&format_str[pos..end]); pos = end; continue; }; // format the part before the '{' res.push_str(&format_str[pos..p]); if let Some(a) = args.get(pa) { write!(&mut res, "{}", a.1) .expect("a Display implementation returned an error unexpectedly"); } else { // FIXME! this is an error, it should be reported res.push_str(&format_str[p..=end]); } pos = end + 1; } res.push_str(&format_str[pos..]); res } #[doc(hidden)] /// runtime_format! macro. See runtime_format module documentation. #[macro_export] macro_rules! runtime_format { ($fmt:expr) => {{ // TODO! check if 'fmt' does not have {} String::from($fmt) }}; ($fmt:expr, $($tail:tt)* ) => {{ $crate::runtime_format::display_string( AsRef::as_ref(&$fmt), $crate::runtime_format!(@parse_args [] $($tail)*), ) }}; (@parse_args [$($args:tt)*]) => { &[ $( $args ),* ] }; (@parse_args [$($args:tt)*] $name:ident) => { $crate::runtime_format!(@parse_args [$($args)* (stringify!($name) , &$name)]) }; (@parse_args [$($args:tt)*] $name:ident, $($tail:tt)*) => { $crate::runtime_format!(@parse_args [$($args)* (stringify!($name) , &$name)] $($tail)*) }; (@parse_args [$($args:tt)*] $name:ident = $e:expr) => { $crate::runtime_format!(@parse_args [$($args)* (stringify!($name) , &$e)]) }; (@parse_args [$($args:tt)*] $name:ident = $e:expr, $($tail:tt)*) => { $crate::runtime_format!(@parse_args [$($args)* (stringify!($name) , &$e)] $($tail)*) }; (@parse_args [$($args:tt)*] $e:expr) => { $crate::runtime_format!(@parse_args [$($args)* ("" , &$e)]) }; (@parse_args [$($args:tt)*] $e:expr, $($tail:tt)*) => { $crate::runtime_format!(@parse_args [$($args)* ("" , &$e)] $($tail)*) }; } #[cfg(test)] mod tests { #[test] fn test_format() { assert_eq!(runtime_format!("Hello"), "Hello"); assert_eq!(runtime_format!("Hello {}!", "world"), "Hello world!"); assert_eq!(runtime_format!("Hello {0}!", "world"), "Hello world!"); assert_eq!( runtime_format!("Hello -{1}- -{0}-", 40 + 5, "World"), "Hello -World- -45-" ); assert_eq!( runtime_format!(format!("Hello {{}}!"), format!("{}", "world")), "Hello world!" ); assert_eq!( runtime_format!("Hello -{}- -{}-", 40 + 5, "World"), "Hello -45- -World-" ); assert_eq!( runtime_format!("Hello {name}!", name = "world"), "Hello world!" ); let name = "world"; assert_eq!(runtime_format!("Hello {name}!", name), "Hello world!"); assert_eq!(runtime_format!("{} {}!", "Hello", name), "Hello world!"); assert_eq!(runtime_format!("{} {name}!", "Hello", name), "Hello world!"); assert_eq!( runtime_format!("{0} {name}!", "Hello", name = "world"), "Hello world!" ); assert_eq!( runtime_format!("Hello {{0}} {}", "world"), "Hello {0} world" ); } } } /// This trait can be implemented by object that can provide a backend for the translation /// /// The backend is only responsable to provide a matching string, the formatting is done /// using this string. /// /// The translator for a crate can be set with the [`set_translator!`] macro pub trait Translator: Send + Sync { fn translate<'a>(&'a self, string: &'a str, context: Option<&'a str>) -> Cow<'a, str>; fn ntranslate<'a>( &'a self, n: u64, singular: &'a str, plural: &'a str, context: Option<&'a str>, ) -> Cow<'a, str>; } impl Translator for std::sync::Arc { fn translate<'a>( &'a self, string: &'a str, context: Option<&'a str>, ) -> std::borrow::Cow<'a, str> { ::translate(self, string, context) } fn ntranslate<'a>( &'a self, n: u64, singular: &'a str, plural: &'a str, context: Option<&'a str>, ) -> std::borrow::Cow<'a, str> { ::ntranslate(self, n, singular, plural, context) } } #[doc(hidden)] pub mod internal { use super::Translator; use std::{borrow::Cow, collections::HashMap, sync::LazyLock, sync::RwLock}; static TRANSLATORS: LazyLock>>> = LazyLock::new(Default::default); pub fn with_translator(module: &'static str, func: impl FnOnce(&dyn Translator) -> T) -> T { let domain = domain_from_module(module); let def = DefaultTranslator(domain); func( TRANSLATORS .read() .unwrap() .get(domain) .map(|x| &**x) .unwrap_or(&def), ) } fn domain_from_module(module: &str) -> &str { module.split("::").next().unwrap_or(module) } #[cfg(feature = "gettext-rs")] fn mangle_context(ctx: &str, s: &str) -> String { format!("{}\u{4}{}", ctx, s) } #[cfg(feature = "gettext-rs")] fn demangle_context(r: String) -> String { if let Some(x) = r.split('\u{4}').next_back() { return x.to_owned(); } r } struct DefaultTranslator(&'static str); #[cfg(feature = "gettext-rs")] impl Translator for DefaultTranslator { fn translate<'a>(&'a self, string: &'a str, context: Option<&'a str>) -> Cow<'a, str> { Cow::Owned(if let Some(ctx) = context { demangle_context(gettextrs::dgettext(self.0, mangle_context(ctx, string))) } else { gettextrs::dgettext(self.0, string) }) } fn ntranslate<'a>( &'a self, n: u64, singular: &'a str, plural: &'a str, context: Option<&'a str>, ) -> Cow<'a, str> { let n = n as u32; Cow::Owned(if let Some(ctx) = context { demangle_context(gettextrs::dngettext( self.0, mangle_context(ctx, singular), mangle_context(ctx, plural), n, )) } else { gettextrs::dngettext(self.0, singular, plural, n) }) } } #[cfg(not(feature = "gettext-rs"))] impl Translator for DefaultTranslator { fn translate<'a>(&'a self, string: &'a str, _context: Option<&'a str>) -> Cow<'a, str> { Cow::Borrowed(string) } fn ntranslate<'a>( &'a self, n: u64, singular: &'a str, plural: &'a str, _context: Option<&'a str>, ) -> Cow<'a, str> { Cow::Borrowed(if n == 1 { singular } else { plural }) } } #[cfg(feature = "gettext-rs")] pub fn init>>(module: &'static str, dir: T) { // FIXME: change T from `Into> to `Into` let dir = String::from_utf8(dir.into()).unwrap(); // FIXME: don't ignore errors let _ = gettextrs::bindtextdomain(domain_from_module(module), dir); static START: std::sync::Once = std::sync::Once::new(); START.call_once(|| { gettextrs::setlocale(gettextrs::LocaleCategory::LcAll, ""); }); } pub fn set_translator(module: &'static str, translator: impl Translator + 'static) { let domain = domain_from_module(module); TRANSLATORS .write() .unwrap() .insert(domain, Box::new(translator)); } pub fn unset_translator(module: &'static str) { let domain = domain_from_module(module); TRANSLATORS.write().unwrap().remove(domain); } } /// Macro used to translate a string. /// /// ``` /// # #[macro_use] extern crate tr; /// // Prints "Hello world!", or a translated version depending on the locale /// println!("{}", tr!("Hello world!")); /// ``` /// /// The string to translate need to be a string literal, as it has to be extracted by /// the `xtr` tool. One can add more argument following a subset of rust formating /// /// ``` /// # #[macro_use] extern crate tr; /// let name = "Olivier"; /// // Prints "Hello, Olivier!", or a translated version of that. /// println!("{}", tr!("Hello, {}!", name)); /// ``` /// /// Plural are using the `"singular" | "plural" % count` syntax. `{n}` will be replaced /// by the count. /// /// ``` /// # #[macro_use] extern crate tr; /// let number_of_items = 42; /// println!("{}", tr!("There is one item" | "There are {n} items" % number_of_items)); /// ``` /// /// Normal formating rules can also be used: /// /// ``` /// # #[macro_use] extern crate tr; /// let number_of_items = 42; /// let folder_name = "/tmp"; /// println!("{}", tr!("There is one item in folder {}" /// | "There are {n} items in folder {}" % number_of_items, folder_name)); /// ``` /// /// /// If the same string appears several time in the crate, it is necessary to add a /// disambiguation context, using the `"context" =>` syntax: /// /// ``` /// # #[macro_use] extern crate tr; /// // These two strings are both "Open" in english, but they may be different in a /// // foreign language. Hence, a context string is necessary. /// let action_name = tr!("File Menu" => "Open"); /// let state = tr!("Document State" => "Open"); /// ``` /// /// To enable the translation, one must first call the `tr_init!` macro once in the crate. /// To translate the strings, one can use the `xtr` utility to extract the string, /// and use the other GNU gettext tools to translate them. /// #[macro_export] macro_rules! tr { ($msgid:tt, $($tail:tt)* ) => { $crate::internal::with_translator(module_path!(), |t| $crate::runtime_format!( t.translate($msgid, None), $($tail)*)) }; ($msgid:tt) => { $crate::internal::with_translator(module_path!(), |t| $crate::runtime_format!( t.translate($msgid, None))) }; ($msgctx:tt => $msgid:tt, $($tail:tt)* ) => { $crate::internal::with_translator(module_path!(), |t| $crate::runtime_format!( t.translate($msgid, Some($msgctx)), $($tail)*)) }; ($msgctx:tt => $msgid:tt) => { $crate::internal::with_translator(module_path!(), |t| $crate::runtime_format!( t.translate($msgid, Some($msgctx)))) }; ($msgid:tt | $plur:tt % $n:expr, $($tail:tt)* ) => {{ let n = $n; $crate::internal::with_translator(module_path!(), |t| $crate::runtime_format!( t.ntranslate(n as u64, $msgid, $plur, None), $($tail)*, n=n)) }}; ($msgid:tt | $plur:tt % $n:expr) => {{ let n = $n; $crate::internal::with_translator(module_path!(), |t| $crate::runtime_format!( t.ntranslate(n as u64, $msgid, $plur, None), n)) }}; ($msgctx:tt => $msgid:tt | $plur:tt % $n:expr, $($tail:tt)* ) => {{ let n = $n; $crate::internal::with_translator(module_path!(), |t| $crate::runtime_format!( t.ntranslate(n as u64, $msgid, $plur, Some($msgctx)), $($tail)*, n=n)) }}; ($msgctx:tt => $msgid:tt | $plur:tt % $n:expr) => {{ let n = $n; $crate::internal::with_translator(module_path!(), |t| $crate::runtime_format!( t.ntranslate(n as u64, $msgid, $plur, Some($msgctx)), n)) }}; } /// Initialize the translation for a crate, using gettext's bindtextdomain /// /// The macro should be called to specify the path in which the .mo files can be looked for. /// The argument is the string passed to bindtextdomain /// /// The alternative is to call the set_translator! macro /// /// This macro is available only if the feature "gettext-rs" is enabled #[cfg(feature = "gettext-rs")] #[macro_export] macro_rules! tr_init { ($path:expr) => { $crate::internal::init(module_path!(), $path) }; } /// Set the translator to be used for this crate. /// /// The argument needs to be something implementing the [`Translator`] trait /// /// For example, using the gettext crate (if the gettext feature is enabled) /// ```ignore /// let f = File::open("french.mo").expect("could not open the catalog"); /// let catalog = Catalog::parse(f).expect("could not parse the catalog"); /// set_translator!(catalog); /// ``` #[macro_export] macro_rules! set_translator { ($translator:expr) => { $crate::internal::set_translator(module_path!(), $translator) }; } /// Clears the translator to be used for this crate. /// /// Use this macro to return back to the source language. #[macro_export] macro_rules! unset_translator { () => { $crate::internal::unset_translator(module_path!()) }; } #[cfg(feature = "gettext")] impl Translator for gettext::Catalog { fn translate<'a>(&'a self, string: &'a str, context: Option<&'a str>) -> Cow<'a, str> { Cow::Borrowed(if let Some(ctx) = context { self.pgettext(ctx, string) } else { self.gettext(string) }) } fn ntranslate<'a>( &'a self, n: u64, singular: &'a str, plural: &'a str, context: Option<&'a str>, ) -> Cow<'a, str> { Cow::Borrowed(if let Some(ctx) = context { self.npgettext(ctx, singular, plural, n) } else { self.ngettext(singular, plural, n) }) } } #[cfg(test)] mod tests { #[test] fn it_works() { assert_eq!(tr!("Hello"), "Hello"); assert_eq!(tr!("ctx" => "Hello"), "Hello"); assert_eq!(tr!("Hello {}", "world"), "Hello world"); assert_eq!(tr!("ctx" => "Hello {}", "world"), "Hello world"); assert_eq!( tr!("I have one item" | "I have {n} items" % 1), "I have one item" ); assert_eq!( tr!("ctx" => "I have one item" | "I have {n} items" % 42), "I have 42 items" ); assert_eq!( tr!("{} have one item" | "{} have {n} items" % 42, "I"), "I have 42 items" ); assert_eq!( tr!("ctx" => "{0} have one item" | "{0} have {n} items" % 42, "I"), "I have 42 items" ); assert_eq!( tr!("{} = {}", 255, format_args!("{:#x}", 255)), "255 = 0xff" ); } } tr-0.1.11/src/rspolib_translator.rs000064400000000000000000000757331046102023000154300ustar 00000000000000/* Copyright (C) 2025 SixtyFPS GmbH Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ use std::collections::HashMap; /// Use this type to load `.po` files directly in your application for translations. /// /// Construct the `PoTranslator` from either a path via [`Self::from_path`] or a vec of /// data via [`Self::from_vec_u8`]. /// /// `PoTranslator` implements the [`crate::Translator`] trait and can be passed to /// [`crate::set_translator!`]. #[cfg(feature = "po-translator")] pub struct PoTranslator(RSPoLibTranslator); #[cfg(feature = "po-translator")] impl PoTranslator { /// Constructs a `PoTranslator` from the given path. pub fn from_path(path: &std::path::Path) -> Result { let options = rspolib::FileOptions::from(path); Ok(Self( rspolib::pofile(options) .map_err(|parse_error| MoPoTranslatorLoadError::PoParseError(parse_error.into())) .and_then(RSPoLibTranslator::try_from)?, )) } /// Constructs a `PoTranslator` from the given raw vec u8 that must be valid `.po` file contents. pub fn from_vec_u8(data: Vec) -> Result { let options = rspolib::FileOptions::from(data); Ok(Self( rspolib::pofile(options) .map_err(|parse_error| MoPoTranslatorLoadError::PoParseError(parse_error.into())) .and_then(RSPoLibTranslator::try_from)?, )) } } #[cfg(feature = "po-translator")] impl crate::Translator for PoTranslator { fn translate<'a>( &'a self, string: &'a str, context: Option<&'a str>, ) -> std::borrow::Cow<'a, str> { self.0.translate(string, context) } fn ntranslate<'a>( &'a self, n: u64, singular: &'a str, plural: &'a str, context: Option<&'a str>, ) -> std::borrow::Cow<'a, str> { self.0.ntranslate(n, singular, plural, context) } } /// Use this type to load `.mo` files directly in your application for translations. /// /// Construct the `MoTranslator` from either a path via [`Self::from_path`] or a vec of /// data via [`Self::from_vec_u8`]. /// /// `MoTranslator` implements the [`crate::Translator`] trait and can be passed to /// [`crate::set_translator!`]. #[cfg(feature = "mo-translator")] pub struct MoTranslator(RSPoLibTranslator); #[cfg(feature = "mo-translator")] impl MoTranslator { /// Constructs a `MoTranslator` from the given path. pub fn from_path(path: &std::path::Path) -> Result { let options = rspolib::FileOptions::from(path); Ok(Self( rspolib::mofile(options) .map_err(|parse_error| MoPoTranslatorLoadError::MoParseError(parse_error.into())) .and_then(RSPoLibTranslator::try_from)?, )) } /// Constructs a `MoTranslator` from the given raw vec u8 that must be valid `.mo` file contents. pub fn from_vec_u8(data: Vec) -> Result { let options = rspolib::FileOptions::from(data); Ok(Self( rspolib::mofile(options) .map_err(|parse_error| MoPoTranslatorLoadError::MoParseError(parse_error.into())) .and_then(RSPoLibTranslator::try_from)?, )) } } #[cfg(feature = "mo-translator")] impl crate::Translator for MoTranslator { fn translate<'a>( &'a self, string: &'a str, context: Option<&'a str>, ) -> std::borrow::Cow<'a, str> { self.0.translate(string, context) } fn ntranslate<'a>( &'a self, n: u64, singular: &'a str, plural: &'a str, context: Option<&'a str>, ) -> std::borrow::Cow<'a, str> { self.0.ntranslate(n, singular, plural, context) } } /// Use the `RSPoLibTranslator` to load messages from a `.po` or `.mo` files. /// /// Convert your [`rspolib::POFile`] or [`rspolib::MOFile`] into this type /// using [`RSPoLibTranslator::try_from`]. /// /// `RSPoLibTranslator` implements the [`crate::Translator`] trait and can then /// be passed to [`crate::set_translator!`]. struct RSPoLibTranslator { /// Translations are indexed by message id, optional, plural message id, and optional context. translations: HashMap, plural_rules: plural_rule_parser::Expression, } impl RSPoLibTranslator { fn new( entries: impl IntoIterator, metadata: &HashMap, ) -> Result { let translations = entries .into_iter() .filter_map(|entry| { let translation = if entry.msgid_plural.is_some() { Some(Translation::Plural(entry.msgstr_plural.into_boxed_slice())) } else { entry.msgstr.map(|msgstr| Translation::Singular(msgstr)) }; let key = TranslationKey { message_id: entry.msgid, plural_message_id: entry.msgid_plural, context: entry.msgctxt, }; translation.map(|t| (key, t)) }) .collect(); let plural_rules = metadata .get("Plural-Forms") .and_then(|entry| { entry.split(';').find_map(|sub_entry| { let (key, expression) = sub_entry.split_once('=')?; if key == "plural" { Some( plural_rule_parser::parse_rule_expression(expression).map_err( |parse_error| MoPoTranslatorLoadError::InvalidPluralRules { rules: expression.to_string(), error: parse_error.0.to_string(), }, ), ) } else { None } }) }) .unwrap_or_else(|| Ok(plural_rule_parser::parse_rule_expression("n != 1").unwrap()))?; Ok(RSPoLibTranslator { translations, plural_rules, }) } } impl TryFrom for RSPoLibTranslator { type Error = MoPoTranslatorLoadError; fn try_from(mofile: rspolib::MOFile) -> Result { RSPoLibTranslator::new( mofile.entries.into_iter().map( |rspolib::MOEntry { msgid, msgstr, msgstr_plural, msgid_plural, msgctxt, }| { POMOEntry { msgid, msgstr, msgid_plural, msgstr_plural, msgctxt, } }, ), &mofile.metadata, ) } } impl TryFrom for RSPoLibTranslator { type Error = MoPoTranslatorLoadError; fn try_from(mofile: rspolib::POFile) -> Result { RSPoLibTranslator::new( mofile.entries.into_iter().map( |rspolib::POEntry { msgid, msgstr, msgstr_plural, msgid_plural, msgctxt, .. }| { POMOEntry { msgid, msgstr, msgid_plural, msgstr_plural, msgctxt, } }, ), &mofile.metadata, ) } } #[derive(PartialEq, Eq, Hash)] struct TranslationKey { message_id: String, plural_message_id: Option, context: Option, } enum Translation { Singular(String), Plural(Box<[String]>), } struct POMOEntry { msgid: String, msgstr: Option, msgid_plural: Option, msgstr_plural: Vec, msgctxt: Option, } /// This error type is returned when creating a [`PoTranslator`] or [`MoTranslator`] /// and an error occurding during parsing. #[non_exhaustive] pub enum MoPoTranslatorLoadError { /// This variant describes a failure during parsing of the `.po` file. PoParseError(Box), /// This variant describes a failure during parsing of the `.mo` file. MoParseError(Box), /// This variant describes a failure during parsing of the plural rules. InvalidPluralRules { /// A copy of the plural rules that could not be parsed. rules: String, /// The error that occured during parsing of the plural rules. error: String, }, } impl std::error::Error for MoPoTranslatorLoadError {} impl core::fmt::Display for MoPoTranslatorLoadError { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { match self { Self::PoParseError(error) => { write!(f, "Error parsing `po` file: {}", error) } Self::MoParseError(error) => { write!(f, "Error parsing `mo` file: {}", error) } Self::InvalidPluralRules { rules, error } => { write!(f, "Error parsing plural rules '{}': {}", rules, error) } } } } impl core::fmt::Debug for MoPoTranslatorLoadError { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { core::fmt::Display::fmt(self, f) } } impl crate::Translator for RSPoLibTranslator { fn translate<'a>( &'a self, message_id: &'a str, context: Option<&'a str>, ) -> std::borrow::Cow<'a, str> { std::borrow::Cow::Borrowed( self.translations .get(&(message_id, None, context) as &dyn TranslationLookup) .and_then(|translation| match translation { Translation::Singular(message) => Some(message.as_str()), Translation::Plural(_) => None, }) .unwrap_or(message_id), ) } fn ntranslate<'a>( &'a self, n: u64, singular: &'a str, plural: &'a str, context: Option<&'a str>, ) -> std::borrow::Cow<'a, str> { std::borrow::Cow::Borrowed( self.translations .get(&(singular, Some(plural), context) as &dyn TranslationLookup) .and_then(|translation| match translation { Translation::Singular(_) => None, Translation::Plural(items) => { let translation_pick = self.plural_rules.evaluate(n); items.get(translation_pick as usize).map(|s| s.as_str()) } }) .unwrap_or_else(|| if n == 1 { singular } else { plural }), ) } } /// Helper trait to permit lookup of translations without copying the key, /// by using a dyn trait object as the borrowed type for the (String, Option) /// key tuple. trait TranslationLookup { fn message_id(&self) -> &str; fn plural_message_id(&self) -> Option<&str>; fn context(&self) -> Option<&str>; } impl TranslationLookup for TranslationKey { fn message_id(&self) -> &str { &self.message_id } fn plural_message_id(&self) -> Option<&str> { self.plural_message_id.as_deref() } fn context(&self) -> Option<&str> { self.context.as_deref() } } impl<'a> TranslationLookup for (&'a str, Option<&'a str>, Option<&'a str>) { fn message_id(&self) -> &str { self.0 } fn plural_message_id(&self) -> Option<&str> { self.1 } fn context(&self) -> Option<&str> { self.2 } } impl std::hash::Hash for dyn TranslationLookup + '_ { fn hash(&self, state: &mut H) { self.message_id().hash(state); self.plural_message_id().hash(state); self.context().hash(state); } } impl std::cmp::PartialEq for dyn TranslationLookup + '_ { fn eq(&self, other: &Self) -> bool { self.message_id() == other.message_id() && self.plural_message_id() == other.plural_message_id() && self.context() == other.context() } } impl std::cmp::Eq for dyn TranslationLookup + '_ {} impl<'a> std::borrow::Borrow for TranslationKey { fn borrow(&self) -> &(dyn TranslationLookup + 'a) { self } } mod plural_rule_parser { pub enum BinaryOp { And, Or, Modulo, Equal, NotEqual, Greater, Smaller, GreaterOrEqual, SmallerOrEqual, } pub enum SubExpression { NumberLiteral(u64), NVariable, Condition { condition: u16, true_expr: u16, false_expr: u16, }, BinaryOp { op: BinaryOp, lhs: u16, rhs: u16, }, } impl SubExpression { fn evaluate(&self, sub_expressions: &[SubExpression], n: u64) -> u64 { match self { Self::NumberLiteral(value) => *value, Self::NVariable => n, Self::Condition { condition, true_expr, false_expr, } => { if sub_expressions[*condition as usize].evaluate(sub_expressions, n) != 0 { sub_expressions[*true_expr as usize].evaluate(sub_expressions, n) } else { sub_expressions[*false_expr as usize].evaluate(sub_expressions, n) } } Self::BinaryOp { op, lhs, rhs } => { let lhs_value = sub_expressions[*lhs as usize].evaluate(sub_expressions, n); let rhs_value = sub_expressions[*rhs as usize].evaluate(sub_expressions, n); match op { BinaryOp::And => (lhs_value != 0 && rhs_value != 0) as u64, BinaryOp::Or => (lhs_value != 0 || rhs_value != 0) as u64, BinaryOp::Modulo => lhs_value % rhs_value, BinaryOp::Equal => (lhs_value == rhs_value) as u64, BinaryOp::NotEqual => (lhs_value != rhs_value) as u64, BinaryOp::Greater => (lhs_value > rhs_value) as u64, BinaryOp::Smaller => (lhs_value < rhs_value) as u64, BinaryOp::GreaterOrEqual => (lhs_value >= rhs_value) as u64, BinaryOp::SmallerOrEqual => (lhs_value <= rhs_value) as u64, } } } } } #[cfg(test)] struct DisplayExpression<'a>(usize, &'a [SubExpression]); #[cfg(test)] impl<'a> DisplayExpression<'a> { fn sub(&self, index: u16) -> Self { Self(index as usize, self.1) } } #[cfg(test)] impl<'a> std::fmt::Display for DisplayExpression<'a> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match &self.1[self.0] { SubExpression::NumberLiteral(value) => write!(f, "{}", value), SubExpression::NVariable => write!(f, "n"), SubExpression::Condition { condition, true_expr, false_expr, } => { write!( f, "({} ? {} : {})", self.sub(*condition), self.sub(*true_expr), self.sub(*false_expr) ) } SubExpression::BinaryOp { op, lhs, rhs } => { let op_str = match op { BinaryOp::And => "&", BinaryOp::Or => "|", BinaryOp::Modulo => "%", BinaryOp::Equal => "=", BinaryOp::NotEqual => "!=", BinaryOp::Greater => ">", BinaryOp::Smaller => "<", BinaryOp::GreaterOrEqual => "≥", BinaryOp::SmallerOrEqual => "≤", }; write!(f, "({} {} {})", self.sub(*lhs), op_str, self.sub(*rhs)) } } } } #[derive(Default)] struct ExpressionBuilder(Vec); impl ExpressionBuilder { fn add(&mut self, sub_expr: SubExpression) -> u16 { let index = self.0.len(); self.0.push(sub_expr); index as u16 } } pub struct Expression { sub_expressions: Box<[SubExpression]>, } impl From for Expression { fn from(expression_builder: ExpressionBuilder) -> Self { Self { sub_expressions: expression_builder.0.into_boxed_slice(), } } } impl Expression { pub fn evaluate(&self, n: u64) -> usize { self.sub_expressions .last() .map(|expr| expr.evaluate(&self.sub_expressions, n) as usize) .unwrap_or(0) } } pub struct ParseError<'a>(pub &'static str, &'a [u8]); impl std::fmt::Debug for ParseError<'_> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( f, "ParseError({}, rest={:?})", self.0, std::str::from_utf8(self.1).unwrap() ) } } pub fn parse_rule_expression(string: &str) -> Result> { let ascii = string.as_bytes(); let mut expression_builder = ExpressionBuilder::default(); let s = parse_expression(ascii, &mut expression_builder)?; if !s.rest.is_empty() { return Err(ParseError("extra character in string", s.rest)); } if matches!(s.ty, Ty::Boolean) { let true_expr = expression_builder.add(SubExpression::NumberLiteral(1)); let false_expr = expression_builder.add(SubExpression::NumberLiteral(0)); expression_builder.add(SubExpression::Condition { condition: s.expr, true_expr, false_expr, }); } Ok(expression_builder.into()) } #[derive(Copy, Clone, Debug, PartialEq, Eq)] enum Ty { Number, Boolean, } struct ParsingState<'a> { expr: u16, rest: &'a [u8], ty: Ty, } impl ParsingState<'_> { fn skip_whitespace(self) -> Self { let rest = skip_whitespace(self.rest); Self { rest, ..self } } } /// ` ('?' : )?` fn parse_expression<'a>( string: &'a [u8], builder: &mut ExpressionBuilder, ) -> Result, ParseError<'a>> { let string = skip_whitespace(string); let state = parse_condition(string, builder)?.skip_whitespace(); if state.ty != Ty::Boolean { return Ok(state); } if let Some(rest) = state.rest.strip_prefix(b"?") { let s1 = parse_expression(rest, builder)?.skip_whitespace(); let rest = s1 .rest .strip_prefix(b":") .ok_or(ParseError("expected ':'", s1.rest))?; let s2 = parse_expression(rest, builder)?; if s1.ty != s2.ty { return Err(ParseError( "incompatible types in ternary operator", s2.rest, )); } Ok(ParsingState { expr: builder.add(SubExpression::Condition { condition: state.expr, true_expr: s1.expr, false_expr: s2.expr, }), rest: skip_whitespace(s2.rest), ty: s2.ty, }) } else { Ok(state) } } /// ` ("||" )?` fn parse_condition<'a>( string: &'a [u8], builder: &mut ExpressionBuilder, ) -> Result, ParseError<'a>> { let string = skip_whitespace(string); let state = parse_and_expr(string, builder)?.skip_whitespace(); if state.rest.is_empty() { return Ok(state); } if let Some(rest) = state.rest.strip_prefix(b"||") { let state2 = parse_condition(rest, builder)?; if state.ty != Ty::Boolean || state2.ty != Ty::Boolean { return Err(ParseError("incompatible types in || operator", state2.rest)); } Ok(ParsingState { expr: builder.add(SubExpression::BinaryOp { lhs: state.expr, rhs: state2.expr, op: BinaryOp::Or, }), ty: Ty::Boolean, rest: skip_whitespace(state2.rest), }) } else { Ok(state) } } /// ` ("&&" )?` fn parse_and_expr<'a>( string: &'a [u8], builder: &mut ExpressionBuilder, ) -> Result, ParseError<'a>> { let string = skip_whitespace(string); let state = parse_cmp_expr(string, builder)?.skip_whitespace(); if state.rest.is_empty() { return Ok(state); } if let Some(rest) = state.rest.strip_prefix(b"&&") { let state2 = parse_and_expr(rest, builder)?; if state.ty != Ty::Boolean || state2.ty != Ty::Boolean { return Err(ParseError("incompatible types in || operator", state2.rest)); } Ok(ParsingState { expr: builder.add(SubExpression::BinaryOp { lhs: state.expr, rhs: state2.expr, op: BinaryOp::And, }), ty: Ty::Boolean, rest: skip_whitespace(state2.rest), }) } else { Ok(state) } } /// ` ('=='|'!='|'<'|'>'|'<='|'>=' )?` fn parse_cmp_expr<'a>( string: &'a [u8], builder: &mut ExpressionBuilder, ) -> Result, ParseError<'a>> { let string = skip_whitespace(string); let mut state = parse_value(string, builder)?; state.rest = skip_whitespace(state.rest); if state.rest.is_empty() { return Ok(state); } for (token, op) in [ (b"==" as &[u8], BinaryOp::Equal), (b"!=", BinaryOp::NotEqual), (b"<=", BinaryOp::SmallerOrEqual), (b">=", BinaryOp::GreaterOrEqual), (b"<", BinaryOp::Smaller), (b">", BinaryOp::Greater), ] { if let Some(rest) = state.rest.strip_prefix(token) { let state2 = parse_cmp_expr(rest, builder)?; if state.ty != Ty::Number || state2.ty != Ty::Number { return Err(ParseError("incompatible types in comparison", state2.rest)); } return Ok(ParsingState { expr: builder.add(SubExpression::BinaryOp { lhs: state.expr, rhs: state2.expr, op, }), ty: Ty::Boolean, rest: skip_whitespace(state2.rest), }); } } Ok(state) } /// ` ('%' )?` fn parse_value<'a>( string: &'a [u8], builder: &mut ExpressionBuilder, ) -> Result, ParseError<'a>> { let string = skip_whitespace(string); let mut state = parse_term(string, builder)?; state.rest = skip_whitespace(state.rest); if state.rest.is_empty() { return Ok(state); } if let Some(rest) = state.rest.strip_prefix(b"%") { let state2 = parse_term(rest, builder)?; if state.ty != Ty::Number || state2.ty != Ty::Number { return Err(ParseError("incompatible types in % operator", state2.rest)); } Ok(ParsingState { expr: builder.add(SubExpression::BinaryOp { lhs: state.expr, rhs: state2.expr, op: BinaryOp::Modulo, }), ty: Ty::Number, rest: skip_whitespace(state2.rest), }) } else { Ok(state) } } fn parse_term<'a>( string: &'a [u8], builder: &mut ExpressionBuilder, ) -> Result, ParseError<'a>> { let string = skip_whitespace(string); let state = match string .first() .ok_or(ParseError("unexpected end of string", string))? { b'n' => ParsingState { expr: builder.add(SubExpression::NVariable), rest: &string[1..], ty: Ty::Number, }, b'(' => { let mut s = parse_expression(&string[1..], builder)?; s.rest = s .rest .strip_prefix(b")") .ok_or(ParseError("expected ')'", s.rest))?; s } x if x.is_ascii_digit() => { let (n, rest) = parse_number(string)?; ParsingState { expr: builder.add(SubExpression::NumberLiteral(n as _)), rest, ty: Ty::Number, } } _ => return Err(ParseError("unexpected token", string)), }; Ok(state) } fn parse_number(string: &[u8]) -> Result<(i32, &[u8]), ParseError<'_>> { let end = string .iter() .position(|&c| !c.is_ascii_digit()) .unwrap_or(string.len()); let n = std::str::from_utf8(&string[..end]) .expect("string is valid utf-8") .parse() .map_err(|_| ParseError("can't parse number", string))?; Ok((n, &string[end..])) } fn skip_whitespace(mut string: &[u8]) -> &[u8] { // slice::trim_ascii_start when MSRV >= 1.80 while !string.is_empty() && string[0].is_ascii_whitespace() { string = &string[1..]; } string } #[test] fn test_parse_rule_expression() { #[track_caller] fn p(string: &str) -> String { let expr = parse_rule_expression(string).expect("parse error"); DisplayExpression( expr.sub_expressions .len() .checked_sub(1) .expect("no expression found"), &expr.sub_expressions, ) .to_string() } // en assert_eq!(p("n != 1"), "((n != 1) ? 1 : 0)"); // fr assert_eq!(p("n > 1"), "((n > 1) ? 1 : 0)"); // ar assert_eq!( p("(n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5)"), "((n = 0) ? 0 : ((n = 1) ? 1 : ((n = 2) ? 2 : ((((n % 100) ≥ 3) & ((n % 100) ≤ 10)) ? 3 : (((n % 100) ≥ 11) ? 4 : 5)))))" ); // ga assert_eq!(p("n==1 ? 0 : n==2 ? 1 : (n>2 && n<7) ? 2 :(n>6 && n<11) ? 3 : 4"), "((n = 1) ? 0 : ((n = 2) ? 1 : (((n > 2) & (n < 7)) ? 2 : (((n > 6) & (n < 11)) ? 3 : 4))))"); // ja assert_eq!(p("0"), "0"); // pl assert_eq!( p("(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)"), "((n = 1) ? 0 : ((((n % 10) ≥ 2) & (((n % 10) ≤ 4) & (((n % 100) < 10) | ((n % 100) ≥ 20)))) ? 1 : 2))", ); // ru assert_eq!( p("(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)"), "((((n % 10) = 1) & ((n % 100) != 11)) ? 0 : ((((n % 10) ≥ 2) & (((n % 10) ≤ 4) & (((n % 100) < 10) | ((n % 100) ≥ 20)))) ? 1 : 2))", ); } } #[test] fn single_message() { use crate::Translator; let mut synthetic_mofile = rspolib::MOFile::new(rspolib::FileOptions::default()); synthetic_mofile.entries.push(rspolib::MOEntry { msgid: "Big Error".to_string(), msgstr: Some("Großer Fehler".to_string()), ..Default::default() }); synthetic_mofile.entries.push(rspolib::MOEntry { msgid: "Small Error".to_string(), msgstr: Some("Kleiner Fehler".to_string()), ..Default::default() }); synthetic_mofile.entries.push(rspolib::MOEntry { msgid: "Small Error".to_string(), msgstr: Some("Kleiner Fehler im Kontext".to_string()), msgctxt: Some("some context".to_string()), ..Default::default() }); let translator = RSPoLibTranslator::try_from(synthetic_mofile).unwrap(); assert_eq!(translator.translate("Big Error", None), "Großer Fehler"); assert_eq!(translator.translate("Small Error", None), "Kleiner Fehler"); assert_eq!( translator.translate("Small Error", Some("some context")), "Kleiner Fehler im Kontext" ); } #[test] fn plural_message() { use crate::Translator; let mut synthetic_mofile = rspolib::MOFile::new(rspolib::FileOptions::default()); synthetic_mofile.entries.push(rspolib::MOEntry { msgid: "{n} file".to_string(), msgid_plural: Some("{n} files".to_string()), msgstr: None, msgstr_plural: vec!["{n} Datei".to_string(), "{n} Dateien".to_string()], ..Default::default() }); synthetic_mofile.metadata.insert( "Plural-Forms".to_string(), "nplurals=2; plural=(n != 1)".to_string(), ); let translator = RSPoLibTranslator::try_from(synthetic_mofile).unwrap(); assert_eq!( translator.ntranslate(1, "{n} file", "{n} files", None), "{n} Datei" ); assert_eq!( translator.ntranslate(0, "{n} file", "{n} files", None), "{n} Dateien" ); assert_eq!( translator.ntranslate(3, "{n} file", "{n} files", None), "{n} Dateien" ); } tr-0.1.11/tests/uppercase.rs000064400000000000000000000024061046102023000140320ustar 00000000000000use tr::{set_translator, tr, unset_translator, Translator}; struct UpperCaseTranslator; impl crate::Translator for UpperCaseTranslator { fn translate<'a>( &'a self, string: &'a str, _context: Option<&'a str>, ) -> std::borrow::Cow<'a, str> { string.to_uppercase().into() } fn ntranslate<'a>( &'a self, n: u64, singular: &'a str, plural: &'a str, _context: Option<&'a str>, ) -> std::borrow::Cow<'a, str> { if n == 1 { singular.to_uppercase().into() } else { plural.to_uppercase().into() } } } #[test] fn uppercase() { let arc = std::sync::Arc::new(UpperCaseTranslator); set_translator!(arc); assert_eq!(tr!("Hello"), "HELLO"); assert_eq!(tr!("ctx" => "Hello"), "HELLO"); assert_eq!(tr!("Hello {}", "world"), "HELLO world"); assert_eq!(tr!("ctx" => "Hello {}", tr!("world")), "HELLO WORLD"); assert_eq!( tr!("I have one item" | "I have {n} items" % 1), "I HAVE ONE ITEM" ); assert_eq!( tr!("ctx" => "I have one item" | "I have {n} items" % 42), "I HAVE {N} ITEMS" // uppercased n is not replaced ); unset_translator!(); assert_eq!(tr!("Hello"), "Hello"); }