Remove product literal strings in "pht()", part 5
[phabricator.git] / src / applications / files / document / PhabricatorTextDocumentEngine.php
blob2fce98baf1bf7a804c0e056de696e41807d10227
1 <?php
3 abstract class PhabricatorTextDocumentEngine
4 extends PhabricatorDocumentEngine {
6 private $encodingMessage = null;
8 protected function canRenderDocumentType(PhabricatorDocumentRef $ref) {
9 return $ref->isProbablyText();
12 public function canConfigureEncoding(PhabricatorDocumentRef $ref) {
13 return true;
16 protected function newTextDocumentContent(
17 PhabricatorDocumentRef $ref,
18 $content,
19 array $options = array()) {
21 PhutilTypeSpec::checkMap(
22 $options,
23 array(
24 'blame' => 'optional wild',
25 'coverage' => 'optional list<wild>',
26 ));
28 if (is_array($content)) {
29 $lines = $content;
30 } else {
31 $lines = phutil_split_lines($content);
34 $view = id(new PhabricatorSourceCodeView())
35 ->setHighlights($this->getHighlightedLines())
36 ->setLines($lines)
37 ->setSymbolMetadata($ref->getSymbolMetadata());
39 $blame = idx($options, 'blame');
40 if ($blame !== null) {
41 $view->setBlameMap($blame);
44 $coverage = idx($options, 'coverage');
45 if ($coverage !== null) {
46 $view->setCoverage($coverage);
49 $message = null;
50 if ($this->encodingMessage !== null) {
51 $message = $this->newMessage($this->encodingMessage);
54 $container = phutil_tag(
55 'div',
56 array(
57 'class' => 'document-engine-text',
59 array(
60 $message,
61 $view,
62 ));
64 return $container;
67 protected function loadTextData(PhabricatorDocumentRef $ref) {
68 $content = $ref->loadData();
70 $encoding = $this->getEncodingConfiguration();
71 if ($encoding !== null) {
72 if (function_exists('mb_convert_encoding')) {
73 $content = mb_convert_encoding($content, 'UTF-8', $encoding);
74 $this->encodingMessage = pht(
75 'This document was converted from %s to UTF8 for display.',
76 $encoding);
77 } else {
78 $this->encodingMessage = pht(
79 'Unable to perform text encoding conversion: mbstring extension '.
80 'is not available.');
82 } else {
83 if (!phutil_is_utf8($content)) {
84 if (function_exists('mb_detect_encoding')) {
85 $try_encodings = array(
86 'JIS' => pht('JIS'),
87 'EUC-JP' => pht('EUC-JP'),
88 'SJIS' => pht('Shift JIS'),
89 'ISO-8859-1' => pht('ISO-8859-1 (Latin 1)'),
92 $guess = mb_detect_encoding($content, array_keys($try_encodings));
93 if ($guess) {
94 $content = mb_convert_encoding($content, 'UTF-8', $guess);
95 $this->encodingMessage = pht(
96 'This document is not UTF8. It was detected as %s and '.
97 'converted to UTF8 for display.',
98 idx($try_encodings, $guess, $guess));
104 if (!phutil_is_utf8($content)) {
105 $content = phutil_utf8ize($content);
106 $this->encodingMessage = pht(
107 'This document is not UTF8 and its text encoding could not be '.
108 'detected automatically. Use "Change Text Encoding..." to choose '.
109 'an encoding.');
112 return $content;