|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Example module. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace ExampleNamespace; |
| 10 | + |
| 11 | +use Fisharebest\Localization\Translation; |
| 12 | +use Fisharebest\Webtrees\I18N; |
| 13 | +use Fisharebest\Webtrees\Module\AbstractModule; |
| 14 | +use Fisharebest\Webtrees\Module\ModuleCustomInterface; |
| 15 | +use Fisharebest\Webtrees\Module\ModuleCustomTrait; |
| 16 | + |
| 17 | +/** |
| 18 | + * Class ExampleModule |
| 19 | + * |
| 20 | + * This example shows how to create a custom module. |
| 21 | + * All the functions are optional. Just implement the ones you need. |
| 22 | + * |
| 23 | + * Modules *must* implement ModuleCustomInterface. They *may* also implement other interfaces. |
| 24 | + */ |
| 25 | +class ExampleModule extends AbstractModule implements ModuleCustomInterface |
| 26 | +{ |
| 27 | + // For every module interface that is implemented, the corresponding trait *should* also use be used. |
| 28 | + use ModuleCustomTrait; |
| 29 | + |
| 30 | + /** |
| 31 | + * The constructor is called on all modules, even ones that are disabled. |
| 32 | + * Note that you cannot rely on other modules (such as languages) here, as they may not yet exist. |
| 33 | + * |
| 34 | + */ |
| 35 | + public function __construct() |
| 36 | + { |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Bootstrap. This function is called on *enabled* modules. |
| 41 | + * It is a good place to register routes and views. |
| 42 | + * Note that it is only called on genealogy pages - not on admin pages. |
| 43 | + * |
| 44 | + * @return void |
| 45 | + */ |
| 46 | + public function boot(): void |
| 47 | + { |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * How should this module be identified in the control panel, etc.? |
| 52 | + * |
| 53 | + * @return string |
| 54 | + */ |
| 55 | + public function title(): string |
| 56 | + { |
| 57 | + return I18N::translate('Example module'); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * A sentence describing what this module does. |
| 62 | + * |
| 63 | + * @return string |
| 64 | + */ |
| 65 | + public function description(): string |
| 66 | + { |
| 67 | + return I18N::translate('This module does not do anything'); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * The person or organisation who created this module. |
| 72 | + * |
| 73 | + * @return string |
| 74 | + */ |
| 75 | + public function customModuleAuthorName(): string |
| 76 | + { |
| 77 | + return 'Greg Roach'; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * The version of this module. |
| 82 | + * |
| 83 | + * @return string |
| 84 | + */ |
| 85 | + public function customModuleVersion(): string |
| 86 | + { |
| 87 | + return '1.0.0'; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * A URL that will provide the latest version of this module. |
| 92 | + * |
| 93 | + * @return string |
| 94 | + */ |
| 95 | + public function customModuleLatestVersionUrl(): string |
| 96 | + { |
| 97 | + return 'https://github.com/webtrees/example-module/raw/main/latest-version.txt'; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Where to get support for this module. Perhaps a github repository? |
| 102 | + * |
| 103 | + * @return string |
| 104 | + */ |
| 105 | + public function customModuleSupportUrl(): string |
| 106 | + { |
| 107 | + return 'https://github.com/webtrees/example-module'; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Additional/updated translations. |
| 112 | + * |
| 113 | + * @param string $language |
| 114 | + * |
| 115 | + * @return array<string> |
| 116 | + */ |
| 117 | + public function customTranslations(string $language): array |
| 118 | + { |
| 119 | + switch ($language) { |
| 120 | + case 'en-AU': |
| 121 | + case 'en-GB': |
| 122 | + case 'en-US': |
| 123 | + // Note the special characters used in plural and context-sensitive translations. |
| 124 | + return [ |
| 125 | + 'Individual' => 'Fish', |
| 126 | + 'Individuals' => 'Fishes', |
| 127 | + '%s individual' . I18N::PLURAL . '%s individuals' => '%s fish' . I18N::PLURAL . '%s fishes', |
| 128 | + 'Unknown given name' . I18N::CONTEXT . '…' => '?fish?', |
| 129 | + 'Unknown surname' . I18N::CONTEXT . '…' => '?FISH?', |
| 130 | + ]; |
| 131 | + |
| 132 | + case 'fr': |
| 133 | + case 'fr-CA': |
| 134 | + return [ |
| 135 | + // These are new translations: |
| 136 | + 'Example module' => 'Exemple module', |
| 137 | + 'This module does not do anything' => 'Ce module ne fait rien', |
| 138 | + // These are updates to existing translations: |
| 139 | + 'Individual' => 'Poisson', |
| 140 | + 'Individuals' => 'Poissons', |
| 141 | + '%s individual' . I18N::PLURAL . '%s individuals' => '%s poisson' . I18N::PLURAL . '%s poissons', |
| 142 | + 'Unknown given name' . I18N::CONTEXT . '…' => '?poission?', |
| 143 | + 'Unknown surname' . I18N::CONTEXT . '…' => '?POISSON?', |
| 144 | + ]; |
| 145 | + |
| 146 | + case 'some-other-language': |
| 147 | + // Arrays are preferred, and are faster. |
| 148 | + // If your module uses .MO files, then you can convert them to arrays like this. |
| 149 | + return (new Translation('path/to/file.mo'))->asArray(); |
| 150 | + |
| 151 | + default: |
| 152 | + return []; |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments