Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardBaumrock committed Oct 18, 2024
2 parents 5a0d7f8 + 2b6f2b1 commit 996b248
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 44 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Releases
on:
push:
branches:
- main

jobs:
changelog:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: conventional Changelog Action
id: changelog
uses: TriPSs/conventional-changelog-action@v5
with:
preset: "conventionalcommits"
github-token: ${{ secrets.github_token }}
git-user-email: "[email protected]"

- name: create release
uses: actions/create-release@v1
if: ${{ steps.changelog.outputs.skipped == 'false' }}
env:
GITHUB_TOKEN: ${{ secrets.github_token }}
with:
tag_name: ${{ steps.changelog.outputs.tag }}
release_name: ${{ steps.changelog.outputs.tag }}
body: ${{ steps.changelog.outputs.clean_changelog }}
102 changes: 58 additions & 44 deletions RockLanguage.module.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
<?php namespace ProcessWire;
<?php

namespace ProcessWire;

/**
* @author Bernhard Baumrock, 05.06.2022
* @license Licensed under MIT
* @link https://www.baumrock.com
*/
class RockLanguage extends WireData implements Module, ConfigurableModule {
class RockLanguage extends WireData implements Module, ConfigurableModule
{

public static function getModuleInfo() {
public static function getModuleInfo()
{
return [
'title' => 'RockLanguage',
'version' => '1.0.1',
Expand All @@ -17,25 +22,28 @@ public static function getModuleInfo() {
];
}

public function init() {
public function init()
{
$this->sync();
}

/**
* Copy file if it is newer
*/
public function copyIfNewer($from, $to, $toName) {
if(is_file($to) AND filemtime($from) <= filemtime($to)) return;
public function copyIfNewer($from, $to, $toName)
{
if (is_file($to) and filemtime($from) <= filemtime($to)) return;
$this->wire->files->copy($from, $to);
$fromName = basename($from);
$this->log("Copied $fromName to $toName");
}

public function getCustomCodes() {
public function getCustomCodes()
{
$codes = [];
foreach(explode("\n", $this->customCodes ?: '') as $line) {
foreach (explode("\n", $this->customCodes ?: '') as $line) {
$parts = explode("=", $line, 2);
if(count($parts)!=2) continue;
if (count($parts) != 2) continue;
$codes[$parts[0]] = $parts[1];
}
return $codes;
Expand All @@ -44,33 +52,35 @@ public function getCustomCodes() {
/**
* @return array
*/
public function getLanguageCodes($returnString = false) {
if(!$this->wire->languages) return [];
public function getLanguageCodes($returnString = false)
{
if (!$this->wire->languages) return [];
$codes = [];
$string = '';
$custom = $this->getCustomCodes();
foreach($this->wire->languages as $lang) {
foreach ($this->wire->languages as $lang) {
$code = strtoupper($lang->name);
if(array_key_exists($lang->name, $custom)) $code = $custom[$lang->name];
if (array_key_exists($lang->name, $custom)) $code = $custom[$lang->name];

$codes[$lang->id] = $code;
$string .= "$lang->name=$code<br>";
}
if($returnString) return $string;
if ($returnString) return $string;
return $codes;
}

/**
* pull translation files from modules to language
*/
public function pull() {
foreach($this->getLanguageCodes() as $id=>$code) {
public function pull()
{
foreach ($this->getLanguageCodes() as $id => $code) {
// find all language files in site modules
$modules = $this->wire->config->paths->siteModules;
$files = glob($modules."*/RockLanguage/$code/site--modules--*.json");
foreach($files as $moduleFile) {
$files = glob($modules . "*/RockLanguage/$code/site--modules--*.json");
foreach ($files as $moduleFile) {
$name = basename($moduleFile);
$langFile = $this->wire->config->paths->files.$id."/$name";
$langFile = $this->wire->config->paths->files . $id . "/$name";
$this->copyIfNewer($moduleFile, $langFile, "language $id");
}
}
Expand All @@ -79,20 +89,21 @@ public function pull() {
/**
* push translation files from language to modules
*/
public function push() {
foreach($this->getLanguageCodes() as $id=>$code) {
public function push()
{
foreach ($this->getLanguageCodes() as $id => $code) {
// find all language files in language folder
$langpath = $this->wire->config->paths->files.$id."/";
$files = glob($langpath."site--modules--*.json");
foreach($files as $langFile) {
$langpath = $this->wire->config->paths->files . $id . "/";
$files = glob($langpath . "site--modules--*.json");
foreach ($files as $langFile) {
$json = json_decode(file_get_contents($langFile));
$parts = explode("/", $json->file);
$module = $parts[2];
$dir =
$this->wire->config->paths->siteModules
."$module/RockLanguage/$code/";
if(!is_dir($dir)) continue;
$moduleFile = $dir.basename($langFile);
. "$module/RockLanguage/$code/";
if (!is_dir($dir)) continue;
$moduleFile = $dir . basename($langFile);
$this->copyIfNewer($langFile, $moduleFile, "module $module");
}
}
Expand All @@ -101,35 +112,38 @@ public function push() {
/**
* Sync language files
*/
public function sync() {
if(!$this->wire->user->isSuperuser()) return;
if(!$this->wire->config->debug) return;
public function sync()
{
if (!$this->wire->user->isSuperuser()) return;
if (!$this->wire->config->debug) return;
$this->pull();
$this->push();
}

/**
* Config inputfields
* @param InputfieldWrapper $inputfields
*/
public function getModuleConfigInputfields($inputfields) {
* Config inputfields
* @param InputfieldWrapper $inputfields
*/
public function getModuleConfigInputfields($inputfields)
{
$inputfields->add([
'type' => 'markup',
'label' => 'Available Mappings',
'value' => $this->getLanguageCodes(true),
'notes' => 'This field lists all available languages and their folder names that will be used to push/pull translation files to/from.
A foldername **DE** means that translations will be synced to /site/modules/YourModule/RockLanguage/**DE**/yourfile.json;
Sync will only take place if $config->debug=true and user is superuser!',
]);

$inputfields->add([
'type' => 'textarea',
'name' => 'customCodes',
'label' => 'Language Code Mapping Overrides',
'notes' => 'Enter one per line, eg default=DE',
'notes' => 'Enter one per line. Also see notes in the field above.
Example: default=DE',
'value' => $this->customCodes,
]);

$inputfields->add([
'type' => 'markup',
'label' => 'Available Mappings',
'value' => $this->getLanguageCodes(true),
'notes' => 'This field lists all available languages and their folder names that will be used to push/pull translation files to/from.'
."\nA foldername **DE** means that translations will be synced to /site/modules/YourModule/RockLanguage/**DE**/yourfile.json",
]);

return $inputfields;
}

}

0 comments on commit 996b248

Please sign in to comment.