{"id":22974146,"url":"https://github.com/diversen/simple-php-translation","last_synced_at":"2025-07-22T10:33:00.642Z","repository":{"id":36102796,"uuid":"40404209","full_name":"diversen/simple-php-translation","owner":"diversen","description":"A simple composer enabled PHP package for doing fast and simple translations and extracting translations from source files.","archived":false,"fork":false,"pushed_at":"2023-10-21T20:17:22.000Z","size":91,"stargazers_count":20,"open_issues_count":0,"forks_count":10,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-19T10:16:47.305Z","etag":null,"topics":["auto-translation","extract-strings","php-translation","translation","translation-key"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/diversen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-08-08T13:47:27.000Z","updated_at":"2025-01-04T02:38:24.000Z","dependencies_parsed_at":"2024-06-21T13:03:56.513Z","dependency_job_id":"ddef116c-406b-4932-8717-485657bb0b18","html_url":"https://github.com/diversen/simple-php-translation","commit_stats":{"total_commits":70,"total_committers":3,"mean_commits":"23.333333333333332","dds":0.02857142857142858,"last_synced_commit":"7d84c96ec4638b25b882b66a870b855ac062cc68"},"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"purl":"pkg:github/diversen/simple-php-translation","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diversen%2Fsimple-php-translation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diversen%2Fsimple-php-translation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diversen%2Fsimple-php-translation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diversen%2Fsimple-php-translation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/diversen","download_url":"https://codeload.github.com/diversen/simple-php-translation/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diversen%2Fsimple-php-translation/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265162954,"owners_count":23720868,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["auto-translation","extract-strings","php-translation","translation","translation-key"],"created_at":"2024-12-15T00:00:16.522Z","updated_at":"2025-07-13T15:33:30.565Z","avatar_url":"https://github.com/diversen.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Brief Overview\n\nThe `simple-php-translation` is a simple solution for adding \ntranslations to your PHP apps.\n\nInstall: \n\n    composer require diversen/simple-php-translation\n\nIf you want to use google auto translate, require the following:\n\n    composer require google/cloud-translate\n\nTranslations are placed in files named:\n\n    lang/en/language.php\n    lang/da/language.php\n\nE.g. inside your app `test_app`\n\n    test_app/lang/en/language.php\n\nThe `language.php` file could consist of this:\n\n~~~.php\n$LANG = array ();\n$LANG['Welcome to my blog'] = 'Welcome to my blog';\n~~~\n\nA Danish translation could be found in: \n\n    test_app/lang/da/language.php\n\nAnd this `language.php` could consist of: \n\n~~~.php\n    $LANG = array ();\n    $LANG['Welcome to my blog'] = 'Velkommen til min blog';\n~~~\n\n# Load language\n\n~~~.php\nuse Diversen\\Lang;\n\n$l = new Lang();\n\n// Most often all translations are placed in a single folder\n$l-\u003esetSingleDir(\"test_app\");\n\n// But you can also set dirs, and look for language files inside multiple language dirs:\n// $l-\u003esetDirsInsideDir(\"modules/\");\n\n// load language. E.g. danish ('da')\n// $l-\u003eloadLanguage('da');\n\n// Or english\n// e.g. test_app/lang/da/language.php\n$l-\u003eloadLanguage('en');\n\n// now all language files are loaded, and we can translate\n~~~\n\n# Translate\n\n~~~.php\n\n// simple\n\nuse Diversen\\Lang;\n\necho Lang::translate('Here is a text');\n\n// with substitution and a span to indicate that a part of a string should not be translated\n\necho Lang::translate('User with ID \u003cspan class=\"notranslate\"\u003e{ID}\u003c/span\u003e has been locked!', array ('ID' =\u003e $id))\n\n~~~\n\n# Extract strings \n\nThis will extract all `Lang::translate` calls, and add new values to translation files. \n\n~~~.php\nuse Diversen\\Translate\\Extractor;\n\n// same pattern as above for extraction\n\n$e = new Extractor();\n$e-\u003edefaultLanguage ='en'; // which language will we extract to\n\n// Most often you will just use a single dir. Like this\n$e-\u003esetSingleDir(\"test_app\");\n\n// Set multiple dirs, like this:\n// This will create translation folders in e.g. modules/blog, modules/account\n// $e-\u003esetDirsInsideDir('modules/');\n\n$e-\u003eupdateLang();\n~~~\n\n\u003e The `$e-\u003eupdateLang()` call only add new strings found in the source, and remove\n\u003e strings that are removed from the source. It also knows if you have changed \n\u003e the value of a translation key, then it will leave the value as it is. \n\u003e It only updates the translation files, when a new key value is found.\n\n# Auto translate using google translate API\n\nYou will need to setup google cloud translation: \n\n[https://cloud.google.com/translate/docs/basic/setup-basic](https://cloud.google.com/translate/docs/basic/setup-basic)\n\nThen require the composer package `google/cloud-translate`\n\n    composer require google/cloud-translate\n\nRemember to export the GOOGLE_APPLICATION_CREDENTIALS or you will not be able to run the script. \n\n~~~.php\n\n// same pattern as above for google auto translation.\nuse Diversen\\Translate\\GoogleTranslate;\n\ninclude_once \"vendor/autoload.php\";\n\n// Google translator needs this. Substitue with path to your own .json file  \nputenv(\"GOOGLE_APPLICATION_CREDENTIALS=config-locale/pebble-2c949028ebcc.json\");\n\n$t = new GoogleTranslate();\n$t-\u003etarget = 'da'; // danish\n$t-\u003esource = 'en';\n\n$t-\u003esetSingleDir(\"app\");\n\n// Or set multiple dirs like this:\n// This will create translation folders in e.g. modules/blog, modules/account\n// $e-\u003esetDirsInsideDir('modules/');\n\n$t-\u003eupdateLang();\n\n~~~\n\n# Demo \n\nInside the [test_app/](test_app/) directory, there is a small php app consisting of \none PHP file: [test_app/index.php](test_app/index.php). There is also included javascript\nin this file. The javascript `Lang.translate` method will also be extracted when using\nthe `Extractor` class. \n\nThe Javascript file [test_app/js/lang.js](test_app/js/lang.js) will do the translation. \n\nStart the app:\n\n    php -S localhost:8000 -t test_app\n\nVisit http://localhost:8000\n\nYou can also test the danish translation at: \n\nhttp://localhost:8000?lang=da\n\nI there is no translations then any string will get the \"NT: \" (Not Translated) prefix. \n\nYou can test this by removing the `en` language file directory:\n\n    rm -rf test_app/lang/en\n\nExtract the english translation `en` using the script\n[test/extract.php](test/extract.php).\n\n    php test/extract.php\n\nReload the browser. \n\nNow all translation are loaded from a file and the prefix 'NT: ' is \nremoved. \n\nThere is also a small script for translating into danish (`da`). \n[test/google_translate.php](test/google_translate.php)\n\nIn order to use this script, you will need to setup a `google service account`. \nThen change this part of the script: \n\n    putenv(\"GOOGLE_APPLICATION_CREDENTIALS=google_json/pebble-2c949028ebcc.json\");\n\nTo: \n\n    putenv(\"GOOGLE_APPLICATION_CREDENTIALS=path/to/service-account-your-key.json\");\n\nNow you can run: \n\n    php test/google_translate.php\n\nThe translation will look like this:\n[test_app/lang/da/language.php](test_app/lang/da/language.php)\n\nFinally there is [test/to_js.php](test/to_js.php) script which translates into \njs module exports, which then can be loaded and translated by the EMS module\n[test_app/js/lang.js](test_app/js/lang.js)\n\n# License\n\nMIT © [Dennis Iversen](https://github.com/diversen)\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiversen%2Fsimple-php-translation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdiversen%2Fsimple-php-translation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiversen%2Fsimple-php-translation/lists"}