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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 19 additions & 0 deletions
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

Lines changed: 7 additions & 4 deletions
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

Lines changed: 11 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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
/**

0 commit comments

Comments
 (0)