{"id":21771142,"url":"https://github.com/initphp/translator","last_synced_at":"2025-04-13T16:41:20.673Z","repository":{"id":62549366,"uuid":"484907101","full_name":"InitPHP/Translator","owner":"InitPHP","description":"This library; It is a micro library that will allow you to add multi-language support to your projects or libraries.","archived":false,"fork":false,"pushed_at":"2022-04-24T15:43:14.000Z","size":8,"stargazers_count":3,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-27T07:35:52.404Z","etag":null,"topics":["multi-language-support","php","php7","translator"],"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/InitPHP.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-04-24T02:42:07.000Z","updated_at":"2024-07-25T18:10:52.000Z","dependencies_parsed_at":"2022-11-03T01:30:32.737Z","dependency_job_id":null,"html_url":"https://github.com/InitPHP/Translator","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/InitPHP%2FTranslator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/InitPHP%2FTranslator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/InitPHP%2FTranslator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/InitPHP%2FTranslator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/InitPHP","download_url":"https://codeload.github.com/InitPHP/Translator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248747050,"owners_count":21155377,"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":["multi-language-support","php","php7","translator"],"created_at":"2024-11-26T14:15:11.986Z","updated_at":"2025-04-13T16:41:20.644Z","avatar_url":"https://github.com/InitPHP.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# InitPHP Translator\n\nThis library; It is a micro library that will allow you to add multi-language support to your projects or libraries.\n\n[![Latest Stable Version](http://poser.pugx.org/initphp/translator/v)](https://packagist.org/packages/initphp/translator) [![Total Downloads](http://poser.pugx.org/initphp/translator/downloads)](https://packagist.org/packages/initphp/translator) [![Latest Unstable Version](http://poser.pugx.org/initphp/translator/v/unstable)](https://packagist.org/packages/initphp/translator) [![License](http://poser.pugx.org/initphp/translator/license)](https://packagist.org/packages/initphp/translator) [![PHP Version Require](http://poser.pugx.org/initphp/translator/require/php)](https://packagist.org/packages/initphp/translator)\n\n## Requirements\n\n- PHP 7.4 or higher\n\n## Installation\n\n```\ncomposer require initphp/translator\n```\n\n## Usage\n\n```php\nrequire_once \"vendor/autoload.php\";\nuse \\InitPHP\\Translator\\Translator;\n\n$lang = new Translator();\n$lang-\u003esetDir(__DIR__ . '/languages/')\n    -\u003esetDefault('en');\n\n$lang-\u003echange('tr'); // Set Current Language\n\necho $lang-\u003e_r('hello');\n```\n\nWhat does a language file look like?\n\n```php\n\u003c?php\nreturn [\n    'hello'     =\u003e 'Hello {user}',\n    'today'     =\u003e 'It\\'s {day}',\n];\n```\n\n### File? Directory?\n\nYou can use a single file for each language or multiple files under a directory. \n\n#### Use File\n\nIf you are going to use a single file for a language; Your directory structure will look something like this;\n\n```\n/languages/\n    en.php\n    tr.php\n    fr.php\n```\n\nYour code looks like the following;\n\n```php\nrequire_once \"vendor/autoload.php\";\nuse \\InitPHP\\Translator\\Translator;\n\n$lang = new Translator;\n$lang-\u003euseFile(); // Note that it is used first of all.\n$lang-\u003esetDir(__DIR__ . '/languages/')\n    -\u003esetDefault('en');\n\necho $lang-\u003e_r('hello');\n```\n\n#### Use Directory\n\nIf you want to use directories that contain multiple files for each language, your directory structure will be something like this;\n\n```\n/languages/\n    en/\n        user.php\n        admin.php\n        profile.php\n    tr/\n        user.php\n        admin.php\n        profile.php\n```\n\nYour code looks like the following;\n\n```php\nrequire_once \"vendor/autoload.php\";\nuse \\InitPHP\\Translator\\Translator;\n\n$lang = new Translator;\n$lang-\u003euseDirectory(); // Note that it is used first of all.\n$lang-\u003esetDir(__DIR__ . '/languages/')\n    -\u003esetDefault('en');\n\n// The filename and keyname are separated by dots.\n// Example : \"filename.key\"\necho $lang-\u003e_r('user.hello');\n```\n\n### Methods\n\n#### `setDir()`\n\nDefines the full path to the parent directory where the language files are kept.\n\n**Structure :**\n\n```php\npublic function setDir(string $dir): self\n```\n\n#### `setDefault()`\n\nDefines the main translation language to be used by default if the desired translation in the current language is not found.\n\n**Structure :**\n\n```php\npublic function setDefault(string $default): self\n```\n\n_Note :_ If a text is given by default during use; this library uses the default string you provided at the time of use.\n\n#### `useFile()` and `useDirectory()`\n\nThese two methods; tells you whether to use a single php file or a directory for localization. [See \"File? Directory?\" above for more.](#file-directory)\n\n_Note :_ If not specified, the file system is used by default.\n\n**Structures :**\n\n```php\npublic function useFile(): self;\n\npublic function useDirectory(): self;\n```\n\n#### `change()`\n\nChanges the current language and loads the desired language if it is not already installed.\n\n**Structure :**\n\n```php\npublic function change(string $current): self\n```\n\n#### `_r()`\n\nReturns the desired translation value.\n\n**Structure :**\n\n```php\npublic function _r(string $key, ?string $default = null, array $context = []): string\n```\n\n- `$key` : The key to the desired translation.\n- `$default` : The string to substitute if the requested translation is not found.\n- `$context` : An associative array that reports the value of placeholders, if any, in the translation.\n\n#### `_e()`\n\nOutputs the desired value directly.\n\n- `$lang-\u003e_e('hello')` = `echo $lang-\u003e_r('hello')`\n\n**Structure :**\n\n```php\npublic function _e(string $key, ?string $default = null, array $context = []): void\n```\n\n## Getting Help\n\nIf you have questions, concerns, bug reports, etc, please file an issue in this repository's Issue Tracker.\n\n## Contributing\n\n\u003e All contributions to this project will be published under the MIT License. By submitting a pull request or filing a bug, issue, or feature request, you are agreeing to comply with this waiver of copyright interest.\n\n- Fork it ( https://github.com/initphp/translator/fork )\n- Create your feature branch (`git checkout -b my-new-feature`)\n- Commit your changes (`git commit -am \"Add some feature\"`)\n- Push to the branch (`git push origin my-new-feature`)\n- Create a new Pull Request\n\n## Credits\n\n- [Muhammet ŞAFAK](https://www.muhammetsafak.com.tr) \u003c\u003cinfo@muhammetsafak.com.tr\u003e\u003e\n\n## License\n\nCopyright \u0026copy; 2022 [MIT License](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finitphp%2Ftranslator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finitphp%2Ftranslator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finitphp%2Ftranslator/lists"}