translation icon indicating copy to clipboard operation
translation copied to clipboard

Import/Export Option

Open ArchBlood opened this issue 5 years ago • 8 comments

Would be amazing to have an import/export option for this module, I know it's not widely used anymore but would be a lot easier to update the repos on GitHub instead of having to go through all the directories just to get the files to upload to their repos

ArchBlood avatar Aug 11 '20 21:08 ArchBlood

That would be really practical!

luke- avatar Aug 13 '20 11:08 luke-

@luke- would it be possible to implement something like the following?

https://github.com/kbjr/Git.php/

ArchBlood avatar Nov 13 '23 00:11 ArchBlood

@ArchBlood hmm not sure. maybe it's better to directly extract the messages from git repos.

luke- avatar Nov 13 '23 15:11 luke-

@luke- how about something like the following?

Of course, the following code can be fixed to better suit HumHub's needs

Export

// Function to dynamically find and create a zip file of the 'messages' directory within a specific module
function createModuleMessagesZip() {
    // Ensure the ZipArchive class is available
    if (!class_exists('ZipArchive')) {
        return 'ZipArchive class is not available.';
    }

    // Assuming 'MyModule' is the name of your module
    $moduleDirectory = Yii::getAlias('@webroot/protected/modules/{module-id}/');

    // Check if the module directory exists
    if (is_dir($moduleDirectory)) {
        // Initialize the directory queue with the module directory
        $directories = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($moduleDirectory),
            RecursiveIteratorIterator::SELF_FIRST
        );

        foreach ($directories as $directory) {
            if ($directory->isDir() && $directory->getBasename() === 'messages') {
                // Found the 'messages' directory within the module

                // Create a new zip archive
                $zip = new ZipArchive();
                $zipFileName = 'module_messages.zip';

                if ($zip->open($zipFileName, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
                    return 'Failed to create the zip file.';
                }

                // Add directory contents to the zip file
                $files = new RecursiveIteratorIterator(
                    new RecursiveDirectoryIterator($directory->getPathname()),
                    RecursiveIteratorIterator::LEAVES_ONLY
                );

                foreach ($files as $name => $file) {
                    if (!$file->isDir()) {
                        $filePath = $file->getRealPath();
                        $relativePath = substr($filePath, strlen($directory->getPath()) + 1);

                        $zip->addFile($filePath, $relativePath);
                    }
                }

                // Close the zip file
                $zip->close();

                // Return the zip file name or a success message
                return 'Zip file created: ' . $zipFileName;
            }
        }
    }

    // If 'messages' directory within the module is not found
    return 'Directory "messages" within the module not found.';
}
// Usage example within your HumHub module or controller action
$result = createModuleMessagesZip();
echo $result;

Import

// Function to handle importing the messages folder into a specific module
function importModuleMessages($zipFile) {
    // Ensure the ZipArchive class is available
    if (!class_exists('ZipArchive')) {
        return 'ZipArchive class is not available.';
    }

    // Assuming 'MyModule' is the name of your module
    $moduleDirectory = Yii::getAlias('@webroot/protected/modules/MyModule/');
    $messagesDirectory = $moduleDirectory . 'messages/';

    // Check if the module directory exists
    if (!is_dir($moduleDirectory)) {
        return 'Module directory not found.';
    }

    // Check if the messages directory exists, if not, create it
    if (!is_dir($messagesDirectory)) {
        mkdir($messagesDirectory, 0777, true);
    }

    // Open the uploaded zip file
    $zip = new ZipArchive();
    if ($zip->open($zipFile) === true) {
        // Extract zip contents to the messages directory
        $zip->extractTo($messagesDirectory);
        $zip->close();

        return 'Import successful.';
    } else {
        return 'Failed to open the zip file.';
    }
}

// Assuming this code is part of an action in a module controller
if (isset($_FILES['zipFile'])) {
    $uploadedFile = $_FILES['zipFile']['tmp_name'];
    $result = importModuleMessages($uploadedFile);
    echo $result;
}

ArchBlood avatar Nov 25 '23 00:11 ArchBlood

Look good, but I don't know if we need an import/export function. The module is actually only intended for internal use at translate.humhub.org and changes here should always be pushed to the respective GitHub repositories regularly.

luke- avatar Nov 25 '23 09:11 luke-

Look good, but I don't know if we need an import/export function. The module is actually only intended for internal use at translate.humhub.org and changes here should always be pushed to the respective GitHub repositories regularly.

I had that thought on my mind as well, but I also thought it would be easier for module developers that also use the module as well. I could probably implement it in a way that does a push and pull for GitHub repos but thought it would overly complicate the module. :thinking:

ArchBlood avatar Nov 25 '23 09:11 ArchBlood

But if module developers like e.g. GreenMeteor use the module, can they simply push the changed translation messages into the Git repos like we do on the HumHub translation page?

luke- avatar Nov 25 '23 22:11 luke-

But if module developers like e.g. GreenMeteor use the module, can they simply push the changed translation messages into the Git repos like we do on the HumHub translation page?

Currently not possible for GreenMeteor

ArchBlood avatar Nov 25 '23 22:11 ArchBlood