├── .editorconfig ├── .github └── workflows │ └── run-tests.yml ├── .php_cs ├── .styleci.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── RELEASE.md ├── bin ├── lint-all.sh ├── pre-commit.sh ├── setup.sh └── switch-version.sh ├── composer.json ├── config └── config.php ├── docs ├── activate-api-0.png ├── activate-api-1.png ├── banner.jpg ├── credentials-0.png ├── credentials-1.png ├── credentials-2.png ├── credentials-3.png ├── credentials-4.png ├── extra-sheets.png └── new-project.png ├── phpunit.xml └── src ├── .gitkeep ├── Client ├── Api.php └── Client.php ├── Commands ├── Lock.php ├── Open.php ├── Output.php ├── Prepare.php ├── Publish.php ├── Pull.php ├── Push.php ├── Setup.php ├── Status.php └── Unlock.php ├── Sheet ├── AbstractSheet.php ├── Styles.php ├── TranslationsSheet.php └── TranslationsSheetCoordinates.php ├── SheetPuller.php ├── SheetPusher.php ├── Spreadsheet.php ├── Translation ├── Item.php ├── Reader.php ├── Transformer.php └── Writer.php ├── TranslationSheetServiceProvider.php └── Util.php /.editorconfig: -------------------------------------------------------------------------------- 1 | ; This file is for unifying the coding style for different editors and IDEs. 2 | ; More information at http://editorconfig.org 3 | 4 | root = true 5 | 6 | [*] 7 | charset = utf-8 8 | indent_size = 4 9 | indent_style = space 10 | end_of_line = lf 11 | insert_final_newline = true 12 | trim_trailing_whitespace = true 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /.github/workflows/run-tests.yml: -------------------------------------------------------------------------------- 1 | name: run-tests 2 | 3 | on: [pull_request] 4 | 5 | jobs: 6 | test: 7 | if: "!contains(github.event.commits[0].message, '[skip ci]')" 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: true 11 | matrix: 12 | php: [8.3] 13 | laravel: [11.*] 14 | include: 15 | - laravel: 11.* 16 | testbench: 9.* 17 | name: PHP ${{ matrix.php }} - LARAVEL ${{ matrix.laravel }} 18 | steps: 19 | - name: Checkout code 20 | uses: actions/checkout@v2 21 | 22 | - name: Decrypt services-account.json 23 | run: | 24 | gpg --quiet --batch --yes --decrypt --passphrase="$GOOGLE_SERVICES_SECRET" \ 25 | --output $GITHUB_WORKSPACE/tests/fixtures/service-account.json $GITHUB_WORKSPACE/tests/fixtures/service-account.json.gpg 26 | env: 27 | GOOGLE_SERVICES_SECRET: ${{ secrets.GOOGLE_SERVICES_SECRET }} 28 | 29 | - name: Cache dependencies 30 | uses: actions/cache@v1 31 | with: 32 | path: ~/.composer/cache/files 33 | key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} 34 | 35 | - name: Setup PHP 36 | uses: shivammathur/setup-php@v2 37 | with: 38 | php-version: ${{ matrix.php }} 39 | extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick 40 | coverage: none 41 | 42 | - name: Install dependencies 43 | run: | 44 | composer require "illuminate/console:${{ matrix.laravel }}" "illuminate/filesystem:${{ matrix.laravel }}" "illuminate/support:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update 45 | composer update --prefer-dist --no-interaction --no-suggest 46 | 47 | - name: Run Tests 48 | run: vendor/bin/phpunit 49 | -------------------------------------------------------------------------------- /.php_cs: -------------------------------------------------------------------------------- 1 | true, 5 | 'array_syntax' => ['syntax' => 'short'], 6 | 'no_multiline_whitespace_before_semicolons' => true, 7 | 'no_short_echo_tag' => true, 8 | 'no_unused_imports' => true, 9 | 'method_argument_space' => ['ensure_fully_multiline' => false], 10 | 'not_operator_with_successor_space' => true, 11 | 'blank_line_after_opening_tag' => true, 12 | 'no_empty_statement' => true, 13 | 'no_extra_consecutive_blank_lines' => true, 14 | 'include' => true, 15 | 'no_trailing_comma_in_list_call' => true, 16 | 'trailing_comma_in_multiline_array' => true, 17 | 'no_leading_namespace_whitespace' => true, 18 | 'no_blank_lines_after_class_opening' => true, 19 | 'no_blank_lines_after_phpdoc' => true, 20 | 'object_operator_without_whitespace' => true, 21 | 'binary_operator_spaces' => ['align_equals' => false], 22 | 'phpdoc_indent' => true, 23 | 'phpdoc_no_access' => true, 24 | 'phpdoc_no_package' => true, 25 | 'phpdoc_scalar' => true, 26 | 'phpdoc_summary' => true, 27 | 'phpdoc_to_comment' => true, 28 | 'phpdoc_trim' => true, 29 | 'phpdoc_no_alias_tag' => ['type' => 'var'], 30 | 'phpdoc_var_without_name' => true, 31 | 'no_leading_import_slash' => true, 32 | 'blank_line_before_return' => true, 33 | 'no_trailing_comma_in_singleline_array' => true, 34 | 'single_blank_line_before_namespace' => true, 35 | 'single_quote' => true, 36 | 'no_singleline_whitespace_before_semicolons' => true, 37 | 'cast_spaces' => true, 38 | 'standardize_not_equals' => true, 39 | 'ternary_operator_spaces' => true, 40 | 'trim_array_spaces' => true, 41 | 'unary_operator_spaces' => true, 42 | 'no_whitespace_in_blank_line' => true, 43 | 'ordered_imports' => true, 44 | ]; 45 | 46 | $excludes = [ 47 | 'vendor', 48 | 'node_modules', 49 | 'system', 50 | ]; 51 | 52 | $finder = PhpCsFixer\Finder::create() 53 | ->in(__DIR__) 54 | ->exclude($excludes) 55 | ->ignoreVCS(true) 56 | ->ignoreDotFiles(true) 57 | ->name('*.php'); 58 | 59 | return PhpCsFixer\Config::create() 60 | ->setRules($rules) 61 | ->setUsingCache(true) 62 | ->setFinder($finder); 63 | -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. 4 | 5 | ### [1.7.1](https://github.com/nikaia/translation-sheet/compare/v1.7.0...v1.7.1) (2024-05-12) 6 | 7 | 8 | ### Bug Fixes 9 | 10 | * **wording:** Replace wrong work for unlock command ([#83](https://github.com/nikaia/translation-sheet/issues/83)) ([b5ac6db](https://github.com/nikaia/translation-sheet/commit/b5ac6dbb6af7f887df5f1e01d6d5d70ddffee27a)) 11 | 12 | ## [1.7.0](https://github.com/nikaia/translation-sheet/compare/v1.6.0...v1.7.0) (2024-05-12) 13 | 14 | 15 | ### Features 16 | 17 | * Add L11 suppport ([#85](https://github.com/nikaia/translation-sheet/issues/85)) ([facd8dc](https://github.com/nikaia/translation-sheet/commit/facd8dc341353d5527c32d900496ecd260444043)) 18 | 19 | ## [1.6.0](https://github.com/nikaia/translation-sheet/compare/v1.5.0...v1.6.0) (2023-03-21) 20 | 21 | ### Features 22 | 23 | * Add Laravel 9 support ([#79](https://github.com/nikaia/translation-sheet/pull/79)) 24 | 25 | ## [1.5.0](https://github.com/nikaia/translation-sheet/compare/v1.4.5...v1.5.0) (2022-03-23) 26 | 27 | 28 | ### Features 29 | 30 | * Add Laravel 9 support ([#76](https://github.com/nikaia/translation-sheet/issues/76)) ([b90a08b](https://github.com/nikaia/translation-sheet/commit/b90a08bf265ece8012ebdbb912d01187c980e5dc)) 31 | 32 | ### [1.4.5](https://github.com/nikaia/translation-sheet/compare/v1.4.4...v1.4.5) (2020-09-25) 33 | 34 | 35 | ### Features 36 | 37 | * Add support for Extra sheets ([#62](https://github.com/nikaia/translation-sheet/issues/62)) ([9cd9222](https://github.com/nikaia/translation-sheet/commit/9cd92220461d20adf01383b4d0cf1f706b87519b)) 38 | 39 | ### [1.4.4](https://github.com/nikaia/translation-sheet/compare/v1.4.3...v1.4.4) (2020-09-14) 40 | 41 | 42 | ### Bug Fixes 43 | 44 | * laravel 7 support ([623e04b](https://github.com/nikaia/translation-sheet/commit/623e04bad7cf018dea49260816e873417dd4eaf8)), closes [nikaia/translation-sheet#60](https://github.com/nikaia/translation-sheet/issues/60) 45 | 46 | ### [1.4.3](https://github.com/nikaia/translation-sheet/compare/v1.4.2...v1.4.3) (2020-09-14) 47 | 48 | 49 | ### Bug Fixes 50 | 51 | * load publish command ([3dc1280](https://github.com/nikaia/translation-sheet/commit/3dc1280b962e167d92bff2e439785ad68b1791b2)) 52 | 53 | ### [1.4.2](https://github.com/nikaia/translation-sheet/compare/v1.4.1...v1.4.2) (2020-09-14) 54 | 55 | ### [1.4.1](https://github.com/nikaia/translation-sheet/compare/v1.4.0...v1.4.1) (2020-09-14) 56 | 57 | 58 | ### Features 59 | 60 | * Add json support ([b9ae229](https://github.com/nikaia/translation-sheet/commit/b9ae229731a47f82db44e858c2155f05f1db60c6)) 61 | * **json:** add json support ([06430bd](https://github.com/nikaia/translation-sheet/commit/06430bddc85b4987517cb7e382a8e5ef6dd0af6b)), closes [nikaia/translation-sheet#34](https://github.com/nikaia/translation-sheet/issues/34) 62 | * **writer:** json format unescaped unicode ([c38547c](https://github.com/nikaia/translation-sheet/commit/c38547c8d9e6cec4db3e49f3555bb451fd18bbfe)) 63 | 64 | ## [1.4.0](https://github.com/nikaia/translation-sheet/compare/v1.3.7...v1.4.0) (2020-09-07) 65 | 66 | 67 | ### Features 68 | 69 | * Add translation_sheet:publish command. ([e5ed9dd](https://github.com/nikaia/translation-sheet/commit/e5ed9ddbe1bf49c368e81813c5602fc8f0dfe0e6)) 70 | * Add Laravel 8 support ([df2fe4f](https://github.com/nikaia/translation-sheet/commit/df2fe4fd9505de7bffbe77cae48f75867810ce7c)) 71 | * Remove meta sheet (not used) ([a1480b4](https://github.com/nikaia/translation-sheet/commit/a1480b40db541b20393d5bd23d7e6e2a77113392)), closes [#56](https://github.com/nikaia/translation-sheet/issues/56) 72 | * Remove meta sheet (not used) ([373a541](https://github.com/nikaia/translation-sheet/commit/373a5413a792a46e21f1eee1624d9fdaaccc0ef4)) 73 | 74 | 75 | ### Bug Fixes 76 | 77 | * issue with translation array with key strings ("0", "1", ...) ([fe0f7d1](https://github.com/nikaia/translation-sheet/commit/fe0f7d15c22d56612de3f8f5daf55c1afc04ea54)), closes [#49](https://github.com/nikaia/translation-sheet/issues/49) 78 | * issue with translations indexed with string keys when excluding files. ([d959544](https://github.com/nikaia/translation-sheet/commit/d9595443f29c295f03e61e50ccd5a31d4adb12fe)) 79 | * **excludes:** sends indexed array when excluding files to google api ([5aa1437](https://github.com/nikaia/translation-sheet/commit/5aa143733ad60e803096efb65384c9a38c7f4965)) 80 | 81 | ### [1.3.7](https://github.com/nikaia/translation-sheet/compare/v1.3.6...v1.3.7) (2020-03-04) 82 | 83 | ### [1.3.6](https://github.com/nikaia/translation-sheet/compare/v1.3.5...v1.3.6) (2020-03-04) 84 | 85 | 86 | ### Features 87 | 88 | * Add Laravel 7 support ([dd4bb1f](https://github.com/nikaia/translation-sheet/commit/dd4bb1f)) 89 | 90 | ### [1.3.5](https://github.com/nikaia/translation-sheet/compare/v1.3.4...v1.3.5) (2019-10-10) 91 | 92 | 93 | ### Bug Fixes 94 | 95 | * update laravel/framework constraints ([4caf5a0](https://github.com/nikaia/translation-sheet/commit/4caf5a0)) 96 | 97 | ### [1.3.4](https://github.com/nikaia/translation-sheet/compare/v1.3.3...v1.3.4) (2019-10-10) 98 | 99 | 100 | ### Features 101 | 102 | * support Laravel 6.* versions ([5da95ef](https://github.com/nikaia/translation-sheet/commit/5da95ef)) 103 | 104 | ### [1.3.3](https://github.com/nikaia/translation-sheet/compare/v1.3.2...v1.3.3) (2019-09-04) 105 | 106 | 107 | ### Bug Fixes 108 | 109 | * move codedungeon/phpunit-result-printer to dev section ([f095e71](https://github.com/nikaia/translation-sheet/commit/f095e71)) 110 | 111 | ### [1.3.2](https://github.com/nikaia/translation-sheet/compare/v1.3.1...v1.3.2) (2019-09-04) 112 | 113 | 114 | ### Features 115 | 116 | * Add Laravel 6.0 support ([f71470b](https://github.com/nikaia/translation-sheet/commit/f71470b)) 117 | 118 | 119 | ## [1.3.1](https://github.com/nikaia/translation-sheet/compare/v1.3.0...v1.3.1) (2019-04-21) 120 | 121 | 122 | ### Bug Fixes 123 | 124 | * Remove usage of deprecated Google client `setAuthConfigFile` method. ([7799f6f](https://github.com/nikaia/translation-sheet/commit/7799f6f)), closes [#40](https://github.com/nikaia/translation-sheet/issues/40) 125 | 126 | 127 | ### Features 128 | 129 | * add a way to exclude some translations via the exclude config option. ([89313fe](https://github.com/nikaia/translation-sheet/commit/89313fe)), closes [#29](https://github.com/nikaia/translation-sheet/issues/29) 130 | 131 | 132 | 133 | 134 | # [1.3.0](https://github.com/nikaia/translation-sheet/compare/v1.2.9...v1.3.0) (2019-02-28) 135 | 136 | 137 | ### Features 138 | 139 | * Add Laravel 5.8 support ([257e4f3](https://github.com/nikaia/translation-sheet/commit/257e4f3)) 140 | 141 | 142 | 143 | 144 | ## 1.2.9 (2019-02-23) 145 | 146 | 147 | ### Bug Fixes 148 | 149 | * Avoid directory scan stopping after encountring vendor dir. ([a624e1e](https://github.com/nikaia/translation-sheet/commit/a624e1e)) 150 | 151 | 152 | 153 | ## 1.2.8 (2019-01-16) 154 | 155 | - Allow empty translations. 156 | 157 | 158 | 159 | ## 1.2.7 (2018-09-06) 160 | 161 | - Change licence & Add L 5.7 support. 162 | 163 | 164 | 165 | ## 1.2.6 (2018-09-06) 166 | 167 | - Add support for columns above Z. 168 | 169 | 170 | 171 | ## 1.2.5 (2018-04-23) 172 | 173 | - Add package auto-discovery support. 174 | 175 | 176 | 177 | ## 1.2.4 (2018-03-05) 178 | 179 | - Add Laravel 5.6 support. 180 | 181 | 182 | 183 | ## 1.2.3 (2017-08-28) 184 | 185 | - Add L5.5 support. 186 | 187 | 188 | 189 | ## 1.2.2 (2017-07-21) 190 | 191 | - Fix translation_sheet:push and google/apiclient v=2.2.0. 192 | 193 | 194 | ## 1.2.1 (2017-06-26) 195 | 196 | - Fix translation_sheet:push and google/apiclient v=2.2.0 197 | 198 | 199 | 200 | # 1.2.0 (2017-06-26) 201 | 202 | - Update goole/apiclient dependency to ^2.1 203 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | 57 | 58 | ## Style coding 59 | 60 | - Setup precommit hook by running `./bin/setup.sh` 61 | 62 | 63 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | 4 | 5 | GNU GENERAL PUBLIC LICENSE 6 | Version 3, 29 June 2007 7 | 8 | Copyright (C) 2007 Free Software Foundation, Inc. 9 | Everyone is permitted to copy and distribute verbatim copies 10 | of this license document, but changing it is not allowed. 11 | 12 | Preamble 13 | 14 | The GNU General Public License is a free, copyleft license for 15 | software and other kinds of works. 16 | 17 | The licenses for most software and other practical works are designed 18 | to take away your freedom to share and change the works. By contrast, 19 | the GNU General Public License is intended to guarantee your freedom to 20 | share and change all versions of a program--to make sure it remains free 21 | software for all its users. We, the Free Software Foundation, use the 22 | GNU General Public License for most of our software; it applies also to 23 | any other work released this way by its authors. You can apply it to 24 | your programs, too. 25 | 26 | When we speak of free software, we are referring to freedom, not 27 | price. Our General Public Licenses are designed to make sure that you 28 | have the freedom to distribute copies of free software (and charge for 29 | them if you wish), that you receive source code or can get it if you 30 | want it, that you can change the software or use pieces of it in new 31 | free programs, and that you know you can do these things. 32 | 33 | To protect your rights, we need to prevent others from denying you 34 | these rights or asking you to surrender the rights. Therefore, you have 35 | certain responsibilities if you distribute copies of the software, or if 36 | you modify it: responsibilities to respect the freedom of others. 37 | 38 | For example, if you distribute copies of such a program, whether 39 | gratis or for a fee, you must pass on to the recipients the same 40 | freedoms that you received. You must make sure that they, too, receive 41 | or can get the source code. And you must show them these terms so they 42 | know their rights. 43 | 44 | Developers that use the GNU GPL protect your rights with two steps: 45 | (1) assert copyright on the software, and (2) offer you this License 46 | giving you legal permission to copy, distribute and/or modify it. 47 | 48 | For the developers' and authors' protection, the GPL clearly explains 49 | that there is no warranty for this free software. For both users' and 50 | authors' sake, the GPL requires that modified versions be marked as 51 | changed, so that their problems will not be attributed erroneously to 52 | authors of previous versions. 53 | 54 | Some devices are designed to deny users access to install or run 55 | modified versions of the software inside them, although the manufacturer 56 | can do so. This is fundamentally incompatible with the aim of 57 | protecting users' freedom to change the software. The systematic 58 | pattern of such abuse occurs in the area of products for individuals to 59 | use, which is precisely where it is most unacceptable. Therefore, we 60 | have designed this version of the GPL to prohibit the practice for those 61 | products. If such problems arise substantially in other domains, we 62 | stand ready to extend this provision to those domains in future versions 63 | of the GPL, as needed to protect the freedom of users. 64 | 65 | Finally, every program is threatened constantly by software patents. 66 | States should not allow patents to restrict development and use of 67 | software on general-purpose computers, but in those that do, we wish to 68 | avoid the special danger that patents applied to a free program could 69 | make it effectively proprietary. To prevent this, the GPL assures that 70 | patents cannot be used to render the program non-free. 71 | 72 | The precise terms and conditions for copying, distribution and 73 | modification follow. 74 | 75 | TERMS AND CONDITIONS 76 | 77 | 0. Definitions. 78 | 79 | "This License" refers to version 3 of the GNU General Public License. 80 | 81 | "Copyright" also means copyright-like laws that apply to other kinds of 82 | works, such as semiconductor masks. 83 | 84 | "The Program" refers to any copyrightable work licensed under this 85 | License. Each licensee is addressed as "you". "Licensees" and 86 | "recipients" may be individuals or organizations. 87 | 88 | To "modify" a work means to copy from or adapt all or part of the work 89 | in a fashion requiring copyright permission, other than the making of an 90 | exact copy. The resulting work is called a "modified version" of the 91 | earlier work or a work "based on" the earlier work. 92 | 93 | A "covered work" means either the unmodified Program or a work based 94 | on the Program. 95 | 96 | To "propagate" a work means to do anything with it that, without 97 | permission, would make you directly or secondarily liable for 98 | infringement under applicable copyright law, except executing it on a 99 | computer or modifying a private copy. Propagation includes copying, 100 | distribution (with or without modification), making available to the 101 | public, and in some countries other activities as well. 102 | 103 | To "convey" a work means any kind of propagation that enables other 104 | parties to make or receive copies. Mere interaction with a user through 105 | a computer network, with no transfer of a copy, is not conveying. 106 | 107 | An interactive user interface displays "Appropriate Legal Notices" 108 | to the extent that it includes a convenient and prominently visible 109 | feature that (1) displays an appropriate copyright notice, and (2) 110 | tells the user that there is no warranty for the work (except to the 111 | extent that warranties are provided), that licensees may convey the 112 | work under this License, and how to view a copy of this License. If 113 | the interface presents a list of user commands or options, such as a 114 | menu, a prominent item in the list meets this criterion. 115 | 116 | 1. Source Code. 117 | 118 | The "source code" for a work means the preferred form of the work 119 | for making modifications to it. "Object code" means any non-source 120 | form of a work. 121 | 122 | A "Standard Interface" means an interface that either is an official 123 | standard defined by a recognized standards body, or, in the case of 124 | interfaces specified for a particular programming language, one that 125 | is widely used among developers working in that language. 126 | 127 | The "System Libraries" of an executable work include anything, other 128 | than the work as a whole, that (a) is included in the normal form of 129 | packaging a Major Component, but which is not part of that Major 130 | Component, and (b) serves only to enable use of the work with that 131 | Major Component, or to implement a Standard Interface for which an 132 | implementation is available to the public in source code form. A 133 | "Major Component", in this context, means a major essential component 134 | (kernel, window system, and so on) of the specific operating system 135 | (if any) on which the executable work runs, or a compiler used to 136 | produce the work, or an object code interpreter used to run it. 137 | 138 | The "Corresponding Source" for a work in object code form means all 139 | the source code needed to generate, install, and (for an executable 140 | work) run the object code and to modify the work, including scripts to 141 | control those activities. However, it does not include the work's 142 | System Libraries, or general-purpose tools or generally available free 143 | programs which are used unmodified in performing those activities but 144 | which are not part of the work. For example, Corresponding Source 145 | includes interface definition files associated with source files for 146 | the work, and the source code for shared libraries and dynamically 147 | linked subprograms that the work is specifically designed to require, 148 | such as by intimate data communication or control flow between those 149 | subprograms and other parts of the work. 150 | 151 | The Corresponding Source need not include anything that users 152 | can regenerate automatically from other parts of the Corresponding 153 | Source. 154 | 155 | The Corresponding Source for a work in source code form is that 156 | same work. 157 | 158 | 2. Basic Permissions. 159 | 160 | All rights granted under this License are granted for the term of 161 | copyright on the Program, and are irrevocable provided the stated 162 | conditions are met. This License explicitly affirms your unlimited 163 | permission to run the unmodified Program. The output from running a 164 | covered work is covered by this License only if the output, given its 165 | content, constitutes a covered work. This License acknowledges your 166 | rights of fair use or other equivalent, as provided by copyright law. 167 | 168 | You may make, run and propagate covered works that you do not 169 | convey, without conditions so long as your license otherwise remains 170 | in force. You may convey covered works to others for the sole purpose 171 | of having them make modifications exclusively for you, or provide you 172 | with facilities for running those works, provided that you comply with 173 | the terms of this License in conveying all material for which you do 174 | not control copyright. Those thus making or running the covered works 175 | for you must do so exclusively on your behalf, under your direction 176 | and control, on terms that prohibit them from making any copies of 177 | your copyrighted material outside their relationship with you. 178 | 179 | Conveying under any other circumstances is permitted solely under 180 | the conditions stated below. Sublicensing is not allowed; section 10 181 | makes it unnecessary. 182 | 183 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 184 | 185 | No covered work shall be deemed part of an effective technological 186 | measure under any applicable law fulfilling obligations under article 187 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 188 | similar laws prohibiting or restricting circumvention of such 189 | measures. 190 | 191 | When you convey a covered work, you waive any legal power to forbid 192 | circumvention of technological measures to the extent such circumvention 193 | is effected by exercising rights under this License with respect to 194 | the covered work, and you disclaim any intention to limit operation or 195 | modification of the work as a means of enforcing, against the work's 196 | users, your or third parties' legal rights to forbid circumvention of 197 | technological measures. 198 | 199 | 4. Conveying Verbatim Copies. 200 | 201 | You may convey verbatim copies of the Program's source code as you 202 | receive it, in any medium, provided that you conspicuously and 203 | appropriately publish on each copy an appropriate copyright notice; 204 | keep intact all notices stating that this License and any 205 | non-permissive terms added in accord with section 7 apply to the code; 206 | keep intact all notices of the absence of any warranty; and give all 207 | recipients a copy of this License along with the Program. 208 | 209 | You may charge any price or no price for each copy that you convey, 210 | and you may offer support or warranty protection for a fee. 211 | 212 | 5. Conveying Modified Source Versions. 213 | 214 | You may convey a work based on the Program, or the modifications to 215 | produce it from the Program, in the form of source code under the 216 | terms of section 4, provided that you also meet all of these conditions: 217 | 218 | a) The work must carry prominent notices stating that you modified 219 | it, and giving a relevant date. 220 | 221 | b) The work must carry prominent notices stating that it is 222 | released under this License and any conditions added under section 223 | 7. This requirement modifies the requirement in section 4 to 224 | "keep intact all notices". 225 | 226 | c) You must license the entire work, as a whole, under this 227 | License to anyone who comes into possession of a copy. This 228 | License will therefore apply, along with any applicable section 7 229 | additional terms, to the whole of the work, and all its parts, 230 | regardless of how they are packaged. This License gives no 231 | permission to license the work in any other way, but it does not 232 | invalidate such permission if you have separately received it. 233 | 234 | d) If the work has interactive user interfaces, each must display 235 | Appropriate Legal Notices; however, if the Program has interactive 236 | interfaces that do not display Appropriate Legal Notices, your 237 | work need not make them do so. 238 | 239 | A compilation of a covered work with other separate and independent 240 | works, which are not by their nature extensions of the covered work, 241 | and which are not combined with it such as to form a larger program, 242 | in or on a volume of a storage or distribution medium, is called an 243 | "aggregate" if the compilation and its resulting copyright are not 244 | used to limit the access or legal rights of the compilation's users 245 | beyond what the individual works permit. Inclusion of a covered work 246 | in an aggregate does not cause this License to apply to the other 247 | parts of the aggregate. 248 | 249 | 6. Conveying Non-Source Forms. 250 | 251 | You may convey a covered work in object code form under the terms 252 | of sections 4 and 5, provided that you also convey the 253 | machine-readable Corresponding Source under the terms of this License, 254 | in one of these ways: 255 | 256 | a) Convey the object code in, or embodied in, a physical product 257 | (including a physical distribution medium), accompanied by the 258 | Corresponding Source fixed on a durable physical medium 259 | customarily used for software interchange. 260 | 261 | b) Convey the object code in, or embodied in, a physical product 262 | (including a physical distribution medium), accompanied by a 263 | written offer, valid for at least three years and valid for as 264 | long as you offer spare parts or customer support for that product 265 | model, to give anyone who possesses the object code either (1) a 266 | copy of the Corresponding Source for all the software in the 267 | product that is covered by this License, on a durable physical 268 | medium customarily used for software interchange, for a price no 269 | more than your reasonable cost of physically performing this 270 | conveying of source, or (2) access to copy the 271 | Corresponding Source from a network server at no charge. 272 | 273 | c) Convey individual copies of the object code with a copy of the 274 | written offer to provide the Corresponding Source. This 275 | alternative is allowed only occasionally and noncommercially, and 276 | only if you received the object code with such an offer, in accord 277 | with subsection 6b. 278 | 279 | d) Convey the object code by offering access from a designated 280 | place (gratis or for a charge), and offer equivalent access to the 281 | Corresponding Source in the same way through the same place at no 282 | further charge. You need not require recipients to copy the 283 | Corresponding Source along with the object code. If the place to 284 | copy the object code is a network server, the Corresponding Source 285 | may be on a different server (operated by you or a third party) 286 | that supports equivalent copying facilities, provided you maintain 287 | clear directions next to the object code saying where to find the 288 | Corresponding Source. Regardless of what server hosts the 289 | Corresponding Source, you remain obligated to ensure that it is 290 | available for as long as needed to satisfy these requirements. 291 | 292 | e) Convey the object code using peer-to-peer transmission, provided 293 | you inform other peers where the object code and Corresponding 294 | Source of the work are being offered to the general public at no 295 | charge under subsection 6d. 296 | 297 | A separable portion of the object code, whose source code is excluded 298 | from the Corresponding Source as a System Library, need not be 299 | included in conveying the object code work. 300 | 301 | A "User Product" is either (1) a "consumer product", which means any 302 | tangible personal property which is normally used for personal, family, 303 | or household purposes, or (2) anything designed or sold for incorporation 304 | into a dwelling. In determining whether a product is a consumer product, 305 | doubtful cases shall be resolved in favor of coverage. For a particular 306 | product received by a particular user, "normally used" refers to a 307 | typical or common use of that class of product, regardless of the status 308 | of the particular user or of the way in which the particular user 309 | actually uses, or expects or is expected to use, the product. A product 310 | is a consumer product regardless of whether the product has substantial 311 | commercial, industrial or non-consumer uses, unless such uses represent 312 | the only significant mode of use of the product. 313 | 314 | "Installation Information" for a User Product means any methods, 315 | procedures, authorization keys, or other information required to install 316 | and execute modified versions of a covered work in that User Product from 317 | a modified version of its Corresponding Source. The information must 318 | suffice to ensure that the continued functioning of the modified object 319 | code is in no case prevented or interfered with solely because 320 | modification has been made. 321 | 322 | If you convey an object code work under this section in, or with, or 323 | specifically for use in, a User Product, and the conveying occurs as 324 | part of a transaction in which the right of possession and use of the 325 | User Product is transferred to the recipient in perpetuity or for a 326 | fixed term (regardless of how the transaction is characterized), the 327 | Corresponding Source conveyed under this section must be accompanied 328 | by the Installation Information. But this requirement does not apply 329 | if neither you nor any third party retains the ability to install 330 | modified object code on the User Product (for example, the work has 331 | been installed in ROM). 332 | 333 | The requirement to provide Installation Information does not include a 334 | requirement to continue to provide support service, warranty, or updates 335 | for a work that has been modified or installed by the recipient, or for 336 | the User Product in which it has been modified or installed. Access to a 337 | network may be denied when the modification itself materially and 338 | adversely affects the operation of the network or violates the rules and 339 | protocols for communication across the network. 340 | 341 | Corresponding Source conveyed, and Installation Information provided, 342 | in accord with this section must be in a format that is publicly 343 | documented (and with an implementation available to the public in 344 | source code form), and must require no special password or key for 345 | unpacking, reading or copying. 346 | 347 | 7. Additional Terms. 348 | 349 | "Additional permissions" are terms that supplement the terms of this 350 | License by making exceptions from one or more of its conditions. 351 | Additional permissions that are applicable to the entire Program shall 352 | be treated as though they were included in this License, to the extent 353 | that they are valid under applicable law. If additional permissions 354 | apply only to part of the Program, that part may be used separately 355 | under those permissions, but the entire Program remains governed by 356 | this License without regard to the additional permissions. 357 | 358 | When you convey a copy of a covered work, you may at your option 359 | remove any additional permissions from that copy, or from any part of 360 | it. (Additional permissions may be written to require their own 361 | removal in certain cases when you modify the work.) You may place 362 | additional permissions on material, added by you to a covered work, 363 | for which you have or can give appropriate copyright permission. 364 | 365 | Notwithstanding any other provision of this License, for material you 366 | add to a covered work, you may (if authorized by the copyright holders of 367 | that material) supplement the terms of this License with terms: 368 | 369 | a) Disclaiming warranty or limiting liability differently from the 370 | terms of sections 15 and 16 of this License; or 371 | 372 | b) Requiring preservation of specified reasonable legal notices or 373 | author attributions in that material or in the Appropriate Legal 374 | Notices displayed by works containing it; or 375 | 376 | c) Prohibiting misrepresentation of the origin of that material, or 377 | requiring that modified versions of such material be marked in 378 | reasonable ways as different from the original version; or 379 | 380 | d) Limiting the use for publicity purposes of names of licensors or 381 | authors of the material; or 382 | 383 | e) Declining to grant rights under trademark law for use of some 384 | trade names, trademarks, or service marks; or 385 | 386 | f) Requiring indemnification of licensors and authors of that 387 | material by anyone who conveys the material (or modified versions of 388 | it) with contractual assumptions of liability to the recipient, for 389 | any liability that these contractual assumptions directly impose on 390 | those licensors and authors. 391 | 392 | All other non-permissive additional terms are considered "further 393 | restrictions" within the meaning of section 10. If the Program as you 394 | received it, or any part of it, contains a notice stating that it is 395 | governed by this License along with a term that is a further 396 | restriction, you may remove that term. If a license document contains 397 | a further restriction but permits relicensing or conveying under this 398 | License, you may add to a covered work material governed by the terms 399 | of that license document, provided that the further restriction does 400 | not survive such relicensing or conveying. 401 | 402 | If you add terms to a covered work in accord with this section, you 403 | must place, in the relevant source files, a statement of the 404 | additional terms that apply to those files, or a notice indicating 405 | where to find the applicable terms. 406 | 407 | Additional terms, permissive or non-permissive, may be stated in the 408 | form of a separately written license, or stated as exceptions; 409 | the above requirements apply either way. 410 | 411 | 8. Termination. 412 | 413 | You may not propagate or modify a covered work except as expressly 414 | provided under this License. Any attempt otherwise to propagate or 415 | modify it is void, and will automatically terminate your rights under 416 | this License (including any patent licenses granted under the third 417 | paragraph of section 11). 418 | 419 | However, if you cease all violation of this License, then your 420 | license from a particular copyright holder is reinstated (a) 421 | provisionally, unless and until the copyright holder explicitly and 422 | finally terminates your license, and (b) permanently, if the copyright 423 | holder fails to notify you of the violation by some reasonable means 424 | prior to 60 days after the cessation. 425 | 426 | Moreover, your license from a particular copyright holder is 427 | reinstated permanently if the copyright holder notifies you of the 428 | violation by some reasonable means, this is the first time you have 429 | received notice of violation of this License (for any work) from that 430 | copyright holder, and you cure the violation prior to 30 days after 431 | your receipt of the notice. 432 | 433 | Termination of your rights under this section does not terminate the 434 | licenses of parties who have received copies or rights from you under 435 | this License. If your rights have been terminated and not permanently 436 | reinstated, you do not qualify to receive new licenses for the same 437 | material under section 10. 438 | 439 | 9. Acceptance Not Required for Having Copies. 440 | 441 | You are not required to accept this License in order to receive or 442 | run a copy of the Program. Ancillary propagation of a covered work 443 | occurring solely as a consequence of using peer-to-peer transmission 444 | to receive a copy likewise does not require acceptance. However, 445 | nothing other than this License grants you permission to propagate or 446 | modify any covered work. These actions infringe copyright if you do 447 | not accept this License. Therefore, by modifying or propagating a 448 | covered work, you indicate your acceptance of this License to do so. 449 | 450 | 10. Automatic Licensing of Downstream Recipients. 451 | 452 | Each time you convey a covered work, the recipient automatically 453 | receives a license from the original licensors, to run, modify and 454 | propagate that work, subject to this License. You are not responsible 455 | for enforcing compliance by third parties with this License. 456 | 457 | An "entity transaction" is a transaction transferring control of an 458 | organization, or substantially all assets of one, or subdividing an 459 | organization, or merging organizations. If propagation of a covered 460 | work results from an entity transaction, each party to that 461 | transaction who receives a copy of the work also receives whatever 462 | licenses to the work the party's predecessor in interest had or could 463 | give under the previous paragraph, plus a right to possession of the 464 | Corresponding Source of the work from the predecessor in interest, if 465 | the predecessor has it or can get it with reasonable efforts. 466 | 467 | You may not impose any further restrictions on the exercise of the 468 | rights granted or affirmed under this License. For example, you may 469 | not impose a license fee, royalty, or other charge for exercise of 470 | rights granted under this License, and you may not initiate litigation 471 | (including a cross-claim or counterclaim in a lawsuit) alleging that 472 | any patent claim is infringed by making, using, selling, offering for 473 | sale, or importing the Program or any portion of it. 474 | 475 | 11. Patents. 476 | 477 | A "contributor" is a copyright holder who authorizes use under this 478 | License of the Program or a work on which the Program is based. The 479 | work thus licensed is called the contributor's "contributor version". 480 | 481 | A contributor's "essential patent claims" are all patent claims 482 | owned or controlled by the contributor, whether already acquired or 483 | hereafter acquired, that would be infringed by some manner, permitted 484 | by this License, of making, using, or selling its contributor version, 485 | but do not include claims that would be infringed only as a 486 | consequence of further modification of the contributor version. For 487 | purposes of this definition, "control" includes the right to grant 488 | patent sublicenses in a manner consistent with the requirements of 489 | this License. 490 | 491 | Each contributor grants you a non-exclusive, worldwide, royalty-free 492 | patent license under the contributor's essential patent claims, to 493 | make, use, sell, offer for sale, import and otherwise run, modify and 494 | propagate the contents of its contributor version. 495 | 496 | In the following three paragraphs, a "patent license" is any express 497 | agreement or commitment, however denominated, not to enforce a patent 498 | (such as an express permission to practice a patent or covenant not to 499 | sue for patent infringement). To "grant" such a patent license to a 500 | party means to make such an agreement or commitment not to enforce a 501 | patent against the party. 502 | 503 | If you convey a covered work, knowingly relying on a patent license, 504 | and the Corresponding Source of the work is not available for anyone 505 | to copy, free of charge and under the terms of this License, through a 506 | publicly available network server or other readily accessible means, 507 | then you must either (1) cause the Corresponding Source to be so 508 | available, or (2) arrange to deprive yourself of the benefit of the 509 | patent license for this particular work, or (3) arrange, in a manner 510 | consistent with the requirements of this License, to extend the patent 511 | license to downstream recipients. "Knowingly relying" means you have 512 | actual knowledge that, but for the patent license, your conveying the 513 | covered work in a country, or your recipient's use of the covered work 514 | in a country, would infringe one or more identifiable patents in that 515 | country that you have reason to believe are valid. 516 | 517 | If, pursuant to or in connection with a single transaction or 518 | arrangement, you convey, or propagate by procuring conveyance of, a 519 | covered work, and grant a patent license to some of the parties 520 | receiving the covered work authorizing them to use, propagate, modify 521 | or convey a specific copy of the covered work, then the patent license 522 | you grant is automatically extended to all recipients of the covered 523 | work and works based on it. 524 | 525 | A patent license is "discriminatory" if it does not include within 526 | the scope of its coverage, prohibits the exercise of, or is 527 | conditioned on the non-exercise of one or more of the rights that are 528 | specifically granted under this License. You may not convey a covered 529 | work if you are a party to an arrangement with a third party that is 530 | in the business of distributing software, under which you make payment 531 | to the third party based on the extent of your activity of conveying 532 | the work, and under which the third party grants, to any of the 533 | parties who would receive the covered work from you, a discriminatory 534 | patent license (a) in connection with copies of the covered work 535 | conveyed by you (or copies made from those copies), or (b) primarily 536 | for and in connection with specific products or compilations that 537 | contain the covered work, unless you entered into that arrangement, 538 | or that patent license was granted, prior to 28 March 2007. 539 | 540 | Nothing in this License shall be construed as excluding or limiting 541 | any implied license or other defenses to infringement that may 542 | otherwise be available to you under applicable patent law. 543 | 544 | 12. No Surrender of Others' Freedom. 545 | 546 | If conditions are imposed on you (whether by court order, agreement or 547 | otherwise) that contradict the conditions of this License, they do not 548 | excuse you from the conditions of this License. If you cannot convey a 549 | covered work so as to satisfy simultaneously your obligations under this 550 | License and any other pertinent obligations, then as a consequence you may 551 | not convey it at all. For example, if you agree to terms that obligate you 552 | to collect a royalty for further conveying from those to whom you convey 553 | the Program, the only way you could satisfy both those terms and this 554 | License would be to refrain entirely from conveying the Program. 555 | 556 | 13. Use with the GNU Affero General Public License. 557 | 558 | Notwithstanding any other provision of this License, you have 559 | permission to link or combine any covered work with a work licensed 560 | under version 3 of the GNU Affero General Public License into a single 561 | combined work, and to convey the resulting work. The terms of this 562 | License will continue to apply to the part which is the covered work, 563 | but the special requirements of the GNU Affero General Public License, 564 | section 13, concerning interaction through a network will apply to the 565 | combination as such. 566 | 567 | 14. Revised Versions of this License. 568 | 569 | The Free Software Foundation may publish revised and/or new versions of 570 | the GNU General Public License from time to time. Such new versions will 571 | be similar in spirit to the present version, but may differ in detail to 572 | address new problems or concerns. 573 | 574 | Each version is given a distinguishing version number. If the 575 | Program specifies that a certain numbered version of the GNU General 576 | Public License "or any later version" applies to it, you have the 577 | option of following the terms and conditions either of that numbered 578 | version or of any later version published by the Free Software 579 | Foundation. If the Program does not specify a version number of the 580 | GNU General Public License, you may choose any version ever published 581 | by the Free Software Foundation. 582 | 583 | If the Program specifies that a proxy can decide which future 584 | versions of the GNU General Public License can be used, that proxy's 585 | public statement of acceptance of a version permanently authorizes you 586 | to choose that version for the Program. 587 | 588 | Later license versions may give you additional or different 589 | permissions. However, no additional obligations are imposed on any 590 | author or copyright holder as a result of your choosing to follow a 591 | later version. 592 | 593 | 15. Disclaimer of Warranty. 594 | 595 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 596 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 597 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 598 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 599 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 600 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 601 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 602 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 603 | 604 | 16. Limitation of Liability. 605 | 606 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 607 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 608 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 609 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 610 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 611 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 612 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 613 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 614 | SUCH DAMAGES. 615 | 616 | 17. Interpretation of Sections 15 and 16. 617 | 618 | If the disclaimer of warranty and limitation of liability provided 619 | above cannot be given local legal effect according to their terms, 620 | reviewing courts shall apply local law that most closely approximates 621 | an absolute waiver of all civil liability in connection with the 622 | Program, unless a warranty or assumption of liability accompanies a 623 | copy of the Program in return for a fee. 624 | 625 | END OF TERMS AND CONDITIONS 626 | 627 | How to Apply These Terms to Your New Programs 628 | 629 | If you develop a new program, and you want it to be of the greatest 630 | possible use to the public, the best way to achieve this is to make it 631 | free software which everyone can redistribute and change under these terms. 632 | 633 | To do so, attach the following notices to the program. It is safest 634 | to attach them to the start of each source file to most effectively 635 | state the exclusion of warranty; and each file should have at least 636 | the "copyright" line and a pointer to where the full notice is found. 637 | 638 | 639 | Copyright (C) Nassif Bourguig 640 | 641 | This program is free software: you can redistribute it and/or modify 642 | it under the terms of the GNU General Public License as published by 643 | the Free Software Foundation, either version 3 of the License, or 644 | (at your option) any later version. 645 | 646 | This program is distributed in the hope that it will be useful, 647 | but WITHOUT ANY WARRANTY; without even the implied warranty of 648 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 649 | GNU General Public License for more details. 650 | 651 | You should have received a copy of the GNU General Public License 652 | along with this program. If not, see . 653 | 654 | Also add information on how to contact you by electronic and paper mail. 655 | 656 | If the program does terminal interaction, make it output a short 657 | notice like this when it starts in an interactive mode: 658 | 659 | Copyright (C) Nassif Bourguig 660 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 661 | This is free software, and you are welcome to redistribute it 662 | under certain conditions; type `show c' for details. 663 | 664 | The hypothetical commands `show w' and `show c' should show the appropriate 665 | parts of the General Public License. Of course, your program's commands 666 | might be different; for a GUI interface, you would use an "about box". 667 | 668 | You should also get your employer (if you work as a programmer) or school, 669 | if any, to sign a "copyright disclaimer" for the program, if necessary. 670 | For more information on this, and how to apply and follow the GNU GPL, see 671 | . 672 | 673 | The GNU General Public License does not permit incorporating your program 674 | into proprietary programs. If your program is a subroutine library, you 675 | may consider it more useful to permit linking proprietary applications with 676 | the library. If this is what you want to do, use the GNU Lesser General 677 | Public License instead of this License. But first, please read 678 | . 679 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Translation Sheet 2 | 3 | Translating Laravel languages files using a Google Spreadsheet. 4 | 5 | 6 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/nikaia/translation-sheet.svg?style=flat-square)](https://packagist.org/packages/nikaia/translation-sheet) 7 | [![Build Status](https://github.com/nikaia/translation-sheet/workflows/run-tests/badge.svg)](https://github.com/nikaia/translation-sheet/actions?query=workflow%3Arun-tests) 8 | 9 | 10 | ![](./docs/banner.jpg) 11 | 12 | ## Contents 13 | 14 | - [Installation](#installation) 15 | - [Usage](#usage) 16 | - [Changelog](#changelog) 17 | - [Testing](#testing) 18 | - [Security](#security) 19 | - [Contributing](#contributing) 20 | - [Credits](#credits) 21 | - [License](#license) 22 | 23 | 24 | ## Installation 25 | 26 | - Install package 27 | 28 | ```bash 29 | $ composer require nikaia/translation-sheet --dev 30 | ``` 31 | 32 | - If Laravel version <= 5.4, Add service provider to your 'config/app.php'. For version >= 5.5, package will be auto-discoverd by Laravel. 33 | 34 | ```php 35 | Nikaia\TranslationSheet\TranslationSheetServiceProvider::class, 36 | ``` 37 | 38 | 39 | 40 | - Configuration can be done via environments variables, but if you prefer you can override the configuration by publishing the package config file using : 41 | 42 | ```bash 43 | $ php artisan translation_sheet:publish 44 | or 45 | $ php artisan vendor:publish --provider="Nikaia\TranslationSheet\TranslationSheetServiceProvider" 46 | ``` 47 | 48 | ### Requirements 49 | Laravel >= 5.1 50 | 51 | ## Configuration 52 | 53 | ### Google Api credentials 54 | 55 | - Create a new project in https://console.developers.google.com/ 56 | - Make sure to activate `Sheet Api` for the created project 57 | - Navigate to "Library" 58 | - Search "Google Sheets API" > Click on "Google Sheets API" and click enable 59 | - Create credentials 60 | - Navigate to "Credentials" 61 | - Click "Create credentials" 62 | - Choose "Service Account key" 63 | - Choose A "New Service Account" in the "Service account" select 64 | - Choose a name. (ie. This is the name that will show up in the Spreadsheet history operations), 65 | - Choose "Editor" as the role 66 | - Choose "JSON" for the key type. 67 | - Save the credentials to 'resources/google/service-account.json' folder. (You can choose another name/folder and update the package configuration) 68 | - Make sure to write down the service account email, you will need to share the spreadsheet with this email (see below). 69 | 70 | ### Spreadsheet 71 | - Create a blank/new spreadsheet here [https://docs.google.com/spreadsheets/](https://docs.google.com/spreadsheets/) . 72 | - Share it with the service account email with `Can edit` permission. 73 | 74 | 75 | ### Package configuration 76 | 77 | In your .env file or in your published config file (`config/translation_sheet.php`), you need to add the following 78 | 79 | # The service account email 80 | TS_SERVICE_ACCOUNT_EMAIL=***@***.iam.gserviceaccount.com 81 | 82 | # The path to the downloaded service account credentials file 83 | TS_SERVICE_ACCOUNT_CREDENTIALS_FILE=resources/google/service-account.json 84 | 85 | # The ID of the spreadsheet that we will be using for translation (the last portion of the spreadsheet url) 86 | TS_SPREADSHEET_ID=xxxx 87 | 88 | # The locales of the application (separated by comma) 89 | TS_LOCALES=fr,en,es 90 | 91 | 92 | ## Usage 93 | 94 | 1/ Setup the spreadsheet 95 | 96 | This need to be done only once. 97 | 98 | ```bash 99 | $ php artisan translation_sheet:setup 100 | ``` 101 | 102 | 2/ Prepare the sheet 103 | 104 | To avoid some conflicts, we will first run this command to rewrite the locale languages files. 105 | 106 | ```bash 107 | $ php artisan translation_sheet:prepare 108 | ``` 109 | 110 | 3/ Publish translation to sheet 111 | 112 | ```bash 113 | $ php artisan translation_sheet:push 114 | ``` 115 | 116 | 4/ Share the spreadsheet with clients or project managers for translations. 117 | 118 | 5/ Once done, You can lock the translations on the spreadsheet (to avoid conflicts) 119 | ```bash 120 | $ php artisan translation_sheet:lock 121 | ``` 122 | 123 | 6/ Pull the translations 124 | 125 | This will pull the translations from the spreadsheet, and write it the language files in your applications. 126 | You can use git diff here to make sure eveything is ok (Conflicts, errors etc ...) 127 | ```bash 128 | $ php artisan translation_sheet:pull 129 | ``` 130 | 131 | 6/ Unlock the translations on the spreadsheet 132 | ```bash 133 | $ php artisan translation_sheet:unlock 134 | ``` 135 | 136 | Open the spreadsheet in the browser 137 | ```bash 138 | $ php artisan translation_sheet:open 139 | ``` 140 | 141 | ## Excluding translations 142 | 143 | Sometimes you might need to instruct the package to exclude some translations. 144 | You can do so by specifying patterns in the `exclude` config option. 145 | It accepts multiple patterns that target the full translation keys and that the [Str::is](https://laravel.com/docs/5.8/helpers#method-str-is) can understand. 146 | 147 | ```php 148 | [ 149 | // ... 150 | 151 | 'exclude' => [ 152 | 'validation*', // This will exclude all the `validation.php` translations. 153 | 'foo::*', // This will exclude all the `foo` namespace translations. 154 | 'foo::bar.*', // this will exclude the `bar` translations from the `foo` namespace. 155 | ], 156 | 157 | // ... 158 | ] 159 | ``` 160 | 161 | 162 | ## Extra sheets 163 | 164 | ![Extra sheets](./docs/extra-sheets.png) 165 | 166 | Sometimes you may have other files that need translations. 167 | They are not related to the laravel application per se and are not stored in the `resources\lang` folder. 168 | Maybe you are building a web app (spa), or even a mobile app alongside 169 | the laravel app, and you need to handle their translations. 170 | 171 | In this specific case, you can configure extra sheets to handle those translations files stored inside a specific path. 172 | 173 | - This feature handles only json files. 174 | - The files must live outside the `resources\lang` directory. For instance `resources\web-app\lang`. 175 | 176 | ```php 177 | [ 178 | // ... 179 | 180 | 'extra_sheets' => [ 181 | [ 182 | 'name' => 'Web App', // Spreadsheet sheet (tab) name. 183 | 'path' => resource_path('web-app/lang'), // Path where json files are stored. Files can be organized inside sub folders. 184 | 'tabColor' => '#0000FF', // Color of the spreadsheet tab 185 | ], 186 | [ 187 | 'name' => 'Mobile App', 188 | 'path' => resource_path('mobile-app/lang'), 189 | 'tabColor' => '#0000FF', 190 | ], 191 | ], 192 | ] 193 | ``` 194 | 195 | > You need to run `translation_sheet:setup` command, if you add this config later on. 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | ## Changelog 207 | 208 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 209 | 210 | ## Testing 211 | 212 | ``` bash 213 | $ composer test 214 | ``` 215 | 216 | _N.B : You need a valid configuration service-account.json file to run tests._ 217 | 218 | ### Github action 219 | 220 | To test your fork using github action, you need a valid `service-account.json`. The file is ignored in the repository to avoid exposing credentials. 221 | You need to encode your credentials file `tests/fixtures/service-account.json using `gpg` 222 | 223 | ```bash 224 | # Save credential file to tests/fixtures/service-account.json 225 | $ gpg -c tests/fixtures/service-account.json tests/fixtures/service-account.json.gpg 226 | ``` 227 | 228 | Commit the `.gpg` encoded file. 229 | 230 | PS. Github action will decrypt the file just before running the tests. See the `run-tests.yml` file. 231 | 232 | 233 | ## Security 234 | 235 | If you discover any security related issues, please email nbourguig@gmail.com instead of using the issue tracker. 236 | 237 | ## Contributing 238 | 239 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 240 | 241 | ## Credits 242 | 243 | - [Nassif Bourguig](https://github.com/nbourguig) 244 | - [All Contributors](../../contributors) 245 | 246 | -------------------------------------------------------------------------------- /RELEASE.md: -------------------------------------------------------------------------------- 1 | # Release 2 | 3 | 4 | 5 | ## Version system 6 | 7 | Versions are handled using `standard-version` node package. 8 | This allows generating automatically version and changelog. 9 | 10 | > This package has version named after the laravel main version. 11 | (ie. 5.7). This implies that all versions that will be published in 12 | a branch will be minor versions (x.x.X). 13 | 14 | ## Commit / style 15 | 16 | - Commit the changes to the current branch. 17 | - When commit use the standard version commit styles. This is required and helps generating the changelog. 18 | - prefix with `feat:` for a feature. 19 | - prefix with `fix:` for a fix. 20 | - prefix with `test:` for a test. 21 | 22 | More info here : https://www.conventionalcommits.org/en/v1.0.0-beta.3/ 23 | 24 | ## Publishing a new path version 25 | 26 | - run `yarn release-patch-dryrun` and check if the result is ok. 27 | - run `yarn release-patch` 28 | - run `git push --follow-tags origin HEAD` 29 | 30 | 31 | ## Publishing a new minor version 32 | - run `yarn release-minor` 33 | 34 | 35 | ## Publishing a new minor version 36 | - run `yarn release-major` 37 | 38 | -------------------------------------------------------------------------------- /bin/lint-all.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | BIN_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd ) 3 | PKG_DIR=$(dirname $BIN_DIR) 4 | SRC_DIR="$PKG_DIR/src" 5 | 6 | # PHP 7 | phpcsfixer_config="$PKG_DIR/.php_cs" 8 | phpcsfixer="$PKG_DIR/vendor/bin/php-cs-fixer fix --config=$phpcsfixer_config" 9 | 10 | echo "[ PHP Linting] target : $PKG_DIR/src" 11 | $phpcsfixer $PKG_DIR/src 12 | 13 | echo "[ PHP Linting] target : $PKG_DIR/config" 14 | $phpcsfixer $PKG_DIR/config -------------------------------------------------------------------------------- /bin/pre-commit.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | PKG_DIR="$( cd "$(dirname "$0")"; cd ..; cd ..; pwd -P )" 4 | 5 | phpcsfixer_config="$PKG_DIR/.php_cs" 6 | phpcsfixer="$PKG_DIR/vendor/bin/php-cs-fixer fix --config=$phpcsfixer_config" 7 | 8 | echo "┌─┐┬ ┬┌─┐┌─┐┌─┐┌─┐┬─┐ ┬" 9 | echo "├─┘├─┤├─┘│ └─┐├┤ │┌┴┬┘" 10 | echo "┴ ┴ ┴┴ └─┘└─┘└ ┴┴ └─" 11 | 12 | git status --porcelain | grep -e '^[AM]\(.*\).php$' | cut -c 3- | while read line; do 13 | $phpcsfixer "$line"; 14 | git add "$line"; 15 | done 16 | 17 | -------------------------------------------------------------------------------- /bin/setup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | GIT_DIR="$( cd "$(dirname "$0")"; cd ..; pwd -P )" 4 | GIT_HOOKS_DIR="$( cd "$GIT_DIR"; cd .git/hooks ; pwd -P )" 5 | 6 | 7 | echo "╔═╗╔═╗╔╗╔╔╦╗╦═╗╦╔╗ ╔═╗╔═╗╔╦╗╦ ╦╔═╗" 8 | echo "║ ║ ║║║║ ║ ╠╦╝║╠╩╗ ╚═╗║╣ ║ ║ ║╠═╝" 9 | echo "╚═╝╚═╝╝╚╝ ╩ ╩╚═╩╚═╝ ╚═╝╚═╝ ╩ ╚═╝╩" 10 | echo " " 11 | 12 | # Make scripts executable 13 | chmod +x "$GIT_DIR"/bin/lint-all.sh 14 | 15 | # Pre Commit 16 | cp "$GIT_DIR/bin/pre-commit.sh" "$GIT_HOOKS_DIR/pre-commit" 17 | chmod +x "$GIT_HOOKS_DIR"/pre-commit 18 | echo " - Copied pre-commit hook. [$GIT_HOOKS_DIR/pre-commit]" 19 | echo " -> Done." 20 | 21 | -------------------------------------------------------------------------------- /bin/switch-version.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | 5 | ILLUMINATE_VERSION=${1?param missing illuminate version ("5.5.*")} 6 | 7 | composer require "illuminate/support:${ILLUMINATE_VERSION}" --no-update -v 8 | composer require "illuminate/console:${ILLUMINATE_VERSION}" --no-update -v 9 | composer require "illuminate/filesystem:${ILLUMINATE_VERSION}" --no-update -v 10 | 11 | 12 | if [ "$ILLUMINATE_VERSION" == "5.5.*" ]; then composer require "orchestra/testbench:^3.5" --dev --no-update -v; fi 13 | if [ "$ILLUMINATE_VERSION" == "5.6.*" ]; then composer require "orchestra/testbench:^3.6" --dev --no-update -v; fi 14 | if [ "$ILLUMINATE_VERSION" == "5.7.*" ]; then composer require "orchestra/testbench:^3.7" --dev --no-update -v; fi 15 | 16 | composer update --no-interaction --prefer-source -vvv 17 | 18 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nikaia/translation-sheet", 3 | "description": "Laravel Translation via Google Spreadsheet", 4 | "homepage": "https://github.com/nikaia/translation-sheet", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Nassif Bourguig", 9 | "email": "nbourguig@gmail.com", 10 | "role": "Developer" 11 | } 12 | ], 13 | "require": { 14 | "google/apiclient": "^2.1", 15 | "illuminate/console": "^v9.0|^10.0|^11.0", 16 | "illuminate/filesystem": "^v9.0|^10.0|^11.0", 17 | "illuminate/support": "^v9.0|^10.0|^11.0" 18 | }, 19 | "require-dev": { 20 | "friendsofphp/php-cs-fixer": "^3.0", 21 | "orchestra/testbench": "^7.0|^8.0|^9.0" 22 | }, 23 | "autoload": { 24 | "psr-4": { 25 | "Nikaia\\TranslationSheet\\": "src" 26 | } 27 | }, 28 | "autoload-dev": { 29 | "psr-4": { 30 | "Nikaia\\TranslationSheet\\Test\\": "tests" 31 | } 32 | }, 33 | "config": { 34 | "sort-packages": true 35 | }, 36 | "minimum-stability": "dev", 37 | "extra": { 38 | "laravel": { 39 | "providers": [ 40 | "Nikaia\\TranslationSheet\\TranslationSheetServiceProvider" 41 | ] 42 | }, 43 | "branch-alias": { 44 | "^8.0": "1.3-dev" 45 | } 46 | }, 47 | "prefer-stable": true, 48 | "scripts": { 49 | "test": "vendor/bin/phpunit", 50 | "release": "npx standard-version release --no-verify", 51 | "release-dry": "npx standard-version release --no-verify --dry-run", 52 | "release-patch": "npx standard-version --release-as patch --no-verify", 53 | "release-minor": "npx standard-version --release-as minor --no-verify", 54 | "release-major": "npx standard-version --release-as major --no-verify", 55 | "release-first": "npx standard-version --first-release --no-verify" 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /config/config.php: -------------------------------------------------------------------------------- 1 | env('TS_GOOGLE_APP_NAME', 'Laravel Translation Sheet'), 11 | 12 | /* 13 | |-------------------------------------------------------------------------- 14 | | Google service account email 15 | |-------------------------------------------------------------------------- 16 | | This is the service account email that you need to create. 17 | | https://console.developers.google.com/apis/credentials 18 | */ 19 | 'serviceAccountEmail' => env('TS_SERVICE_ACCOUNT_EMAIL', '***@***.iam.gserviceaccount.com'), 20 | 21 | /* 22 | |-------------------------------------------------------------------------- 23 | | Google service account credentials file path 24 | |-------------------------------------------------------------------------- 25 | */ 26 | 'serviceAccountCredentialsFile' => env('TS_SERVICE_ACCOUNT_CREDENTIALS_FILE', base_path('resources/google/service-account-crendentials.json')), 27 | 28 | /* 29 | |-------------------------------------------------------------------------- 30 | | Spreadsheet 31 | |-------------------------------------------------------------------------- 32 | | The spreadsheet that will be used for translations. 33 | | You need to create a new empty spreadsheet manually and fill its ID here. 34 | | You can find the ID in the spreadsheet URL. 35 | | (https://docs.google.com/spreadsheets/d/{spreadsheetId}/edit#gid=0) 36 | */ 37 | 'spreadsheetId' => env('TS_SPREADSHEET_ID', '***'), 38 | 39 | /* 40 | |-------------------------------------------------------------------------- 41 | | Available locales 42 | |-------------------------------------------------------------------------- 43 | | List here the app locales 44 | */ 45 | 'locales' => env('TS_LOCALES', ['en']), 46 | 47 | /* 48 | |-------------------------------------------------------------------------- 49 | | Exclude lang files or namespaces 50 | |-------------------------------------------------------------------------- 51 | | List here files or namespaces that the package will exclude 52 | | from all the operations. (prepare, push & pull). 53 | | 54 | | You can use wild card pattern that can be used with the Str::is() 55 | | Laravel helper. (https://laravel.com/docs/5.8/helpers#method-str-is) 56 | | 57 | | Example: 58 | | 'validation*' 59 | | 'foo::*', 60 | | 'foo::bar.*', 61 | */ 62 | 'exclude' => [], 63 | 64 | /** 65 | * Primary sheet (tab) used for the translations. 66 | * 67 | */ 68 | 'primary_sheet' => [ 69 | 'name' => 'Translations', 70 | ], 71 | 72 | /* 73 | |-------------------------------------------------------------------------- 74 | | Extra Sheets 75 | |-------------------------------------------------------------------------- 76 | | This config area give you the possibility to other sheets (tabs) to your spreadsheet. 77 | | they can be used to translate sperately other sections of your application. 78 | 79 | | ie. if you handle your web app or mobile app translation in laravel app. you can instruct 80 | | translations-sheet to add them as sheets. 81 | | Files for theses sheets must no live under resources/lang folder. But, resources/web-app-lang for instance. 82 | | 83 | */ 84 | 'extra_sheets' => [ 85 | /* 86 | [ 87 | // Sheet name 88 | 'name' => 'Web App', 89 | 90 | // Relative path to the resources/lang folder, where the translations files are stored 91 | 'path' => resource_path('web-app/lang'), 92 | 93 | // Tab color 94 | 'tabColor' => '#0000FF', 95 | ] 96 | */ 97 | ], 98 | ]; 99 | -------------------------------------------------------------------------------- /docs/activate-api-0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nikaia/translation-sheet/dd182c02b211b83a77e9bd7d2c9eb65b022c6a00/docs/activate-api-0.png -------------------------------------------------------------------------------- /docs/activate-api-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nikaia/translation-sheet/dd182c02b211b83a77e9bd7d2c9eb65b022c6a00/docs/activate-api-1.png -------------------------------------------------------------------------------- /docs/banner.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nikaia/translation-sheet/dd182c02b211b83a77e9bd7d2c9eb65b022c6a00/docs/banner.jpg -------------------------------------------------------------------------------- /docs/credentials-0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nikaia/translation-sheet/dd182c02b211b83a77e9bd7d2c9eb65b022c6a00/docs/credentials-0.png -------------------------------------------------------------------------------- /docs/credentials-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nikaia/translation-sheet/dd182c02b211b83a77e9bd7d2c9eb65b022c6a00/docs/credentials-1.png -------------------------------------------------------------------------------- /docs/credentials-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nikaia/translation-sheet/dd182c02b211b83a77e9bd7d2c9eb65b022c6a00/docs/credentials-2.png -------------------------------------------------------------------------------- /docs/credentials-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nikaia/translation-sheet/dd182c02b211b83a77e9bd7d2c9eb65b022c6a00/docs/credentials-3.png -------------------------------------------------------------------------------- /docs/credentials-4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nikaia/translation-sheet/dd182c02b211b83a77e9bd7d2c9eb65b022c6a00/docs/credentials-4.png -------------------------------------------------------------------------------- /docs/extra-sheets.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nikaia/translation-sheet/dd182c02b211b83a77e9bd7d2c9eb65b022c6a00/docs/extra-sheets.png -------------------------------------------------------------------------------- /docs/new-project.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nikaia/translation-sheet/dd182c02b211b83a77e9bd7d2c9eb65b022c6a00/docs/new-project.png -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | tests/Unit 6 | 7 | 8 | tests/Feature 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | src/ 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /src/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nikaia/translation-sheet/dd182c02b211b83a77e9bd7d2c9eb65b022c6a00/src/.gitkeep -------------------------------------------------------------------------------- /src/Client/Api.php: -------------------------------------------------------------------------------- 1 | client = $client; 33 | $this->requests = new Collection([]); 34 | } 35 | 36 | /** 37 | * Empty the requests collection. 38 | */ 39 | public function reset() 40 | { 41 | $this->requests = new Collection([]); 42 | 43 | return $this; 44 | } 45 | 46 | /** 47 | * Set the service speadsheet ID. 48 | * 49 | * @param string $spreadsheetId 50 | * @return $this 51 | */ 52 | public function setSpreadsheetId($spreadsheetId) 53 | { 54 | $this->spreadsheetId = $spreadsheetId; 55 | 56 | return $this; 57 | } 58 | 59 | public function addBatchRequests($requests) 60 | { 61 | $requests = is_array($requests) ? $requests : [$requests]; 62 | foreach ($requests as $request) { 63 | $this->requests->push($request); 64 | } 65 | 66 | return $this; 67 | } 68 | 69 | public function sendBatchRequests() 70 | { 71 | $sheets = new Google_Service_Sheets($this->client); 72 | 73 | $sheets->spreadsheets->batchUpdate( 74 | $this->spreadsheetId, 75 | new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([ 76 | 'requests' => $this->requests->toArray(), 77 | ]) 78 | ); 79 | 80 | $this->requests = new Collection([]); 81 | 82 | return $this; 83 | } 84 | 85 | public function frozenRowRequest($sheetId, $frozonRowCount = 1) 86 | { 87 | return new Google_Service_Sheets_Request([ 88 | 'updateSheetProperties' => [ 89 | 'properties' => [ 90 | 'sheetId' => $sheetId, 91 | 'gridProperties' => [ 92 | 'frozenRowCount' => $frozonRowCount, 93 | ], 94 | ], 95 | 'fields' => 'gridProperties.frozenRowCount', 96 | ], 97 | ]); 98 | } 99 | 100 | public function frozenColumnRequest($sheetId, $frozonColumnCount = 1) 101 | { 102 | return new Google_Service_Sheets_Request([ 103 | 'updateSheetProperties' => [ 104 | 'properties' => [ 105 | 'sheetId' => $sheetId, 106 | 'gridProperties' => [ 107 | 'frozenColumnCount' => $frozonColumnCount, 108 | ], 109 | ], 110 | 'fields' => 'gridProperties.frozenColumnCount', 111 | ], 112 | ]); 113 | } 114 | 115 | public function styleArea($range, $styles) 116 | { 117 | return new Google_Service_Sheets_Request([ 118 | 'repeatCell' => [ 119 | 'range' => $range, 120 | 'cell' => [ 121 | 'userEnteredFormat' => [ 122 | 'backgroundColor' => $this->fractalColors($styles['backgroundColor']), 123 | 'horizontalAlignment' => $styles['horizontalAlignment'], 124 | 'verticalAlignment' => $styles['verticalAlignment'], 125 | 'textFormat' => [ 126 | 'foregroundColor' => $this->fractalColors($styles['foregroundColor']), 127 | 'fontSize' => $styles['fontSize'], 128 | 'fontFamily' => $styles['fontFamily'], 129 | 'bold' => $styles['bold'], 130 | ], 131 | ], 132 | ], 133 | 'fields' => 'userEnteredFormat(backgroundColor,textFormat,horizontalAlignment)', 134 | ], 135 | ]); 136 | } 137 | 138 | public function fixedColumnWidthRequest($sheetId, $startIndex, $endIndex, $width) 139 | { 140 | return new Google_Service_Sheets_Request([ 141 | 'updateDimensionProperties' => [ 142 | 'range' => [ 143 | 'sheetId' => $sheetId, 144 | 'dimension' => 'COLUMNS', 145 | 'startIndex' => $startIndex, 146 | 'endIndex' => $endIndex, 147 | ], 148 | 'properties' => [ 149 | 'pixelSize' => $width, 150 | ], 151 | 'fields' => 'pixelSize', 152 | ], 153 | ]); 154 | } 155 | 156 | public function setSheetTitle($sheetId, $title) 157 | { 158 | return new Google_Service_Sheets_Request([ 159 | 'updateSheetProperties' => [ 160 | 'properties' => [ 161 | 'sheetId' => $sheetId, 162 | 'title' => $title, 163 | ], 164 | 'fields' => 'title', 165 | ], 166 | ]); 167 | } 168 | 169 | public function setTabColor($sheetId, $tabColor) 170 | { 171 | return new Google_Service_Sheets_Request([ 172 | 'updateSheetProperties' => [ 173 | 'properties' => [ 174 | 'sheetId' => $sheetId, 175 | 'tabColor' => $this->fractalColors($tabColor), 176 | ], 177 | 'fields' => 'tabColor', 178 | ], 179 | ]); 180 | } 181 | 182 | // @todo: remove if not used any more. 183 | public function setSheetPropertiesRequest($sheetId, $title, $tabColor) 184 | { 185 | return new Google_Service_Sheets_Request([ 186 | 'updateSheetProperties' => [ 187 | 'properties' => [ 188 | 'sheetId' => $sheetId, 189 | 'title' => $title, 190 | 'tabColor' => $this->fractalColors($tabColor), 191 | ], 192 | 'fields' => 'title,tabColor', 193 | ], 194 | ]); 195 | } 196 | 197 | public function addSheetRequest($title, $rowCount, $columnCount, $tabColor = null) 198 | { 199 | $properties = [ 200 | 'title' => $title, 201 | 'gridProperties' => [ 202 | 'rowCount' => $rowCount, 203 | 'columnCount' => $columnCount, 204 | ], 205 | ]; 206 | 207 | if (!is_null($tabColor)) { 208 | $properties['tabColor'] = $this->fractalColors($tabColor); 209 | } 210 | 211 | return new Google_Service_Sheets_Request(['addSheet' => ['properties' => $properties]]); 212 | } 213 | 214 | public function addBlankSheet($title = null) 215 | { 216 | $properties = $title ? ['title' => $title] : []; 217 | return new Google_Service_Sheets_Request(['addSheet' => ['properties' => $properties]]); 218 | } 219 | 220 | public function clearSheetRequest($sheetId) 221 | { 222 | return new Google_Service_Sheets_Request([ 223 | 'updateCells' => [ 224 | 'range' => [ 225 | 'sheetId' => $sheetId, 226 | ], 227 | 'fields' => 'userEnteredValue', 228 | ], 229 | ]); 230 | } 231 | 232 | public function protectRangeRequest($range, $description) 233 | { 234 | return new Google_Service_Sheets_Request([ 235 | 'addProtectedRange' => [ 236 | 'protectedRange' => [ 237 | 'range' => $range, 238 | 'description' => $description, 239 | 'warningOnly' => false, 240 | 'editors' => [ 241 | 'users' => [config('translation_sheet.serviceAccountEmail')], 242 | ], 243 | ], 244 | ], 245 | 246 | ]); 247 | } 248 | 249 | public function writeCells($shortRange, $values) 250 | { 251 | $sheets = new \Google_Service_Sheets($this->client); 252 | 253 | $request = new Google_Service_Sheets_BatchUpdateValuesRequest(); 254 | $request->setValueInputOption('RAW'); 255 | $request->setData([ 256 | 'range' => $shortRange, 257 | 'majorDimension' => 'ROWS', 258 | 'values' => $values, 259 | ]); 260 | 261 | $sheets->spreadsheets_values->batchUpdate($this->spreadsheetId, $request); 262 | } 263 | 264 | public function readCells($sheetId, $range) 265 | { 266 | $range .= $this->getSheetRowCount($sheetId); 267 | 268 | $sheets = new \Google_Service_Sheets($this->client); 269 | 270 | return $sheets->spreadsheets_values->get($this->spreadsheetId, $range)->values; 271 | } 272 | 273 | public function getSheetRowCount($sheetId) 274 | { 275 | $sheet = $this->getSheet($sheetId); 276 | 277 | return $sheet['properties']['gridProperties']['rowCount']; 278 | } 279 | 280 | public function getSheet($sheetId) 281 | { 282 | $sheets = $this->getSheets(); 283 | 284 | foreach ($sheets as $sheet) { 285 | if ($sheet['properties']['sheetId'] === $sheetId) { 286 | return $sheet; 287 | } 288 | } 289 | 290 | return null; 291 | } 292 | 293 | public function getSheetByTitle($title) 294 | { 295 | $sheets = $this->getSheets(); 296 | 297 | foreach ($sheets as $sheet) { 298 | if (strtolower($sheet['properties']['title']) === strtolower($title)) { 299 | return $sheet; 300 | } 301 | } 302 | 303 | return null; 304 | } 305 | 306 | public function getSheets() 307 | { 308 | if (empty($this->cachedSheets)) { 309 | $this->cachedSheets = (new \Google_Service_Sheets($this->client)) 310 | ->spreadsheets 311 | ->get($this->spreadsheetId) 312 | ->getSheets(); 313 | } 314 | 315 | return $this->cachedSheets; 316 | } 317 | 318 | public function forgetSheets() 319 | { 320 | $this->cachedSheets = []; 321 | 322 | return $this; 323 | } 324 | 325 | public function freshSheets() 326 | { 327 | $this->forgetSheets(); 328 | 329 | return $this->getSheets(); 330 | } 331 | 332 | public function firstSheetId() 333 | { 334 | return data_get(collect($this->getSheets())->first(), 'properties.sheetId'); 335 | } 336 | 337 | public function getSheetProtectedRanges($sheetId, $description = null) 338 | { 339 | $sheet = $this->getSheet($sheetId); 340 | 341 | if (is_null($description)) { 342 | return $sheet['protectedRanges']; 343 | } 344 | 345 | $ranges = []; 346 | foreach ($sheet['protectedRanges'] as $range) { 347 | if ($range->description === $description) { 348 | $ranges[] = $range; 349 | } 350 | } 351 | 352 | return $ranges; 353 | } 354 | 355 | public function deleteColumnsFrom($sheetId, $fromColumnIndex) 356 | { 357 | return new Google_Service_Sheets_Request([ 358 | 'deleteDimension' => [ 359 | 'range' => [ 360 | 'sheetId' => $sheetId, 361 | 'dimension' => 'COLUMNS', 362 | 'startIndex' => $fromColumnIndex, 363 | ], 364 | ], 365 | ]); 366 | } 367 | 368 | public function deleteRowsFrom($sheetId, $fromRowIndex) 369 | { 370 | return new Google_Service_Sheets_Request([ 371 | 'deleteDimension' => [ 372 | 'range' => [ 373 | 'sheetId' => $sheetId, 374 | 'dimension' => 'ROWS', 375 | 'startIndex' => $fromRowIndex, 376 | ], 377 | ], 378 | ]); 379 | } 380 | 381 | public function deleteProtectedRange($protectedRangeId) 382 | { 383 | return new Google_Service_Sheets_Request([ 384 | 'deleteProtectedRange' => [ 385 | 'protectedRangeId' => $protectedRangeId, 386 | ], 387 | ]); 388 | } 389 | 390 | public function deleteSheetRequest($sheetId) 391 | { 392 | return new Google_Service_Sheets_Request([ 393 | 'deleteSheet' => [ 394 | 'sheetId' => $sheetId, 395 | ], 396 | ]); 397 | } 398 | 399 | /** 400 | * Return Fractal RGB color array with r,g,b between 0 and 1. 401 | * 402 | * @param mixed $color array of RGB color ([255, 255, 255]) or hex string (#FFFFFF) 403 | * 404 | * @return array 405 | */ 406 | protected function fractalColors($color) 407 | { 408 | if (is_array($color)) { 409 | list($red, $green, $blue) = $color; 410 | } else { 411 | list($red, $green, $blue) = sscanf($color, '#%02x%02x%02x'); 412 | } 413 | 414 | return [ 415 | 'red' => round($red / 255, 1), 416 | 'green' => round($green / 255, 1), 417 | 'blue' => round($blue / 255, 1), 418 | ]; 419 | } 420 | 421 | 422 | } 423 | -------------------------------------------------------------------------------- /src/Client/Client.php: -------------------------------------------------------------------------------- 1 | setAuthConfig($authConfigFile); 21 | $client->setApplicationName($applicationName); 22 | $client->setScopes(\Google_Service_Sheets::SPREADSHEETS); 23 | 24 | return $client; 25 | } 26 | 27 | /** 28 | * Throw an exception if google service config file is not found. 29 | * 30 | * @param $authConfigFile 31 | * @throws \Exception 32 | */ 33 | private static function checkAuthConfigFile($authConfigFile) 34 | { 35 | if (! file_exists($authConfigFile)) { 36 | throw new \Exception('You must specify a valid google service authentication file. Given file ['.$authConfigFile.'] does not exists'); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/Commands/Lock.php: -------------------------------------------------------------------------------- 1 | sheets()->each(function (TranslationsSheet $translationsSheet) { 18 | $this->info("Locking translation sheet [{$translationsSheet->getTitle()}] :"); 19 | 20 | $translationsSheet->lockTranslations(); 21 | $translationsSheet->api()->reset(); 22 | 23 | $this->output->writeln('Done.'); 24 | $this->output->writeln(PHP_EOL); 25 | }); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Commands/Open.php: -------------------------------------------------------------------------------- 1 | getUrl(); 17 | 18 | $this->comment('Opening spreadsheet '.$url); 19 | 20 | shell_exec($this->openCmd().' '.$url); 21 | } 22 | 23 | protected function openCmd() 24 | { 25 | return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? 'start' : 'open'; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Commands/Output.php: -------------------------------------------------------------------------------- 1 | output = new NullOutput; 16 | } 17 | 18 | public function withOutput($output) 19 | { 20 | $this->output = $output; 21 | 22 | return $this; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Commands/Prepare.php: -------------------------------------------------------------------------------- 1 | sheets()->each(function (TranslationsSheet $translationsSheet) use ($pusher, $spreadsheet, $writer) { 21 | 22 | $this->output->writeln("Scanning local languages files for sheet [{$translationsSheet->getTitle()}]"); 23 | 24 | $pusher = $pusher->setTranslationsSheet($translationsSheet); 25 | 26 | $translations = Util::keyValues( 27 | $pusher->getScannedAndTransformedTranslations(), 28 | $spreadsheet->getCamelizedHeader() 29 | ); 30 | 31 | $this->output->writeln('.... Rewriting'); 32 | $writer 33 | ->setTranslationsSheet($translationsSheet) 34 | ->setTranslations($translations) 35 | ->withOutput($this->output) 36 | ->write(); 37 | 38 | $translationsSheet->api()->reset(); 39 | }); 40 | 41 | $this->output->writeln('Done.'); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Commands/Publish.php: -------------------------------------------------------------------------------- 1 | call('vendor:publish', [ 18 | '--provider' => 'Nikaia\TranslationSheet\TranslationSheetServiceProvider', 19 | ]); 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /src/Commands/Pull.php: -------------------------------------------------------------------------------- 1 | sheets()->each(function (TranslationsSheet $translationsSheet) use ($puller) { 20 | $puller 21 | ->setTranslationsSheet($translationsSheet) 22 | ->withOutput($this->output) 23 | ->pull(); 24 | 25 | $translationsSheet->api()->reset(); 26 | }); 27 | } catch (\ErrorException $e) { 28 | if ($e->getMessage() === 'Trying to access array offset on value of type null') { 29 | $this->error('Something is wrong. Did you just add an extra sheet ? try to re-run translation_sheet:setup!'); 30 | return; 31 | } 32 | 33 | throw $e; 34 | } 35 | } 36 | 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/Commands/Push.php: -------------------------------------------------------------------------------- 1 | sheets()->each(function (TranslationsSheet $translationsSheet) use ($pusher) { 21 | $pusher 22 | ->setTranslationsSheet($translationsSheet) 23 | ->withOutput($this->output) 24 | ->push(); 25 | 26 | $translationsSheet->api()->reset(); 27 | }); 28 | } catch (DirectoryNotFoundException $e) { 29 | $this->error($e->getMessage()); 30 | $this->error('Something is wrong. Did you just add an extra sheet ? try to re-run translation_sheet:setup!'); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Commands/Setup.php: -------------------------------------------------------------------------------- 1 | ensureConfiguredSheetsAreCreated(); 18 | 19 | $spreadsheet->sheets()->each(function (TranslationsSheet $translationsSheet) { 20 | $this->output->writeln( 21 | 'Setting up translations sheet [' . $translationsSheet->getTitle() . ']' 22 | ); 23 | 24 | $translationsSheet->api()->addBatchRequests( 25 | $translationsSheet->api()->setTabColor($translationsSheet->getId(), $translationsSheet->getTabColor()) 26 | ); 27 | }); 28 | 29 | $spreadsheet->api()->sendBatchRequests(); 30 | 31 | $this->output->writeln('Done. Spreasheet is ready.'); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Commands/Status.php: -------------------------------------------------------------------------------- 1 | isTranslationsLocked(); 17 | 18 | $label = $locked ? 'LOCKED' : 'UNLOCKED'; 19 | $style = $locked ? 'error' : 'info'; 20 | 21 | $this->line("Translations area is <$style>$label"); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Commands/Unlock.php: -------------------------------------------------------------------------------- 1 | sheets()->each(function (TranslationsSheet $translationsSheet) { 18 | $this->info("Unlocking translation sheet [{$translationsSheet->getTitle()}] :"); 19 | 20 | $translationsSheet->unlockTranslations(); 21 | $translationsSheet->api()->reset(); 22 | 23 | $this->output->writeln('Done.'); 24 | $this->output->writeln(PHP_EOL); 25 | }); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Sheet/AbstractSheet.php: -------------------------------------------------------------------------------- 1 | spreadsheet = $spreadsheet; 15 | } 16 | 17 | abstract public function getId(); 18 | 19 | abstract public function getTitle(); 20 | 21 | public function api() 22 | { 23 | return $this->spreadsheet->api(); 24 | } 25 | 26 | public function styles() 27 | { 28 | return $this->spreadsheet->sheetStyles(); 29 | } 30 | 31 | public function getSpreadsheet() 32 | { 33 | return $this->spreadsheet; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Sheet/Styles.php: -------------------------------------------------------------------------------- 1 | '#546E7A', 11 | 'foregroundColor' => '#ffffff', 12 | 'fontSize' => 10, 13 | 'fontFamily' => 'Droid Serif', 14 | 'bold' => true, 15 | 'horizontalAlignment' => 'LEFT', 16 | 'verticalAlignment' => 'MIDDLE', 17 | 'hyperlinkDisplayType' => 'PLAIN_TEXT', 18 | ]; 19 | } 20 | 21 | public function fullKeyColumn() 22 | { 23 | return [ 24 | 'backgroundColor' => '#E1F5FE', 25 | 'foregroundColor' => '#9E9E9E', 26 | 'fontSize' => 9, 27 | 'fontFamily' => 'Consolas', 28 | 'bold' => false, 29 | 'horizontalAlignment' => 'LEFT', 30 | 'verticalAlignment' => 'MIDDLE', 31 | 'hyperlinkDisplayType' => 'PLAIN_TEXT', 32 | ]; 33 | } 34 | 35 | public function lockedTranslationsColumns() 36 | { 37 | return [ 38 | 'backgroundColor' => '#ffe1e1', 39 | 'foregroundColor' => '#000000', 40 | 'fontSize' => 9, 41 | 'fontFamily' => 'Consolas', 42 | 'bold' => false, 43 | 'horizontalAlignment' => 'LEFT', 44 | 'verticalAlignment' => 'MIDDLE', 45 | 'hyperlinkDisplayType' => 'PLAIN_TEXT', 46 | ]; 47 | } 48 | 49 | public function translationsColumns() 50 | { 51 | return [ 52 | 'backgroundColor' => '#ffffff', 53 | 'foregroundColor' => '#000000', 54 | 'fontSize' => 9, 55 | 'fontFamily' => 'Droid Serif', 56 | 'bold' => false, 57 | 'horizontalAlignment' => 'LEFT', 58 | 'verticalAlignment' => 'MIDDLE', 59 | 'wrapStrategy' => 'WRAP', 60 | 'hyperlinkDisplayType' => 'PLAIN_TEXT', 61 | ]; 62 | } 63 | 64 | public function metaColumns() 65 | { 66 | return [ 67 | 'backgroundColor' => '#ededed', 68 | 'foregroundColor' => '#9E9E9E', 69 | 'fontSize' => 8, 70 | 'fontFamily' => 'Ubuntu', 71 | 'bold' => false, 72 | 'horizontalAlignment' => 'LEFT', 73 | 'verticalAlignment' => 'MIDDLE', 74 | 'hyperlinkDisplayType' => 'PLAIN_TEXT', 75 | ]; 76 | } 77 | 78 | public function translationSheetTabColor() 79 | { 80 | return '#546E7A'; 81 | } 82 | 83 | public function metaSheetTabColor() 84 | { 85 | return [0, 188, 212]; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/Sheet/TranslationsSheet.php: -------------------------------------------------------------------------------- 1 | id; 16 | } 17 | 18 | public function setId($id) 19 | { 20 | $this->id = $id; 21 | 22 | return $this; 23 | } 24 | 25 | public function getTitle() 26 | { 27 | return $this->title; 28 | } 29 | 30 | public function setTitle($title) 31 | { 32 | $this->title = $title; 33 | 34 | return $this; 35 | } 36 | 37 | public function setPath($path) 38 | { 39 | $this->path = $path; 40 | 41 | return $this; 42 | } 43 | 44 | public function getPath() 45 | { 46 | return $this->path ?? app()->make('path.lang'); 47 | } 48 | 49 | public function setTabColor($tabColor) 50 | { 51 | $this->tabColor = $tabColor; 52 | return $this; 53 | } 54 | 55 | public function getTabColor() 56 | { 57 | return $this->tabColor ?? $this->styles()->translationSheetTabColor(); 58 | } 59 | 60 | public function markAsPrimarySheet() 61 | { 62 | $this->primary = true; 63 | 64 | return $this; 65 | } 66 | 67 | public function markAsExtraSheet() 68 | { 69 | $this->primary = false; 70 | 71 | return $this; 72 | } 73 | 74 | public function isPrimarySheet() 75 | { 76 | return $this->primary; 77 | } 78 | 79 | public function isExtraSheet() 80 | { 81 | return !$this->primary; 82 | } 83 | 84 | public function coordinates() 85 | { 86 | return $this->spreadsheet->translationsSheetCoordinates($this->getId(), $this->getTitle()); 87 | } 88 | 89 | public function emptyCoordinates() 90 | { 91 | return $this->spreadsheet->translationsSheetCoordinates($this->getId(), $this->getTitle()); 92 | } 93 | 94 | public function setup() 95 | { 96 | $this->spreadsheet->api() 97 | ->addBatchRequests([ 98 | // Set properties 99 | $this->spreadsheet->api()->setSheetPropertiesRequest( 100 | $this->getId(), 101 | $this->getTitle(), 102 | $this->styles()->translationSheetTabColor() 103 | ), 104 | ]) 105 | ->sendBatchRequests(); 106 | } 107 | 108 | public function writeTranslations($translations) 109 | { 110 | $translations = array_values($translations); 111 | 112 | $this->spreadsheet->setTranslations($translations); 113 | 114 | $this->spreadsheet->api() 115 | ->writeCells($this->coordinates()->dataShortRange(), $translations); 116 | } 117 | 118 | public function readTranslations() 119 | { 120 | return $this->spreadsheet->api()->readCells($this->getId(), $this->coordinates()->dataShortRange(2, true)); 121 | } 122 | 123 | public function styleDocument() 124 | { 125 | $fullkeyRange = $this->coordinates()->fullKeyColumnRange(); 126 | $translationsRange = $this->coordinates()->translationsRange(); 127 | $metaRange = $this->coordinates()->metaColumnsRange(); 128 | 129 | $requests = [ 130 | // Header row 131 | $this->spreadsheet->api()->frozenRowRequest($this->getId()), 132 | $this->spreadsheet->api()->styleArea($this->emptyCoordinates()->headerRange(), $this->styles()->translationsHeader()), 133 | $this->spreadsheet->api()->protectRangeRequest($this->emptyCoordinates()->headerRange(), 'HEADER.'), 134 | 135 | // Full key column 136 | $this->spreadsheet->api()->frozenColumnRequest($this->getId()), 137 | $this->spreadsheet->api()->protectRangeRequest($fullkeyRange, 'FULL_KEY'), 138 | $this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), 0, 1, 450), 139 | $this->spreadsheet->api()->styleArea($fullkeyRange, $this->styles()->fullKeyColumn()), 140 | 141 | // Translations columns 142 | $this->spreadsheet->api()->styleArea($translationsRange, $this->styles()->translationsColumns()), 143 | 144 | // Meta columns 145 | $this->spreadsheet->api()->protectRangeRequest($metaRange, 'META'), 146 | $this->spreadsheet->api()->styleArea($metaRange, $this->styles()->metaColumns()), 147 | $this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), $this->coordinates()->namespaceColumnIndex(), $this->coordinates()->namespaceColumnIndex() + 1, 145), 148 | $this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), $this->coordinates()->groupColumnIndex(), $this->coordinates()->groupColumnIndex() + 1, 80), 149 | $this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), $this->coordinates()->keyColumnIndex(), $this->coordinates()->keyColumnIndex() + 1, 240), 150 | $this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), $this->coordinates()->sourceFileColumnIndex(), $this->coordinates()->sourceFileColumnIndex() + 1, 360), 151 | ]; 152 | 153 | // Fixed locales translations column width 154 | $beginAt = 1; 155 | foreach ($this->spreadsheet->getLocales() as $locale) { 156 | $requests[] = $this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), $beginAt, $beginAt + 1, 350); 157 | $beginAt++; 158 | } 159 | 160 | // Send requests 161 | $this->spreadsheet->api()->addBatchRequests($requests)->sendBatchRequests(); 162 | 163 | // Delete extra columns and rows if any 164 | try { 165 | $this->spreadsheet->api()->addBatchRequests([ 166 | $this->spreadsheet->api()->deleteRowsFrom($this->getId(), $this->coordinates()->getRowsCount()), 167 | $this->spreadsheet->api()->deleteColumnsFrom($this->getId(), $this->coordinates()->getColumnsCount()), 168 | ])->sendBatchRequests(); 169 | } catch (\Google_Service_Exception $e) { 170 | // Avoid exception failing the command 171 | // ... If there is no extra columns or rows Google api will raise an exception : 172 | // ... Invalid requests[xxx].deleteDimension: Cannot delete a column that doesn't exist ... 173 | } 174 | } 175 | 176 | public function prepareForWrite() 177 | { 178 | // We need to remove all protected ranges, to avoid any duplicates that may lead to 179 | // some weird behaviours 180 | $this->removeAllProtectedRanges(); 181 | } 182 | 183 | public function lockTranslations() 184 | { 185 | $range = $this->coordinates()->translationsRange(1, $this->spreadsheet->api()->getSheetRowCount($this->getId())); 186 | 187 | $this->api() 188 | ->addBatchRequests([ 189 | $this->spreadsheet->api()->protectRangeRequest($range, 'TRANSLATIONS'), 190 | $this->spreadsheet->api()->styleArea($range, $this->styles()->lockedTranslationsColumns()), 191 | ]) 192 | ->sendBatchRequests(); 193 | } 194 | 195 | public function unlockTranslations() 196 | { 197 | $requests = []; 198 | $range = $this->coordinates()->translationsRange(1, $this->spreadsheet->api()->getSheetRowCount($this->getId())); 199 | 200 | $protectedRanges = $this->spreadsheet->api()->getSheetProtectedRanges($this->getId(), 'TRANSLATIONS'); 201 | foreach ($protectedRanges as $protectedRange) { 202 | $requests[] = $this->spreadsheet->api()->deleteProtectedRange($protectedRange->protectedRangeId); 203 | } 204 | 205 | $requests[] = $this->spreadsheet->api()->styleArea($range, $this->styles()->translationsColumns()); 206 | 207 | $this->spreadsheet->api()->addBatchRequests($requests)->sendBatchRequests(); 208 | } 209 | 210 | public function isTranslationsLocked() 211 | { 212 | $protectedRanges = $this->spreadsheet->api()->getSheetProtectedRanges($this->getId(), 'TRANSLATIONS'); 213 | 214 | return !empty($protectedRanges) && count($protectedRanges) > 0; 215 | } 216 | 217 | public function removeAllProtectedRanges() 218 | { 219 | $protectedRanges = $this->spreadsheet->api()->getSheetProtectedRanges($this->getId()); 220 | 221 | $requests = []; 222 | foreach ($protectedRanges as $protectedRange) { 223 | $requests[] = $this->spreadsheet->api()->deleteProtectedRange($protectedRange->protectedRangeId); 224 | } 225 | 226 | if (empty($requests)) { 227 | return; 228 | } 229 | 230 | $this->spreadsheet->api()->addBatchRequests($requests)->sendBatchRequests(); 231 | } 232 | 233 | public function updateHeaderRow() 234 | { 235 | $this->api()->writeCells( 236 | $this->emptyCoordinates()->headerShortRange(), 237 | [$this->spreadsheet->getHeader()] 238 | ); 239 | } 240 | 241 | } 242 | -------------------------------------------------------------------------------- /src/Sheet/TranslationsSheetCoordinates.php: -------------------------------------------------------------------------------- 1 | sheetId = $sheetId; 26 | 27 | return $this; 28 | } 29 | 30 | public function setSheetTitle($sheetTitle) 31 | { 32 | $this->sheetTitle = $sheetTitle; 33 | 34 | return $this; 35 | } 36 | 37 | public static function emptySheet($columnsCount, $localesCount, $headerRowsCount) 38 | { 39 | $instance = new self; 40 | 41 | $instance->headerRowsCount = $headerRowsCount; 42 | $instance->dataRowsCount = 10; 43 | $instance->columnsCount = $columnsCount; 44 | $instance->localesCount = $localesCount; 45 | 46 | return $instance; 47 | } 48 | 49 | public static function sheetWithData($dataRowsCount, $columnsCount, $localesCount, $headerRowsCount) 50 | { 51 | $instance = new self; 52 | 53 | $instance->headerRowsCount = $headerRowsCount; 54 | $instance->dataRowsCount = $dataRowsCount; 55 | $instance->columnsCount = $columnsCount; 56 | $instance->localesCount = $localesCount; 57 | 58 | return $instance; 59 | } 60 | 61 | public function headerShortRange() 62 | { 63 | return $this->sheetTitle.'!A1:'.self::stringFromColumnIndex($this->getColumnsCount()).'1'; 64 | } 65 | 66 | public function headerRange() 67 | { 68 | return [ 69 | 'sheetId' => $this->sheetId, 70 | 'startRowIndex' => 0, 71 | 'endRowIndex' => 1, 72 | 'startColumnIndex' => 0, 73 | 'endColumnIndex' => $this->getColumnsCount(), 74 | ]; 75 | } 76 | 77 | public function fullKeyColumnRange() 78 | { 79 | return [ 80 | 'sheetId' => $this->sheetId, 81 | 'startRowIndex' => 1, 82 | 'endRowIndex' => $this->getRowsCount(), 83 | 'startColumnIndex' => 0, 84 | 'endColumnIndex' => 1, 85 | ]; 86 | } 87 | 88 | public function metaColumnsRange() 89 | { 90 | return [ 91 | 'sheetId' => $this->sheetId, 92 | 'startRowIndex' => 1, 93 | 'endRowIndex' => $this->getRowsCount(), 94 | 'startColumnIndex' => $this->getLocalesCount() + 1, 95 | 'endColumnIndex' => $this->getColumnsCount(), 96 | ]; 97 | } 98 | 99 | public function dataShortRange($firstRow = 2, $noLastRow = false) 100 | { 101 | $firstColumn = 'A'; 102 | $lastColumn = self::stringFromColumnIndex($this->getColumnsCount()); 103 | $lastRow = $this->getRowsCount(); 104 | 105 | return $this->sheetTitle.'!'.$firstColumn.$firstRow.':'.$lastColumn.($noLastRow ? '' : $lastRow); 106 | } 107 | 108 | public function dataRange($endRow, $firstRow = 2) 109 | { 110 | return [ 111 | 'sheetId' => $this->sheetId, 112 | 'startRowIndex' => $firstRow, 113 | 'endRowIndex' => $endRow, 114 | 'startColumnIndex' => 0, 115 | 'endColumnIndex' => $this->getColumnsCount(), 116 | ]; 117 | } 118 | 119 | public function translationsRange($firstColumn = 1, $rowCount = null) 120 | { 121 | return [ 122 | 'sheetId' => $this->sheetId, 123 | 'startRowIndex' => 1, 124 | 'endRowIndex' => $rowCount ?: $this->getRowsCount(), 125 | 'startColumnIndex' => $firstColumn, 126 | 'endColumnIndex' => $firstColumn + $this->getLocalesCount(), 127 | ]; 128 | } 129 | 130 | public function getColumnsCount() 131 | { 132 | return $this->columnsCount; 133 | } 134 | 135 | public function getRowsCount() 136 | { 137 | return $this->dataRowsCount + $this->headerRowsCount; 138 | } 139 | 140 | public function getLocalesCount() 141 | { 142 | return $this->localesCount; 143 | } 144 | 145 | public function namespaceColumnIndex() 146 | { 147 | return $this->getLocalesCount() + 1; 148 | } 149 | 150 | public function groupColumnIndex() 151 | { 152 | return $this->getLocalesCount() + 2; 153 | } 154 | 155 | public function keyColumnIndex() 156 | { 157 | return $this->getLocalesCount() + 3; 158 | } 159 | 160 | public function sourceFileColumnIndex() 161 | { 162 | return $this->getLocalesCount() + 4; 163 | } 164 | 165 | /** 166 | * String from column index. 167 | * 168 | * @see https://github.com/PHPOffice/PhpSpreadsheet/blob/master/src/PhpSpreadsheet/Cell/Coordinate.php Source of implementation. 169 | * 170 | * @license https://raw.githubusercontent.com/PHPOffice/PhpSpreadsheet/master/LICENSE LGPL (GNU LESSER GENERAL PUBLIC LICENSE) 171 | * 172 | * @param int $columnIndex Column index (A = 1) 173 | * 174 | * @return string 175 | */ 176 | public static function stringFromColumnIndex($columnIndex) 177 | { 178 | static $indexCache = []; 179 | if (! isset($indexCache[$columnIndex])) { 180 | $indexValue = $columnIndex; 181 | $base26 = null; 182 | do { 183 | $characterValue = ($indexValue % 26) ?: 26; 184 | $indexValue = ($indexValue - $characterValue) / 26; 185 | $base26 = chr($characterValue + 64).($base26 ?: ''); 186 | } while ($indexValue > 0); 187 | $indexCache[$columnIndex] = $base26; 188 | } 189 | 190 | return $indexCache[$columnIndex]; 191 | } 192 | } 193 | -------------------------------------------------------------------------------- /src/SheetPuller.php: -------------------------------------------------------------------------------- 1 | writer = $writer; 22 | 23 | $this->nullOutput(); 24 | } 25 | 26 | public function setTranslationsSheet(TranslationsSheet $translationsSheet) 27 | { 28 | $this->translationsSheet = $translationsSheet; 29 | 30 | return $this; 31 | } 32 | 33 | public function pull() 34 | { 35 | $this->output->writeln("Pulling translations from sheet [{$this->translationsSheet->getTitle()}] :"); 36 | $translations = $this->getTranslations(); 37 | 38 | $this->output->writeln(' Writing languages files :'); 39 | $this->writer 40 | ->withOutput($this->output) 41 | ->setTranslationsSheet($this->translationsSheet) 42 | ->setTranslations($translations) 43 | ->write(); 44 | 45 | $this->output->writeln(' Done.'); 46 | $this->output->writeln(PHP_EOL); 47 | } 48 | 49 | public function getTranslations() 50 | { 51 | $header = $this->translationsSheet->getSpreadsheet()->getCamelizedHeader(); 52 | 53 | $translations = $this->translationsSheet->readTranslations(); 54 | 55 | return Util::keyValues($translations, $header); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/SheetPusher.php: -------------------------------------------------------------------------------- 1 | reader = $reader; 28 | $this->transformer = $transformer; 29 | 30 | $this->nullOutput(); 31 | } 32 | 33 | public function setTranslationsSheet(TranslationsSheet $translationsSheet) 34 | { 35 | $this->translationsSheet = $translationsSheet; 36 | 37 | $this->reader->setTranslationsSheet($translationsSheet); 38 | $this->transformer->setTranslationsSheet($translationsSheet); 39 | 40 | return $this; 41 | } 42 | 43 | public function push() 44 | { 45 | $this->output->writeln("Working on translation sheet [{$this->translationsSheet->getTitle()}] :"); 46 | 47 | $this->output->writeln(' Scanning languages files'); 48 | $translations = $this->getScannedAndTransformedTranslations(); 49 | 50 | 51 | $this->output->writeln(' Preparing spreadsheet for new write operation'); 52 | $this->translationsSheet->prepareForWrite(); 53 | 54 | $this->output->writeln(' Updating header'); 55 | $this->translationsSheet->updateHeaderRow(); 56 | 57 | $this->output->writeln(' Writing translations in the spreadsheet'); 58 | $this->translationsSheet->writeTranslations($translations->all()); 59 | 60 | $this->output->writeln(' Styling document'); 61 | $this->translationsSheet->styleDocument(); 62 | 63 | $this->output->writeln('Done.'); 64 | $this->output->writeln(PHP_EOL); 65 | } 66 | 67 | public function getScannedAndTransformedTranslations() 68 | { 69 | $excludePatterns = config('translation_sheet.exclude'); 70 | 71 | return $this->transformer 72 | ->setLocales($this->translationsSheet->getSpreadsheet()->getLocales()) 73 | ->transform($this->reader->scan()) 74 | ->when(is_array($excludePatterns) && !empty($excludePatterns), function (Collection $collection) use ($excludePatterns) { 75 | return $collection->reject(function ($item) use ($excludePatterns) { 76 | return Str::is($excludePatterns, $item[0] /* full key */); 77 | })->values(); 78 | }); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/Spreadsheet.php: -------------------------------------------------------------------------------- 1 | id = $id; 29 | $this->locales = $locales; 30 | $this->api = $api ?: app(Api::class); 31 | } 32 | 33 | public function setLocales($locales) 34 | { 35 | $this->locales = $locales; 36 | 37 | return $this; 38 | } 39 | 40 | public function setTranslations($translations) 41 | { 42 | $this->translations = $translations; 43 | 44 | return $this; 45 | } 46 | 47 | public function getTranslations() 48 | { 49 | return $this->translations; 50 | } 51 | 52 | public function getTranslationsCount() 53 | { 54 | return count($this->translations); 55 | } 56 | 57 | public function getId() 58 | { 59 | return $this->id; 60 | } 61 | 62 | public function getUrl() 63 | { 64 | return 'https://docs.google.com/spreadsheets/d/' . $this->id; 65 | } 66 | 67 | public function getLocales() 68 | { 69 | return $this->locales; 70 | } 71 | 72 | public function getLocalesCount() 73 | { 74 | return count($this->locales); 75 | } 76 | 77 | public function getHeader() 78 | { 79 | return array_merge( 80 | array_merge(['Full key'], $this->getLocales()), 81 | ['Namespace', 'Group', 'Key', 'Source file'] 82 | ); 83 | } 84 | 85 | public function getCamelizedHeader() 86 | { 87 | return array_map(function ($item) { 88 | if (in_array($item, Util::asArray($this->getLocales()))) { 89 | return $item; 90 | } 91 | 92 | return Str::camel($item); 93 | }, $this->getHeader()); 94 | } 95 | 96 | public function getHeaderColumnsCount() 97 | { 98 | $header = $this->getHeader(); 99 | 100 | return count($header); 101 | } 102 | 103 | public function getHeaderRowsCount() 104 | { 105 | return 1; 106 | } 107 | 108 | /** 109 | * @param $sheetId 110 | * @param $sheetTitle 111 | * 112 | * @return TranslationsSheetCoordinates 113 | */ 114 | public function translationsEmptySheetCoordinates($sheetId, $sheetTitle) 115 | { 116 | return TranslationsSheetCoordinates::emptySheet( 117 | $this->getHeaderColumnsCount(), 118 | $this->getLocalesCount(), 119 | $this->getHeaderColumnsCount() 120 | )->setSheetId($sheetId)->setSheetTitle($sheetTitle); 121 | } 122 | 123 | /** 124 | * @param $sheetId 125 | * @param $sheetTitle 126 | * @return TranslationsSheetCoordinates 127 | */ 128 | public function translationsSheetCoordinates($sheetId, $sheetTitle) 129 | { 130 | return TranslationsSheetCoordinates::sheetWithData( 131 | $this->getTranslationsCount(), 132 | $this->getHeaderColumnsCount(), 133 | $this->getLocalesCount(), 134 | $this->getHeaderRowsCount() 135 | )->setSheetId($sheetId)->setSheetTitle($sheetTitle); 136 | } 137 | 138 | public function sheetStyles() 139 | { 140 | return new Styles; 141 | } 142 | 143 | public function deleteAllSheets() 144 | { 145 | // We need to create a black sheet, cause we cannot delete all sheets 146 | $this->api() 147 | ->addBatchRequests([ 148 | $this->api()->addBlankSheet() 149 | ]) 150 | ->sendBatchRequests(); 151 | 152 | // Delete all sheet and keep the last created one 153 | $this->api() 154 | ->addBatchRequests( 155 | collect($this->api()->freshSheets())->slice(0, -1)->map(function ($sheet) { 156 | return $this->api()->deleteSheetRequest($sheet['properties']['sheetId']); 157 | })->toArray() 158 | ) 159 | ->sendBatchRequests(); 160 | 161 | $this->api()->forgetSheets(); 162 | } 163 | 164 | /** 165 | * Return api instance initialized with the spreadsheet ID. 166 | * 167 | * @return Api 168 | */ 169 | public function api() 170 | { 171 | return $this->api->setSpreadsheetId($this->getId()); 172 | } 173 | 174 | /** 175 | * Returns collection of the spreadsheets represented by TranslationSheet objects. 176 | * 177 | * @return Collection 178 | */ 179 | public function sheets() 180 | { 181 | $this->api()->forgetSheets()->getSheets(); 182 | 183 | return $this->configuredSheets()->map(function (TranslationsSheet $translationsSheet) { 184 | if ($sheet = $this->api()->getSheetByTitle($translationsSheet->getTitle())) { 185 | $translationsSheet->setId( 186 | data_get($sheet, 'properties.sheetId') 187 | ); 188 | } 189 | 190 | return $translationsSheet; 191 | }); 192 | } 193 | 194 | public function configuredSheets() 195 | { 196 | return collect([static::primaryTranslationSheet()]) 197 | ->merge( 198 | collect(config('translation_sheet.extra_sheets')) 199 | ->map(function ($sheetConfig) { 200 | /** @var TranslationsSheet $instance */ 201 | $instance = resolve(TranslationsSheet::class); 202 | 203 | return $instance 204 | ->markAsExtraSheet() 205 | ->setTitle($sheetConfig['name']) 206 | ->setPath($sheetConfig['path']) 207 | ->setTabColor($sheetConfig['tabColor']); 208 | }) 209 | ); 210 | } 211 | 212 | public function configuredExtraSheets() 213 | { 214 | // Remove the first one. (aka. the translations required default sheet). 215 | return $this->configuredSheets()->slice(1); 216 | } 217 | 218 | public function hasConfiguredExtaSheets() 219 | { 220 | return $this->configuredExtraSheets()->count() > 0; 221 | } 222 | 223 | public function ensureConfiguredSheetsAreCreated() 224 | { 225 | $googleSheets = collect($this->api()->forgetSheets()->getSheets()); 226 | 227 | // By default, spreadsheet has 1 required sheet. This will match translation-sheet 228 | // default translations sheet. 229 | $requests = [ 230 | $this->api()->setSheetTitle( 231 | $firstSheetId = data_get(collect($googleSheets)->first(), 'properties.sheetId'), 232 | config('translation_sheet.primary_sheet.name', 'Translations') 233 | ) 234 | ]; 235 | 236 | // Then, we need to create any extra sheets before we can use them. 237 | $this->configuredExtraSheets()->each(function (TranslationsSheet $translationsSheet) use (&$requests) { 238 | if ($this->api()->getSheetByTitle($translationsSheet->getTitle())) { 239 | return; 240 | } 241 | 242 | $requests[] = $this->api()->addBlankSheet($translationsSheet->getTitle()); 243 | }); 244 | 245 | $this->api()->addBatchRequests($requests)->sendBatchRequests(); 246 | } 247 | 248 | public static function primaryTranslationSheet() 249 | { 250 | /** @var TranslationsSheet $instance */ 251 | $instance = resolve(TranslationsSheet::class); 252 | 253 | return $instance 254 | ->markAsPrimarySheet() 255 | ->setTitle(config('translation_sheet.primary_sheet.name', 'Translations')); 256 | } 257 | 258 | } 259 | -------------------------------------------------------------------------------- /src/Translation/Item.php: -------------------------------------------------------------------------------- 1 | namespace = $values['namespace']; 21 | $item->locale = $values['locale']; 22 | $item->group = $values['group']; 23 | $item->key = $values['key']; 24 | $item->full_key = $values['full_key']; 25 | $item->value = $values['value']; 26 | $item->source_file = $values['source_file']; 27 | $item->status = $values['status']; 28 | 29 | return $item; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Translation/Reader.php: -------------------------------------------------------------------------------- 1 | app = $app; 50 | $this->files = $files; 51 | } 52 | 53 | /** 54 | * Set the translation sheets for reader. 55 | * 56 | * @param TranslationsSheet $translationSheet 57 | * @return Reader 58 | */ 59 | public function setTranslationsSheet(TranslationsSheet $translationSheet) 60 | { 61 | $this->translationSheet = $translationSheet; 62 | 63 | return $this; 64 | } 65 | 66 | /** 67 | * Set reader locale. 68 | * 69 | * @param $locale 70 | * @return $this 71 | */ 72 | public function setLocale($locale) 73 | { 74 | $this->locale = $locale; 75 | 76 | return $this; 77 | } 78 | 79 | /** 80 | * Scan modules, app and overridden packages lang 81 | * and return all defined translations. 82 | * 83 | * @return Collection 84 | * @return array 85 | */ 86 | public function scan() 87 | { 88 | $this->translations = new Collection; 89 | 90 | $this->scanDirectory($this->translationSheet->getPath()); 91 | 92 | return $this->translations; 93 | } 94 | 95 | /** 96 | * Scan a directory. 97 | * 98 | * @param string $path to directory to scan 99 | */ 100 | protected function scanDirectory($path) 101 | { 102 | foreach ($this->files->directories($path) as $directory) { 103 | if ($this->isVendorDirectory($directory)) { 104 | $this->scanVendorDirectory($directory); 105 | } else { 106 | $this->loadTranslationsInDirectory($directory, $this->getLocaleFromDirectory($directory), null); 107 | } 108 | } 109 | 110 | foreach ($this->files->files($path) as $file) { 111 | $this->loadTranslations($file->getBasename('.json'), '*', '*', $file); 112 | } 113 | } 114 | 115 | /** 116 | * Scan overridden packages lang. 117 | * 118 | * @param $vendorsDirectory 119 | */ 120 | private function scanVendorDirectory($vendorsDirectory) 121 | { 122 | foreach ($this->files->directories($vendorsDirectory) as $vendorPath) { 123 | $namespace = basename($vendorPath); 124 | foreach ($this->files->directories($vendorPath) as $localePath) { 125 | $this->loadTranslationsInDirectory($localePath, basename($localePath), $namespace); 126 | } 127 | } 128 | } 129 | 130 | /** 131 | * Load all directory file translation (multiple group) into translations collection. 132 | * 133 | * @param $directory 134 | * @param $locale 135 | * @param $namespace 136 | */ 137 | private function loadTranslationsInDirectory($directory, $locale, $namespace) 138 | { 139 | if (!$this->requestedLocale($locale)) { 140 | return; 141 | } 142 | 143 | foreach ($this->files->files($directory) as $file) { 144 | $info = pathinfo($file); 145 | $group = $info['filename']; 146 | $this->loadTranslations($locale, $group, $namespace, $file); 147 | } 148 | } 149 | 150 | /** 151 | * Load file translation (group) into translations collection. 152 | * 153 | * @param $locale 154 | * @param $group 155 | * @param $namespace 156 | * @param $file 157 | */ 158 | private function loadTranslations($locale, $group, $namespace, $file) 159 | { 160 | if ($this->translationSheet->isExtraSheet()) { 161 | $translations = Arr::dot(json_decode(file_get_contents($file), true)); 162 | } else { 163 | $translations = Arr::dot($this->app['translator']->getLoader()->load($locale, $group, $namespace)); 164 | } 165 | 166 | foreach ($translations as $key => $value) { 167 | 168 | // Avoid break in this case : 169 | // Case of 'car.messages = []', the key 'messages' is specified in the translation 170 | // file but no items defined inside. 171 | if (is_array($value) && count($value) === 0) { 172 | continue; 173 | } 174 | 175 | $entity = new Item; 176 | $entity->namespace = $namespace; 177 | $entity->locale = $locale; 178 | $entity->group = $group; 179 | $entity->key = $key; 180 | $entity->full_key = $this->fullKey($namespace, $group, $key); 181 | $entity->value = (string)$value; 182 | $entity->source_file = $this->sourceFile($file); 183 | 184 | $this->translations->push($entity); 185 | } 186 | } 187 | 188 | /** 189 | * Return a full lang key. 190 | * 191 | * @param $namespace 192 | * @param $group 193 | * @param $key 194 | * @return string 195 | */ 196 | private function fullKey($namespace, $group, $key) 197 | { 198 | return 199 | ($namespace ? "$namespace::" : '') 200 | . $group . '.' 201 | . $key; 202 | } 203 | 204 | /** 205 | * Return relative path of language file. 206 | * 207 | * @param $file 208 | * @return mixed 209 | */ 210 | private function sourceFile($file) 211 | { 212 | return $this->toRelative(realpath($file)); 213 | } 214 | 215 | /** 216 | * Return relative path related to base_path(). 217 | * 218 | * @param $path 219 | * @return string 220 | */ 221 | private function toRelative($path) 222 | { 223 | $relative = str_replace($this->translationSheet->getPath() . DIRECTORY_SEPARATOR, '', $path); 224 | $relative = str_replace('\\', '/', $relative); 225 | 226 | return $relative; 227 | } 228 | 229 | /** 230 | * Determine if a found locale is requested for scanning. 231 | * If $this->locale is not set, we assume that all the locales were requested. 232 | * 233 | * @param string $locale the locale to check 234 | * @return bool 235 | */ 236 | private function requestedLocale($locale) 237 | { 238 | if (empty($this->locale)) { 239 | return true; 240 | } 241 | 242 | return $locale === $this->locale; 243 | } 244 | 245 | /** 246 | * Return locale from directory 247 | * ie. resources/lang/en -> en. 248 | * 249 | * @param $directory 250 | * @return string 251 | */ 252 | private function getLocaleFromDirectory($directory) 253 | { 254 | return basename($directory); 255 | } 256 | 257 | /** 258 | * Return true if it is the vendor directory. 259 | * 260 | * @param $directory 261 | * @return bool 262 | */ 263 | private function isVendorDirectory($directory) 264 | { 265 | return basename($directory) === 'vendor'; 266 | } 267 | } 268 | -------------------------------------------------------------------------------- /src/Translation/Transformer.php: -------------------------------------------------------------------------------- 1 | locales = $locales; 26 | 27 | return $this; 28 | } 29 | 30 | /** 31 | * Set the translation sheets that we are working on. 32 | * 33 | * @param TranslationsSheet $translationSheet 34 | * @return Transformer 35 | */ 36 | public function setTranslationsSheet(TranslationsSheet $translationSheet) 37 | { 38 | $this->translationSheet = $translationSheet; 39 | 40 | return $this; 41 | } 42 | 43 | public function transform(Collection $translations) 44 | { 45 | return $translations 46 | ->sortBy('locale') 47 | ->groupBy('full_key') 48 | ->map(function (Collection $translation) { 49 | $firstLocale = $translation->first(); 50 | 51 | $row = []; 52 | $row['full_key'] = $firstLocale->full_key; 53 | 54 | $translation = $translation 55 | ->groupBy('locale') 56 | ->map(function (Collection $item) { 57 | return $item->first(); 58 | }); 59 | 60 | $translation = $this->ensureWeHaveAllLocales($translation); 61 | 62 | $localesValues = []; 63 | foreach ($this->locales as $locale) { 64 | $item = $translation->get($locale); 65 | $value = !is_null($item) && isset($item->value) ? $item->value : ''; 66 | $localesValues [$locale] = $value; 67 | } 68 | 69 | $row = array_merge($row, $localesValues); 70 | 71 | $row = array_merge($row, [ 72 | 'namespace' => !is_null($firstLocale->namespace) ? $firstLocale->namespace : '', 73 | 'group' => !is_null($firstLocale->group) ? $firstLocale->group : '', 74 | 'key' => $firstLocale->key, 75 | 'source_file' => $this->formatSourceFile($firstLocale), 76 | ]); 77 | 78 | return array_values($row); 79 | }) 80 | ->sortBy(function ($product, $key) { 81 | return $key; 82 | }) 83 | ->values(); 84 | } 85 | 86 | private function ensureWeHaveAllLocales(Collection $translation) 87 | { 88 | foreach ($this->locales as $locale) { 89 | if (!$translation->get($locale)) { 90 | $translation->put($locale, new Item); 91 | } 92 | } 93 | 94 | return $translation; 95 | } 96 | 97 | private function formatSourceFile(Item $translationItem) 98 | { 99 | if ($this->translationSheet->isPrimarySheet() && Str::endsWith($translationItem->source_file, ['.json'])) { 100 | return '{locale}.json'; 101 | } 102 | 103 | return str_replace($translationItem->locale . '/', '{locale}/', $translationItem->source_file); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/Translation/Writer.php: -------------------------------------------------------------------------------- 1 | spreadsheet = $spreadsheet; 36 | $this->files = $files; 37 | $this->app = $app; 38 | 39 | $this->nullOutput(); 40 | } 41 | 42 | /** 43 | * Set the translation sheets for reader. 44 | * 45 | * @param TranslationsSheet $translationSheet 46 | * @return Writer 47 | */ 48 | public function setTranslationsSheet(TranslationsSheet $translationSheet) 49 | { 50 | $this->translationSheet = $translationSheet; 51 | 52 | return $this; 53 | } 54 | 55 | 56 | public function setTranslations($translations) 57 | { 58 | $this->translations = $translations; 59 | 60 | return $this; 61 | } 62 | 63 | public function write() 64 | { 65 | $this 66 | ->groupTranslationsByFile() 67 | ->each(function ($items, $sourceFile) { 68 | 69 | if ($this->files->extension($sourceFile) === 'json') { 70 | $this->writeJsonFile( 71 | $this->translationSheet->getPath() . '/' . $sourceFile, 72 | $items 73 | ); 74 | return; 75 | } 76 | 77 | $this->writeFile( 78 | $this->translationSheet->getPath() . '/' . $sourceFile, 79 | $items 80 | ); 81 | }); 82 | } 83 | 84 | protected function writeFile($file, $items) 85 | { 86 | $this->output->writeln(' php ' . $file); 87 | 88 | $content = sprintf('files->isDirectory($dir = dirname($file))) { 91 | $this->files->makeDirectory($dir, 0755, true); 92 | } 93 | 94 | $this->files->put($file, $content); 95 | } 96 | 97 | protected function writeJsonFile($file, $items) 98 | { 99 | $this->output->writeln(' json ' . $file); 100 | 101 | if (!$this->files->isDirectory($dir = dirname($file))) { 102 | $this->files->makeDirectory($dir, 0755, true); 103 | } 104 | $this->files->put($file, json_encode($items, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL); 105 | } 106 | 107 | protected function groupTranslationsByFile() 108 | { 109 | $items = $this 110 | ->translations 111 | ->groupBy('sourceFile') 112 | ->map(function ($fileTranslations) { 113 | return $this->buildTranslationsForFile($fileTranslations); 114 | }); 115 | 116 | // flatten does not seem to work for every case. !!! refactor !!! 117 | $result = []; 118 | foreach ($items as $subitems) { 119 | $result = array_merge($result, $subitems); 120 | } 121 | 122 | return new Collection($result); 123 | } 124 | 125 | protected function buildTranslationsForFile($fileTranslations) 126 | { 127 | $files = []; 128 | $locales = $this->spreadsheet->getLocales(); 129 | 130 | foreach ($locales as $locale) { 131 | foreach ($fileTranslations as $translation) { 132 | 133 | // We will only write non empty translations 134 | // For instance, we have `app.title` that is the same for each locale, 135 | // We dont want to translate it to every locale, and prefer letting 136 | // Laravel default back to the default locale. 137 | if ($this->skipToDefault($translation, $locale)) { 138 | continue; 139 | } 140 | 141 | $localeFile = $this->fileIsJson($translation['sourceFile']) ? 142 | $locale . '.json' : 143 | str_replace('{locale}/', $locale . '/', $translation['sourceFile']); 144 | 145 | if (empty($files[$localeFile])) { 146 | $files[$localeFile] = []; 147 | } 148 | 149 | if ($this->fileIsJson($translation['sourceFile'])) { 150 | $files[$localeFile][$translation['key']] = $translation[$locale]; 151 | } else { 152 | Arr::set($files[$localeFile], $translation['key'], $translation[$locale]); 153 | } 154 | } 155 | } 156 | 157 | return $files; 158 | } 159 | 160 | protected function skipToDefault($translation, $locale) 161 | { 162 | if (!isset($translation[$locale])) { 163 | return true; 164 | } 165 | 166 | return empty($translation[$locale]) && $this->fileIsJson($translation['sourceFile']); 167 | } 168 | 169 | protected function fileIsJson($sourceFile) 170 | { 171 | return $sourceFile === '{locale}.json'; 172 | } 173 | 174 | } 175 | -------------------------------------------------------------------------------- /src/TranslationSheetServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 22 | __DIR__.'/../config/config.php' => config_path('translation_sheet.php'), 23 | ], 'config'); 24 | } 25 | 26 | public function register() 27 | { 28 | $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'translation_sheet'); 29 | 30 | $this->registerGoogleApiClient(); 31 | 32 | $this->registerSpreadsheet(); 33 | 34 | $this->registerCommands(); 35 | } 36 | 37 | private function registerGoogleApiClient() 38 | { 39 | $this->app->singleton(Client::class, function () { 40 | return Client::create( 41 | $this->app['config']['translation_sheet.serviceAccountCredentialsFile'], 42 | $this->app['config']['translation_sheet.googleApplicationName'] 43 | ); 44 | }); 45 | } 46 | 47 | private function registerSpreadsheet() 48 | { 49 | $this->app->singleton(Spreadsheet::class, function () { 50 | return new Spreadsheet( 51 | $this->app['config']['translation_sheet.spreadsheetId'], 52 | Util::asArray($this->app['config']['translation_sheet.locales']) 53 | ); 54 | }); 55 | } 56 | 57 | private function registerCommands() 58 | { 59 | $this->commands([ 60 | Setup::class, 61 | Push::class, 62 | Pull::class, 63 | Prepare::class, 64 | Lock::class, 65 | Unlock::class, 66 | Status::class, 67 | Open::class, 68 | Publish::class, 69 | ]); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/Util.php: -------------------------------------------------------------------------------- 1 | $value) { 18 | $r[] = "$indent " 19 | .($indexed ? '' : self::varExport($key).' => ') 20 | .self::varExport($value, "$indent "); 21 | } 22 | 23 | return "[\n".implode(",\n", $r)."\n".$indent.']'; 24 | case 'boolean': 25 | return $var ? 'true' : 'false'; 26 | default: 27 | return var_export($var, true); 28 | } 29 | } 30 | 31 | public static function keyValues($values, $keys) 32 | { 33 | $values = $values instanceof Collection ? $values : new Collection($values); 34 | 35 | return $values->map(function ($values) use ($keys) { 36 | return array_combine($keys, $values); 37 | }); 38 | } 39 | 40 | public static function asArray($value) 41 | { 42 | if (is_array($value)) { 43 | return $value; 44 | } 45 | 46 | return array_filter(array_map(function ($item) { 47 | return trim($item); 48 | }, explode(',', $value))); 49 | } 50 | } 51 | --------------------------------------------------------------------------------