Skip to content

Commit 0beeb27

Browse files
authored
Add support for changing the document language (#1108)
1 parent 743dbff commit 0beeb27

38 files changed

+625
-65
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ This is the last version to support PHP 5.3
1414
- Introduced the `\PhpOffice\PhpWord\SimpleType\NumberFormat` simple type. - @troosan
1515
- Support for ContextualSpacing - @postHawk #1088
1616
- Possiblity to hide spelling and/or grammatical errors - @troosan #542
17+
- Possiblity to set default document language as well as changing the language for each text element - @troosan #1108
1718
- Support for Comments - @troosan #1067
18-
- Add support for changing the document language - @troosan #1108
1919

2020
### Fixed
2121
- Loosen dependency to Zend

docs/general.rst

+19
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,25 @@ The default symbol to represent a decimal figure is the ``.`` in english. In fre
201201
202202
$phpWord->getSettings()->setDecimalSymbol(',');
203203
204+
Document Language
205+
~~~~~~~~~~~~~~~~~
206+
The default language of the document can be change with the following.
207+
208+
.. code-block:: php
209+
210+
$phpWord->getSettings()->setThemeFontLang(new Language(Language::FR_BE));
211+
212+
``Languge`` has 3 parameters, one for Latin languages, one for East Asian languages and one for Complex (Bi-Directional) languages.
213+
A couple of language codes are provided in the ``PhpOffice\PhpWord\ComplexType\Language`` class but any valid code/ID can be used.
214+
215+
In case you are generating an RTF document the Language need to be set differently.
216+
217+
.. code-block:: php
218+
219+
$lang = new Language();
220+
$lang->setLangId(Language::EN_GB_ID);
221+
$phpWord->getSettings()->setThemeFontLang($lang);
222+
204223
Document information
205224
--------------------
206225

docs/styles.rst

+7-4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ Available Font style options:
5555
- ``subScript``. Subscript, *true* or *false*.
5656
- ``superScript``. Superscript, *true* or *false*.
5757
- ``underline``. Underline, *dash*, *dotted*, etc.
58+
- ``lang``. Language, either a language code like *en-US*, *fr-BE*, etc. or an object (or as an array) if you need to set eastAsian or bidirectional languages
59+
See ``\PhpOffice\PhpWord\Style\Language`` class for some language codes.
5860

5961
.. _paragraph-style:
6062

@@ -64,7 +66,7 @@ Paragraph
6466
Available Paragraph style options:
6567

6668
- ``alignment``. Supports all alignment modes since 1st Edition of ECMA-376 standard up till ISO/IEC 29500:2012.
67-
See ``\PhpOffice\PhpWord\SimpleType\Jc`` class for the details.
69+
See ``\PhpOffice\PhpWord\SimpleType\Jc`` class for the details.
6870
- ``basedOn``. Parent style.
6971
- ``hanging``. Hanging by how much.
7072
- ``indent``. Indent by how much.
@@ -87,7 +89,7 @@ Table
8789
Available Table style options:
8890

8991
- ``alignment``. Supports all alignment modes since 1st Edition of ECMA-376 standard up till ISO/IEC 29500:2012.
90-
See ``\PhpOffice\PhpWord\SimpleType\JcTable`` and ``\PhpOffice\PhpWord\SimpleType\Jc`` classes for the details.
92+
See ``\PhpOffice\PhpWord\SimpleType\JcTable`` and ``\PhpOffice\PhpWord\SimpleType\Jc`` classes for the details.
9193
- ``bgColor``. Background color, e.g. '9966CC'.
9294
- ``border(Top|Right|Bottom|Left)Color``. Border color, e.g. '9966CC'.
9395
- ``border(Top|Right|Bottom|Left)Size``. Border size in twips.
@@ -106,7 +108,8 @@ Available Cell style options:
106108
- ``border(Top|Right|Bottom|Left)Color``. Border color, e.g. '9966CC'.
107109
- ``border(Top|Right|Bottom|Left)Size``. Border size in twips.
108110
- ``gridSpan``. Number of columns spanned.
109-
- ``textDirection(btLr|tbRl)``. Direction of text. You can use constants ``\PhpOffice\PhpWord\Style\Cell::TEXT_DIR_BTLR`` and ``\PhpOffice\PhpWord\Style\Cell::TEXT_DIR_TBRL``
111+
- ``textDirection(btLr|tbRl)``. Direction of text.
112+
You can use constants ``\PhpOffice\PhpWord\Style\Cell::TEXT_DIR_BTLR`` and ``\PhpOffice\PhpWord\Style\Cell::TEXT_DIR_TBRL``
110113
- ``valign``. Vertical alignment, *top*, *center*, *both*, *bottom*.
111114
- ``vMerge``. *restart* or *continue*.
112115
- ``width``. Cell width in twips.
@@ -133,7 +136,7 @@ Numbering level
133136
Available NumberingLevel style options:
134137

135138
- ``alignment``. Supports all alignment modes since 1st Edition of ECMA-376 standard up till ISO/IEC 29500:2012.
136-
See ``\PhpOffice\PhpWord\SimpleType\Jc`` class for the details.
139+
See ``\PhpOffice\PhpWord\SimpleType\Jc`` class for the details.
137140
- ``font``. Font name.
138141
- ``format``. Numbering format bullet\|decimal\|upperRoman\|lowerRoman\|upperLetter\|lowerLetter.
139142
- ``hanging``. See paragraph style.

samples/Sample_01_SimpleText.php

+11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
<?php
2+
use PhpOffice\PhpWord\Style\Paragraph;
3+
use PhpOffice\PhpWord\Style\Font;
4+
25
include_once 'Sample_Header.php';
36

47
// New Word Document
58
echo date('H:i:s') , ' Create new PhpWord object' , EOL;
9+
10+
$languageEnGb = new \PhpOffice\PhpWord\Style\Language(\PhpOffice\PhpWord\Style\Language::EN_GB);
11+
612
$phpWord = new \PhpOffice\PhpWord\PhpWord();
13+
$phpWord->getSettings()->setThemeFontLang($languageEnGb);
714

815
$fontStyleName = 'rStyle';
916
$phpWord->addFontStyle($fontStyleName, array('bold' => true, 'italic' => true, 'size' => 16, 'allCaps' => true, 'doubleStrikethrough' => true));
@@ -20,6 +27,10 @@
2027
$section->addTitle('Welcome to PhpWord', 1);
2128
$section->addText('Hello World!');
2229

30+
// $pStyle = new Font();
31+
// $pStyle->setLang()
32+
$section->addText('Ce texte-ci est en français.', array('lang' => \PhpOffice\PhpWord\Style\Language::FR_BE));
33+
2334
// Two text break
2435
$section->addTextBreak(2);
2536

samples/Sample_10_EastAsianFontStyle.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
$section = $phpWord->addSection();
88
$header = array('size' => 16, 'bold' => true);
99
//1.Use EastAisa FontStyle
10-
$section->addText('中文楷体样式测试', array('name' => '楷体', 'size' => 16, 'color' => '1B2232'));
10+
$section->addText('中文楷体样式测试', array('name' => '楷体', 'size' => 16, 'color' => '1B2232', 'lang' => array('latin' => 'en-US', 'eastAsia' => 'zh-CN')));
1111

1212
// Save file
1313
echo write($phpWord, basename(__FILE__, '.php'), $writers);

src/PhpWord/ComplexType/TrackChangesView.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ public function hasFormatting()
135135
/**
136136
* Set Display Formatting Revisions
137137
*
138-
* @param boolean $insDel
138+
* @param boolean|null $formatting
139139
* Set to true to show formatting revisions
140140
*/
141-
public function setFormatting($formatting)
141+
public function setFormatting($formatting = null)
142142
{
143143
$this->formatting = $formatting === null ? true : $formatting;
144144
}

src/PhpWord/Element/AbstractElement.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public function setElementIndex($value)
231231
/**
232232
* Get element unique ID
233233
*
234-
* @return string
234+
* @return integer
235235
*/
236236
public function getElementId()
237237
{

src/PhpWord/Element/Bookmark.php

-2
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ class Bookmark extends AbstractElement
4545
*/
4646
public function __construct($name)
4747
{
48-
4948
$this->name = CommonText::toUTF8($name);
50-
return $this;
5149
}
5250

5351
/**

src/PhpWord/Element/CheckBox.php

-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ class CheckBox extends Text
4040
* @param string $text
4141
* @param mixed $fontStyle
4242
* @param mixed $paragraphStyle
43-
* @return self
4443
*/
4544
public function __construct($name = null, $text = null, $fontStyle = null, $paragraphStyle = null)
4645
{

src/PhpWord/Element/Comment.php

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ public function __construct($author, $date, $initials)
6161
{
6262
parent::__construct($author, $date);
6363
$this->initials = $initials;
64-
return $this;
6564
}
6665

6766
/**

src/PhpWord/Element/FormField.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class FormField extends Text
3535
/**
3636
* Form field name
3737
*
38-
* @var string
38+
* @var string|bool|int
3939
*/
4040
private $name;
4141

@@ -70,7 +70,6 @@ class FormField extends Text
7070
* @param string $type
7171
* @param mixed $fontStyle
7272
* @param mixed $paragraphStyle
73-
* @return self
7473
*/
7574
public function __construct($type, $fontStyle = null, $paragraphStyle = null)
7675
{

src/PhpWord/Element/Link.php

-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ public function __construct($source, $text = null, $fontStyle = null, $paragraph
8383
$this->fontStyle = $this->setNewStyle(new Font('text'), $fontStyle);
8484
$this->paragraphStyle = $this->setNewStyle(new Paragraph(), $paragraphStyle);
8585
$this->internal = $internal;
86-
return $this;
8786
}
8887

8988
/**

src/PhpWord/Element/PreserveText.php

-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ class PreserveText extends AbstractElement
5454
* @param string $text
5555
* @param mixed $fontStyle
5656
* @param mixed $paragraphStyle
57-
* @return self
5857
*/
5958
public function __construct($text = null, $fontStyle = null, $paragraphStyle = null)
6059
{

src/PhpWord/Element/SDT.php

-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ class SDT extends Text
5151
* @param string $type
5252
* @param mixed $fontStyle
5353
* @param mixed $paragraphStyle
54-
* @return self
5554
*/
5655
public function __construct($type, $fontStyle = null, $paragraphStyle = null)
5756
{

src/PhpWord/Element/TOC.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class TOC extends AbstractElement
3636
/**
3737
* Font style
3838
*
39-
* @var \PhpOffice\PhpWord\Style\Font|array|string
39+
* @var \PhpOffice\PhpWord\Style\Font|string
4040
*/
4141
private $fontStyle;
4242

@@ -121,7 +121,7 @@ public function getStyleTOC()
121121
/**
122122
* Get Font Style
123123
*
124-
* @return \PhpOffice\PhpWord\Style\Font
124+
* @return \PhpOffice\PhpWord\Style\Font|string
125125
*/
126126
public function getStyleFont()
127127
{

src/PhpWord/Element/TrackChange.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class TrackChange extends AbstractContainer
4545
* Create a new TrackChange Element
4646
*
4747
* @param string $author
48-
* @param DateTime $date
48+
* @param \DateTime $date
4949
*/
5050
public function __construct($author, \DateTime $date)
5151
{

src/PhpWord/Metadata/Settings.php

+28
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use PhpOffice\PhpWord\ComplexType\ProofState;
2020
use PhpOffice\PhpWord\SimpleType\Zoom;
2121
use PhpOffice\PhpWord\ComplexType\TrackChangesView;
22+
use PhpOffice\PhpWord\Style\Language;
2223

2324
/**
2425
* Setting class
@@ -100,6 +101,13 @@ class Settings
100101
*/
101102
private $evenAndOddHeaders = false;
102103

104+
/**
105+
* Theme Font Languages
106+
*
107+
* @var Language
108+
*/
109+
private $themeFontLang;
110+
103111
/**
104112
* Radix Point for Field Code Evaluation
105113
*
@@ -291,6 +299,26 @@ public function setZoom($zoom)
291299
}
292300
}
293301

302+
/**
303+
* Returns the Language
304+
*
305+
* @return Language
306+
*/
307+
public function getThemeFontLang()
308+
{
309+
return $this->themeFontLang;
310+
}
311+
312+
/**
313+
* sets the Language for this document
314+
*
315+
* @param Language $themeFontLang
316+
*/
317+
public function setThemeFontLang($themeFontLang)
318+
{
319+
$this->themeFontLang = $themeFontLang;
320+
}
321+
294322
/**
295323
* Returns the Radix Point for Field Code Evaluation
296324
*

src/PhpWord/Reader/Word2007/AbstractPart.php

+17-12
Original file line numberDiff line numberDiff line change
@@ -314,18 +314,20 @@ protected function readParagraphStyle(XMLReader $xmlReader, \DOMElement $domNode
314314

315315
$styleNode = $xmlReader->getElement('w:pPr', $domNode);
316316
$styleDefs = array(
317-
'styleName' => array(self::READ_VALUE, 'w:pStyle'),
318-
'alignment' => array(self::READ_VALUE, 'w:jc'),
319-
'basedOn' => array(self::READ_VALUE, 'w:basedOn'),
320-
'next' => array(self::READ_VALUE, 'w:next'),
321-
'indent' => array(self::READ_VALUE, 'w:ind', 'w:left'),
322-
'hanging' => array(self::READ_VALUE, 'w:ind', 'w:hanging'),
323-
'spaceAfter' => array(self::READ_VALUE, 'w:spacing', 'w:after'),
324-
'spaceBefore' => array(self::READ_VALUE, 'w:spacing', 'w:before'),
325-
'widowControl' => array(self::READ_FALSE, 'w:widowControl'),
326-
'keepNext' => array(self::READ_TRUE, 'w:keepNext'),
327-
'keepLines' => array(self::READ_TRUE, 'w:keepLines'),
328-
'pageBreakBefore' => array(self::READ_TRUE, 'w:pageBreakBefore'),
317+
'styleName' => array(self::READ_VALUE, 'w:pStyle'),
318+
'alignment' => array(self::READ_VALUE, 'w:jc'),
319+
'basedOn' => array(self::READ_VALUE, 'w:basedOn'),
320+
'next' => array(self::READ_VALUE, 'w:next'),
321+
'indent' => array(self::READ_VALUE, 'w:ind', 'w:left'),
322+
'hanging' => array(self::READ_VALUE, 'w:ind', 'w:hanging'),
323+
'spaceAfter' => array(self::READ_VALUE, 'w:spacing', 'w:after'),
324+
'spaceBefore' => array(self::READ_VALUE, 'w:spacing', 'w:before'),
325+
'widowControl' => array(self::READ_FALSE, 'w:widowControl'),
326+
'keepNext' => array(self::READ_TRUE, 'w:keepNext'),
327+
'keepLines' => array(self::READ_TRUE, 'w:keepLines'),
328+
'pageBreakBefore' => array(self::READ_TRUE, 'w:pageBreakBefore'),
329+
'contextualSpacing' => array(self::READ_TRUE, 'w:contextualSpacing'),
330+
'bidi' => array(self::READ_TRUE, 'w:bidi'),
329331
);
330332

331333
return $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);
@@ -369,6 +371,9 @@ protected function readFontStyle(XMLReader $xmlReader, \DOMElement $domNode)
369371
'subScript' => array(self::READ_EQUAL, 'w:vertAlign', 'w:val', 'subscript'),
370372
'fgColor' => array(self::READ_VALUE, 'w:highlight'),
371373
'rtl' => array(self::READ_TRUE, 'w:rtl'),
374+
'font-latin' => array(self::READ_VALUE, 'w:font', 'w:val'),
375+
'font-eastAsia' => array(self::READ_VALUE, 'w:font', 'w:eastAsia'),
376+
'font-bidi' => array(self::READ_VALUE, 'w:font', 'w:bidi'),
372377
);
373378

374379
return $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);

0 commit comments

Comments
 (0)