humhub / translation

Internal translation tool
https://translate.humhub.org
Apache License 2.0
17 stars 15 forks source link

Import/Export Option #30

Open ArchBlood opened 4 years ago

ArchBlood commented 4 years ago

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

luke- commented 4 years ago

That would be really practical!

ArchBlood commented 10 months ago

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

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

luke- commented 10 months ago

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

ArchBlood commented 10 months ago

@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;
}
luke- commented 10 months ago

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.

ArchBlood commented 10 months ago

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:

luke- commented 10 months ago

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?

ArchBlood commented 10 months ago

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