Skip to content

Commit

Permalink
release 5.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tomFlidr committed Jun 20, 2022
0 parents commit 379decf
Show file tree
Hide file tree
Showing 14 changed files with 714 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# http://EditorConfig.org

root = true

[*]
indent_style = tab
tab_width = 4
charset = utf-8
end_of_line = lf
insert_final_newline = false
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/tests export-ignore
61 changes: 61 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk

# =========================
# Operating System Files
# =========================

# OSX
# =========================

.DS_Store
.AppleDouble
.LSOverride

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

# Mercurial versioning system
.hg
.hgignore
.hgtags

# Project files
*.phpproj
.vs
*.phpproj.user
composer.dev.json
composer.dev.lock

vendor/
23 changes: 23 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# @see https://tester.nette.org/cs/testing-with-travis

language: php

php:
- 5.4
- 8.1

env:
- TESTER_PHP_BIN="php"

before_install:
- composer self-update

install:
- composer update --dev

script:
- ./vendor/bin/tester -p $TESTER_PHP_BIN -s -c ./tests/php.ini ./tests

after_failure:
# Vytiskne obsah souborů *.actual
- for i in $(find ./tests -name \*.actual); do echo "--- $i"; cat $i; echo; echo; done
29 changes: 29 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2016, Tom Flídr
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# MvcCore - Extension - Translator - CSV

[![Latest Stable Version](https://img.shields.io/badge/Stable-v5.0.0-brightgreen.svg?style=plastic)](https://github.com/mvccore/ext-translator-csv/releases)
[![License](https://img.shields.io/badge/License-BSD%203-brightgreen.svg?style=plastic)](https://mvccore.github.io/docs/mvccore/5.0.0/LICENSE.md)
![PHP Version](https://img.shields.io/badge/PHP->=5.4-brightgreen.svg?style=plastic)

Simple CSV translator implementation.

## Installation
```shell
composer require mvccore/translator-csv
```
3 changes: 3 additions & 0 deletions composer-update-dev.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@set COMPOSER=composer.dev.json
@call composer update -v --dev
@pause
40 changes: 40 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "mvccore/ext-translator-csv",
"type": "library",
"description": "MvcCore - Extension - Translator - CSV - simple CSV translator implementation.",
"keywords": [
"mvc",
"mvccore",
"framework",
"extension",
"plugin",
"plug-in",
"ext",
"translator",
"translate",
"simple",
"csv",
"file"
],
"license": ["BSD-3-Clause"],
"authors": [{
"name": "Tom Flidr",
"homepage": "https://github.com/tomFlidr"
}],
"require": {
"php": ">=5.4.0",
"mvccore/mvccore": "^5.0",
"mvccore/ext-translator": "^5.0"
},
"require-dev": {
"php": ">=5.4.0",
"nette/tester": "<=2.0",
"tomflidr/tracy": "<=2.5.13"
},
"autoload": {
"classmap": [
"src/"
]
},
"minimum-stability": "dev"
}
144 changes: 144 additions & 0 deletions src/MvcCore/Ext/Translators/Csv.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

/**
* MvcCore
*
* This source file is subject to the BSD 3 License
* For the full copyright and license information, please view
* the LICENSE.md file that are distributed with this source code.
*
* @copyright Copyright (c) 2016 Tom Flidr (https://github.com/mvccore)
* @license https://mvccore.github.io/docs/mvccore/5.0.0/LICENSE.md
*/

namespace MvcCore\Ext\Translators;

/**
* Responsibility - basic CSV translator:
* - Not translated keys are writen into store when request ends.
* - Translation value could contains basic integer or string
* Replacements in curly brackets.
* - Translation value could contains i18n ICU translation format.
* - Configurable location for CSV files.
*/
class Csv
extends \MvcCore\Ext\Translators\AbstractTranslator
implements \MvcCore\Ext\ITranslator {

/**
* MvcCore Extension - Translator - CSV - version:
* Comparison by PHP function version_compare();
* @see http://php.net/manual/en/function.version-compare.php
*/
const VERSION = '5.0.0';

/**
* Relative path to directory with CSV translations,
* relative to application root directory.
* Default value is `~/Var/Translations`.
* @var string
*/
protected $dataDir = '~/Var/Translations';

/**
* Configure relative path to directory with CSV translations,
* relative to application root directory.
* Default value is `/Var/Translations`.
* @param string $dataDir
* @return string
*/
public function SetDataDir ($dataDir) {
return $this->dataDir = $dataDir;
}

/**
* Return relative path to directory with CSV translations,
* relative to application root directory.
* @return string
*/
public function GetDataDir () {
return $this->dataDir;
}

/**
* Complete CSV store full path.
* @return string
*/
protected function getCsvStoreFullPath () {
$dataDir = $this->dataDir;
if (mb_substr($dataDir, 0, 2) === '~/') {
$app = \MvcCore\Application::GetInstance();
$dataDir = $app->GetRequest()->GetAppRoot() . mb_substr($dataDir, 1);
}
return $dataDir . '/' . $this->localization . '.csv';
}

/**
* Load and parse CSV translation store from HDD.
* @param int|string|NULL Translation store id, optional.
* @throws \Exception
* @return array
*/
public function LoadStore ($id = NULL) {
$store = [];
$csvFullPath = $this->getCsvStoreFullPath();
if (!file_exists($csvFullPath)) {
if (!self::$writeTranslations)
trigger_error("No translations found in path: `{$csvFullPath}`.", E_USER_NOTICE);
return [];
} else {
$rawCsv = file_get_contents($csvFullPath);
$rawCsvRows = explode("\n", str_replace(["\r\n", "\r"], "\n", $rawCsv));
foreach ($rawCsvRows as $rowKey => $rawCsvRow) {
if (!trim($rawCsvRow)) continue;
$keyAndValue = str_getcsv($rawCsvRow, ";", '');
if (count($keyAndValue) === 1) throw new \Exception(
"Missing translation - line: `{$rowKey}`, localization: `{$this->localization}`."
);
list($key, $value) = $keyAndValue;
if (isset($store[$key])) {
$rowKey += 1;
self::thrownAnException(
"Translation key already defined. "
."(path: '{$csvFullPath}', row: '{$rowKey}', key: '{$key}')"
);
}
$value = str_replace('\\n', "\n", $value);
if ($this->detectI18nIcuTranslation($value)) {
$store[$key] = [TRUE, new \MvcCore\Ext\Translators\IcuTranslation(
$this->localization, $value
)];
} else {
$store[$key] = [FALSE, $value];
}
}
}
return $store;
}

/**
* Append not translated keys into CSV store, for current
* localization, after request is done, when browser connection
* is closed, in `register_shutdown_function()` handler.
* @return void
*/
protected function writeTranslations () {
$app = \MvcCore\Application::GetInstance();
$toolsClass = $app->GetToolClass();

$csvFullPath = $this->getCsvStoreFullPath();
$rawContent = '';
if (file_exists($csvFullPath)) {
$rawContent = str_replace(["\r\n", "\r"], "\n", trim(file_get_contents($csvFullPath)));
}

$newItems = [];
foreach (array_keys($this->newTranslations) as $newTranslation)
$newItems[] = $newTranslation . ';' . static::NOT_TRANSLATED_KEY_MARK . $newTranslation;
$separator = mb_strlen($rawContent) > 0 ? "\n" : "";

$toolsClass::AtomicWrite(
$csvFullPath, $rawContent . $separator . implode("\n", $newItems)
);
}
}

0 comments on commit 379decf

Please sign in to comment.