Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
GettextDocumentationAid.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\TranslatorInterface\Aid;
5
7use GettextFFS;
9use MediaWiki\MediaWikiServices;
10
19 public function getData(): array {
20 // We need to get the primary group to get the correct file
21 // So $group can be different from $this->group
22 $group = $this->handle->getGroup();
23 if ( !$group instanceof FileBasedMessageGroup ) {
24 throw new TranslationHelperException( 'Not a FileBasedMessageGroup group' );
25 }
26
27 $ffs = $group->getFFS();
28 if ( !$ffs instanceof GettextFFS ) {
29 throw new TranslationHelperException( 'Group is not using GettextFFS' );
30 }
31
32 $cache = $group->getMessageGroupCache( $group->getSourceLanguage() );
33 if ( !$cache->exists() ) {
34 throw new TranslationHelperException( 'Definitions are not cached' );
35 }
36
37 $extra = $cache->getExtra();
38 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
39 $messageKey = $contLang->lcfirst( $this->handle->getKey() );
40 $messageKey = str_replace( ' ', '_', $messageKey );
41
42 $help = $extra['TEMPLATE'][$messageKey]['comments'] ?? null;
43 if ( !$help ) {
44 throw new TranslationHelperException( "No comments found for key '$messageKey'" );
45 }
46
47 $conf = $group->getConfiguration();
48 if ( isset( $conf['BASIC']['codeBrowser'] ) ) {
49 $pattern = $conf['BASIC']['codeBrowser'];
50 $pattern = str_replace( '%FILE%', '\1', $pattern );
51 $pattern = str_replace( '%LINE%', '\2', $pattern );
52 $pattern = "[$pattern \\1:\\2]";
53 } else {
54 $pattern = "\\1:\\2";
55 }
56
57 $out = '';
58 foreach ( $help as $type => $lines ) {
59 if ( $type === ':' ) {
60 $files = '';
61 foreach ( $lines as $line ) {
62 $files .= ' ' . preg_replace( '/([^ :]+):(\d+)/', $pattern, $line );
63 }
64 $out .= "<nowiki>#:</nowiki> $files<br />";
65 } else {
66 foreach ( $lines as $line ) {
67 $out .= "<nowiki>#$type</nowiki> $line<br />";
68 }
69 }
70 }
71
72 $html = $this->context->getOutput()->parseAsContent( $out );
73
74 return [
75 'language' => $contLang->getCode(),
76 // @todo Provide raw data when possible
77 // 'value' => $help,
78 'html' => $html,
79 ];
80 }
81}
This class implements default behavior for file based message groups.
New-style FFS class that implements support for gettext file format.
Translation helpers can throw this exception when they cannot do anything useful with the current mes...