Skip to content

Conversation

@yyc12345
Copy link
Contributor

@yyc12345 yyc12345 commented May 10, 2025

  • use &str and &Path instead of &String and &PathBuf in files changed this time.
  • rename something to make they are more in line with Rust's expressive habits.
    • rename CmdStatsError to CmdError in subcmd::statistics. it can be shorten without any name conflict.
    • remove the Error tail of members in some error enum.
    • correct LoadPO and LoadPo. Rust doesn't suggest left style.
    • correct FileLoad to LoadFile. Rust suggest that the verb should be the first position.
  • create a new module folder trfile for holding various translation file logic.
    • create a linguist module for holding Qt Linguist TS file logic.
    • create a gettext module for holding GNU Gettext PO file logic.
    • create a common module for holding these 2 file types shared components.
  • move linguist_file
    • most content are moved into trfile::linguist.
    • rename TsMessageStats to MessageStats and move it to trfile::common
    • remove useless import, such as std::u64.
    • create new function get_language for Ts to have uniform interface.
    • move correct_language_code to subcmd::zhconv because only this module use it.
  • move Gettext PO file logic.
    • extract logic involving load, load with fallback, save, from subcmd::zhconv.
    • extract logic involving generate statistics from subcmd::statistics.
    • and put them into trfile::gettext with a proper wrapper (not in raw gettext catalog).
  • cleanup subcmd::zhconv module
    • remove fat ZhConvertible, use lighter ZhConvFile enum instead.
    • clean all expect()
    • move the logic about translating PO content into a function translate_po_content and optimize its performance.
  • extract the function, that guess the translation file type from the file extension, from subcmd::{zhconv, statistics} and put into trfile::common with a new created enum TrFileKind.
  • move load, load with fallback, save logic into Ts and Po inside.
  • add testing stuff.
    • add a load from string feature for Ts and Po only in test mode for convenient test.
    • add 2 unittest for new organised PO file logic in subcmd::zhconv and trfile::gettext respectively like TS file unittest.
  • improve performances.
    • optimize ts load function. use std::io::Read trait instead of reading the whole file before parsing it.
    • optimize subcmd::zhconv::translate_po_content by using Iterator::zip() and caching length to reduce useless iterator calling.
  • modify other modules according to hereinabove said changes to make project can be built.

Summary by Sourcery

Refactor and reorganize translation file handling modules, improving code structure and maintainability by introducing a new trfile module with separate components for different translation file types.

New Features:

  • Create a new trfile module with separate submodules for different translation file types
  • Introduce a uniform interface for handling translation files
  • Add a new TrFileKind enum for detecting translation file types

Enhancements:

  • Improve error handling and type safety
  • Optimize file loading and parsing methods
  • Simplify translation file processing logic
  • Reduce code duplication across translation file handling

Chores:

  • Clean up imports
  • Remove unused code
  • Improve type signatures
  • Update function signatures to use more idiomatic Rust patterns

@deepin-ci-robot
Copy link

Hi @yyc12345. Thanks for your PR.

I'm waiting for a linuxdeepin member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @yyc12345 - I've reviewed your changes - here's some feedback:

  • Consider splitting similarly large refactorings into multiple, smaller PRs in the future for easier review.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

let source_msg_count = source_catalog.count();
if target_msg_count != source_msg_count {
return Err(CmdError::DifferentMessages(language_code, source_msg_count, target_msg_count));
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider extracting the common translation looping logic into a helper trait implemented by both TS and PO types to reduce code duplication.

The added dispatch in ZhConvFile and the near-duplicate translation loops for TS and PO files increase the complexity. To simplify and reduce duplication, consider extracting the common looping logic into a helper (or trait) that both TS and PO types implement. For example, you could define a new trait for translating messages:

trait Translator {
    fn messages_count(&self) -> usize;
    fn iterate_messages_mut<'a>(&'a mut self) -> Box<dyn Iterator<Item = (&'a mut dyn MessageMut, &'a dyn Message)> + 'a>;
    fn get_language(&self) -> String;
}

trait MessageMut {
    fn is_translated(&self) -> bool;
    fn is_plural(&self) -> bool;
    fn set_translation(&mut self, msg: String) -> Result<(), CmdError>;
}

fn translate_generic<T: Translator>(source: &T, target: &mut T) -> Result<(), CmdError> {
    let language = target.get_language();
    if source.messages_count() != target.messages_count() {
        return Err(CmdError::DifferentMessages(language, source.messages_count(), target.messages_count()));
    }
    for (target_msg, source_msg) in target.iterate_messages_mut().zip(source.iterate_messages_mut()) {
        if target_msg.0.is_translated() {
            continue;
        }
        if !target_msg.0.is_plural() && source_msg.1.is_translated() {
            // Assuming source_msg.1 provides a method to get the translation string.
            let src_str = /* extract string from source_msg.1 */;
            let translated = zhconv_wrapper(&src_str, &language)?;
            target_msg.0.set_translation(translated)?;
        }
    }
    Ok(())
}

Then update your translate_content_based_on implementations for both TS and PO to delegate to this helper. This refactoring centralizes the common behavior, reducing the number of places that need to be maintained while keeping functionality intact.

@sourcery-ai
Copy link

sourcery-ai bot commented May 10, 2025

Reviewer's Guide

This pull request refactors the handling of translation files (Qt Linguist .ts and Gettext .po) by introducing a new trfile module. This module centralizes file parsing, loading, saving, and statistics generation. Key changes include replacing the ZhConvertible trait with a ZhConvFile enum in the zhconv subcommand for unified processing, standardizing on &str and &Path for string and path arguments, and optimizing TS file loading to use std::io::Read for better performance. Gettext PO file logic has been extracted into trfile::gettext, and a common MessageStats struct is now used for both file types.

File-Level Changes

Change Details Files
Introduced a new trfile module to encapsulate translation file logic, with submodules for common utilities, Linguist TS files, and Gettext PO files.
  • Created trfile module with common, linguist, and gettext submodules.
  • Moved Linguist TS file logic from linguist_file.rs to trfile/linguist.rs.
  • Extracted Gettext PO file logic into trfile/gettext.rs.
  • Created trfile/common.rs for shared components like MessageStats and TrFileKind.
src/lib.rs
src/trfile.rs
src/trfile/common.rs
src/trfile/linguist.rs
src/trfile/gettext.rs
src/linguist_file.rs
Refactored subcmd::zhconv to use the new trfile module and a ZhConvFile enum, replacing the ZhConvertible trait for handling different translation file types.
  • Replaced ZhConvertible trait with ZhConvFile enum (wrapping Ts or Po).
  • Updated file loading, saving, and translation logic to use ZhConvFile and methods from trfile structs.
  • Moved correct_language_code function into this module.
  • Optimized translate_po_content using Iterator::zip().
src/subcmd/zhconv.rs
Standardized translation statistics and file type detection.
  • Renamed TsMessageStats to MessageStats and moved to trfile::common, now used for both TS and PO files.
  • Introduced TrFileKind enum in trfile::common for detecting translation file types from extensions.
  • Updated subcmd::statistics to use TrFileKind and MessageStats from the trfile module.
src/trfile/common.rs
src/trfile/linguist.rs
src/trfile/gettext.rs
src/subcmd/statistics.rs
Improved file operations and performance for TS and PO files.
  • Implemented load/save operations as methods on Ts and Po structs (e.g., Ts::load_from_file, Po::save_into_file).
  • Optimized TS file loading to use std::io::Read (via quick_xml::de::from_reader) instead of reading the whole file.
  • Added load_from_file_or_default methods for Ts and Po.
src/trfile/linguist.rs
src/trfile/gettext.rs
src/subcmd/zhconv.rs
Adopted more idiomatic Rust practices, including using &str/&Path and refining error handling.
  • Changed function signatures to use &str and &Path for borrowed string/path arguments.
  • Renamed error enums (e.g., CmdStatsError to CmdError) and updated error variants.
  • Removed expect() calls in subcmd::zhconv in favor of explicit error propagation.
src/subcmd/zhconv.rs
src/trfile/linguist.rs
src/subcmd/statistics.rs
src/cli.rs
Added and updated unit tests for new and refactored translation logic.
  • Added load_from_str methods to Ts and Po for easier testing.
  • Added new unit test for translate_po_content in subcmd::zhconv.
  • Added unit tests for PO file parsing in trfile::gettext.
src/subcmd/zhconv.rs
src/trfile/linguist.rs
src/trfile/gettext.rs

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@yyc12345 yyc12345 force-pushed the patch branch 2 times, most recently from 0a476e1 to a72a917 Compare May 12, 2025 06:35
- use `&str` and `&Path` instead of `&String` and `&PathBuf` in files changed this time.
- rename something to make they are more in line with Rust's expressive habits.
	* rename `CmdStatsError` to `CmdError` in `subcmd::statistics`. it can be shorten without any name conflict.
	* remove the `Error` tail of members in some error enum.
	* correct `LoadPO` and `LoadPo`. Rust doesn't suggest left style.
	* correct `FileLoad` to `LoadFile`. Rust suggest that the verb should be the first position.
- create a new module folder `i18n_file` for holding various translation file logic.
	* create a `linguist` module for holding Qt Linguist TS file logic.
	* create a `gettext` module for holding GNU Gettext PO file logic.
	* create a `common` module for holding these 2 file types shared components.
- move `linguist_file`
	* most content are moved into `i18n_file::linguist`.
	* rename `TsMessageStats` to `MessageStats` and move it to `i18n_file::common`
	* remove useless import, such as `std::u64`.
	* create new function `get_language` for `Ts` to have uniform interface.
	* move `correct_language_code` to `subcmd::zhconv` because only this module use it.
- move Gettext PO file logic.
	* extract logic involving load, load with fallback, save, from `subcmd::zhconv`.
	* extract logic involving generate statistics from `subcmd::statistics`.
	* and put them into `i18n_file::gettext` with a proper wrapper (not in raw gettext catalog).
- cleanup `subcmd::zhconv` module
	* remove fat `ZhConvertible`, use lighter `ZhConvFile` enum instead.
	* clean all `expect()`
	* move the logic about translating PO content into a function `translate_po_content` and optimize its performance.
- extract the function, that guess the translation file type from the file extension, from `subcmd::{zhconv, statistics}` and put into `i18n_file::common` with a new created enum `I18nFileKind`.
- move load, load with fallback, save logic into Ts and Po inside.
- add testing stuff.
	* add a load from string feature for Ts and Po only in test mode for convenient test.
	* add 2 unittest for new organised PO file logic in `subcmd::zhconv` and `i18n_file::gettext` respectively like TS file unittest.
- improve performances.
	* optimize ts load function. use `std::io::Read` trait instead of reading the whole file before parsing it.
	* optimize `subcmd::zhconv::translate_po_content` by using `Iterator::zip()` and caching length to reduce useless iterator calling.
- modify other modules according to hereinabove said changes to make project can be built.
@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: BLumia, yyc12345

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@BLumia BLumia merged commit f95d52c into linuxdeepin:master May 12, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants