{"id":21722698,"url":"https://github.com/czukowski/i18n_plurals_nette","last_synced_at":"2025-04-12T21:51:05.778Z","repository":{"id":56961017,"uuid":"62565459","full_name":"czukowski/I18n_Plurals_Nette","owner":"czukowski","description":"Nette Framework adapter package for I18n module.","archived":false,"fork":false,"pushed_at":"2018-12-11T10:02:55.000Z","size":30,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-26T15:54:38.376Z","etag":null,"topics":["i18n","localization","nette","plurals"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/czukowski.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-07-04T13:45:49.000Z","updated_at":"2024-04-06T14:49:24.000Z","dependencies_parsed_at":"2022-08-21T09:20:45.991Z","dependency_job_id":null,"html_url":"https://github.com/czukowski/I18n_Plurals_Nette","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czukowski%2FI18n_Plurals_Nette","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czukowski%2FI18n_Plurals_Nette/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czukowski%2FI18n_Plurals_Nette/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czukowski%2FI18n_Plurals_Nette/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/czukowski","download_url":"https://codeload.github.com/czukowski/I18n_Plurals_Nette/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248637832,"owners_count":21137538,"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":["i18n","localization","nette","plurals"],"created_at":"2024-11-26T02:32:02.153Z","updated_at":"2025-04-12T21:51:05.744Z","avatar_url":"https://github.com/czukowski.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Introduction\n============\n\nThis package will help you to do grammatically accurate translations in your Nette application\n(framework version 2.2+ supported; still working great in 2.4, yay!).\n\nThe _suggested_ translation sources for Nette application are actually Neon files which may be\nlocated virtually anywhere in the application, for example, `app/i18n` folder, this will be used\nfor the examples. Note that you are not limited in where you store your translations: also pure\nPHP files, databases and even all of them together may be used!\n\nInstallation and setup\n----------------------\n\nAdd to your application using Composer:\n\n`composer require czukowski/i18n-nette`\n\nAdd I18n service to your configuration file:\n\n```yaml\nextensions:\n    i18n: I18n\\Nette\\NetteExtension\n\ni18n:\n    defaultLang: cs       # Default fallback language\n    directories:          # List of directories containing i18n files\n        - %appDir%/i18n\n    languages:            # List of languages available in the application,\n        - cs              # it is useful for choosing default language from\n        - en              # HTTP request headers\n```\n\nPlace your translations into the i18n directory, like this:\n\n * `en.neon` - English translations,\n * `cs.neon` - Czech translations,\n * `fr.neon` - General French translations,\n * `fr/be.neon` - Belgium French translations that are different from general French,\n * `fr/ch.neon` - Swiss French translations that are different from general French.\n\nIf you request the translation for 'fr-CH' locale, it'll look in the `fr/ch.neon` first, and failing\nthat in the general `fr.neon`. If the translation wasn't found even there, the untranslated input string\nis returned.\n\nThe translation data structure is very similar to what you're used to in Neon configuration (may be\neither flat or nested, your choice!):\n\n```yaml\nstring: řetězec\nsection:\n    string: 'řetězec v podsekci'\n```\n\nSome of the Nette controls are ready for translations, you just need to set the translator instance\nto them in your factories, for example (assuming `$this-\u003etranslator` is an instance of the translator\nservice provided by this package):\n\n```php\n// Set translator to control (Nette\\Forms\\Controls\\BaseControl):\n$control-\u003esetTranslator($this-\u003etranslator);\n// Set translator to form (Nette\\Forms\\Form):\n$form-\u003esetTranslator($this-\u003etranslator);\n// Set translator to template (Nette\\Templating\\Template or Nette\\Bridges\\ApplicationLatte\\Template):\n$template-\u003esetTranslator($this-\u003etranslator);\n```\n\nAfter setting the translator to the templates, you'll be able to use the translation macro:\n`{_'translate this'}`. We'll get into the details on its usage later on.\n\nConfiguration options\n---------------------\n\nConfiguration options available for this Nette extension:\n\n - `defaultLang` - Default application language, ie translate to this language if no target\n   language specified in translate function call (default value: 'en-us').\n - `directories` - Directories containing application translations. May be more than one if\n   application contains multiple modules, each adding to the translations list. Paths may contain\n   template keys from config parameters section, eg `%appDir%/i18n` (no default value).\n - `languages` - List of available languages. Useful when default language is set automatically\n    from HTTP Request headers (no default value).\n - `useNeonStyleParams` - If set to TRUE, wraps replacement parameter names into percent signs\n   (eg. `param` becomes `%param%`), so that translation keys can look similarly to the template\n   parameters in neon configuration file, while using bare parameter keys in the translate calls.\n   Example: `{_'I have %count% strings to translate', $count, ['count' =\u003e $count]}`. Default value\n   is FALSE, but if your application uses only neon files as translation sources, using this may\n   look nicer. Changing this parameter mid-way will require to review all translate calls across\n   your application, so choose wisely.\n - `setLangFromRequestHeaders` - If set to TRUE, will automatically set default language from HTTP\n   Request (the respective function can also be called manually). Only languages existing in the\n   `languages` list will be set, according to priorities in the headers.\n - `replaceLatteFactory` - If set to TRUE, replaces `latte.templateFactory` service with a new one,\n   that implements a callback on template create. This callback may be used to inject translator to\n   templates automatically (default value is FALSE, but if you use custom Latte Template Factory\n   replacement, you may set it to TRUE safely).\n - `latteFactoryClass` - This is a class name that will be used for the replacement template factory.\n   This setting allows to override it and use another class that implements the same functionality, if\n   needed (default value: `'I18n\\Nette\\TemplateFactory'`). This setting will have no effect, unless\n   the `replaceLatteFactory` parameter is set to TRUE.\n - `autoSetTranslatorToTemplates` - If set to TRUE, will inject translator to templates automatically,\n   using the replacement Latte Template Factory. If `replaceLatteFactory` is set to TRUE, this parameter\n   is also set to TRUE implicitly. The only valid use case to set this parameter is when another template\n   factory is already replaced by another class, and it is still desired to auto-set translator to\n   templates using `onCreateTemplate` callback.\n\nHow to make the translations work\n---------------------------------\n\nYou can find the information about the translation contexts, plural forms and even more in\n[the base package readme](https://github.com/czukowski/I18n_Plural#translation-contexts). *It is\nomitted here in order to avoid duplication*.\n\nTranslating in Nette templates\n------------------------------\n\nHere are some examples, those are pretty self-explanatory:\n\n\t// Basic translation.\n\t{_'Welcome!'}\n\t// Translation with context.\n\t{_'New customer has been saved.', $customer-\u003egender}\n\t// Translation with parameters and context skipped.\n\t{_'Hi, my name is :name', [':name' =\u003e $name]}\n\t// All arguments present, including target language.\n\t{_'You have :count messages', $count, [':count' =\u003e $count], 'cs'}\n\nNote: if you use `useNeonStyleParams`, the translation could look like:\n\n\t{_'You have %count% messages', $count, ['count' =\u003e $count], 'cs'}\n\nAPI\n---\n\nThe base package API is covered in its own readme. \n\n### class I18n\\NetteTranslator\n\nYou are not required to use core object directly. This class is a Nette-compatible wrapper and it's the\nsuggested usage in Nette applications. See above for an example on how to setup a trasnlation service.\n\n#### public function __construct($default_lang = 'x', $use_neon_style_params = FALSE)\n\n  * @param  string   $default_lang\n  * @param  boolean  $use_neon_style_params\n\nTranslator constructor takes default language to use when none is specified explicitly. Initializes a Core\nobject instance internally.\n\n#### public function attach(I18n\\Reader\\ReaderInterface $reader)\n\n  * @param  I18n\\Reader\\ReaderInterface  $reader\n\nAttaches a Reader object to the Core object (see below). `I18n\\Nette\\NeonReader` is a suggested default\nreader for Nette application, although there's `I18n\\Nette\\NetteReader` that gets translations from raw PHP\nfiles placed similarly into the Nette application, and you may of course also implement your own readers to\nprovide translations from any source of your choice.\n\n#### public function getAvailableLanguages()\n\nReturns list of available languages.\n\n#### public function setAvailableLanguages($langs)\n\n  * @param   array  $langs\n  * @return  $this\n\nSets list of available languages.\n\n#### public function getDefaultLanguage()\n\nReturns default language, used when no target language passed to translate function call.\n\n#### public function setDefaultLanguage($lang)\n\n  * @param   string  $lang\n  * @return  $this\n\nSets default language.\n\n#### public function setLanguageFromHeaders(Nette\\Http\\IRequest $httpRequest)\n\n  * @param   Nette\\Http\\IRequest  $httpRequest\n  * @return  $this\n\nSets default language from HTTP Request headers, if at least one of accepted language is\ncontained in the available languages list. The one with the highest priority is chosen.\n\n#### public function setTranslator($object)\n\n  * @param  object  $object\n\nSets Nette translator (`$this` object instance) to the compatible objects:\n\n - `'Nette\\Bridges\\ApplicationLatte\\Template'`\n - `'Nette\\Forms\\Controls\\BaseControl'`\n - `'Nette\\Forms\\Form'`\n - `'Nette\\Templating\\Template'`\n\n#### public function translate($string, $count, $parameters, $lang)\n\n  * @param   string  $string      String to translate\n  * @param   mixed   $count       String form or numeric count (optional)\n  * @param   array   $parameters  Param values to replace (optional)\n  * @param   string  $lang        Target language (optional)\n  * @return  string\n\nThe `$parameters` values (array) may be passed as 2nd argument, in that case `$count` is considered `NULL`\nand `$lang` is the 3rd argument.\n\n#### public function getService()\n\n  * @return  I18n\\Core\n\nReturns the internal Core object reference if needed for some reason.\n\nLicense\n-------\n\nThe distribution is permitted under the MIT License. See LICENSE.md for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fczukowski%2Fi18n_plurals_nette","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fczukowski%2Fi18n_plurals_nette","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fczukowski%2Fi18n_plurals_nette/lists"}