Skip to content

Commit

Permalink
Add support for changing the document language (#1108)
Browse files Browse the repository at this point in the history
  • Loading branch information
troosan committed Sep 26, 2017
1 parent 743dbff commit 0beeb27
Show file tree
Hide file tree
Showing 38 changed files with 625 additions and 65 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ This is the last version to support PHP 5.3
- Introduced the `\PhpOffice\PhpWord\SimpleType\NumberFormat` simple type. - @troosan
- Support for ContextualSpacing - @postHawk #1088
- Possiblity to hide spelling and/or grammatical errors - @troosan #542
- Possiblity to set default document language as well as changing the language for each text element - @troosan #1108
- Support for Comments - @troosan #1067
- Add support for changing the document language - @troosan #1108

### Fixed
- Loosen dependency to Zend
Expand Down
19 changes: 19 additions & 0 deletions docs/general.rst
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,25 @@ The default symbol to represent a decimal figure is the ``.`` in english. In fre
$phpWord->getSettings()->setDecimalSymbol(',');
Document Language
~~~~~~~~~~~~~~~~~
The default language of the document can be change with the following.

.. code-block:: php
$phpWord->getSettings()->setThemeFontLang(new Language(Language::FR_BE));
``Languge`` has 3 parameters, one for Latin languages, one for East Asian languages and one for Complex (Bi-Directional) languages.
A couple of language codes are provided in the ``PhpOffice\PhpWord\ComplexType\Language`` class but any valid code/ID can be used.

In case you are generating an RTF document the Language need to be set differently.

.. code-block:: php
$lang = new Language();
$lang->setLangId(Language::EN_GB_ID);
$phpWord->getSettings()->setThemeFontLang($lang);
Document information
--------------------

Expand Down
11 changes: 7 additions & 4 deletions docs/styles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ Available Font style options:
- ``subScript``. Subscript, *true* or *false*.
- ``superScript``. Superscript, *true* or *false*.
- ``underline``. Underline, *dash*, *dotted*, etc.
- ``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
See ``\PhpOffice\PhpWord\Style\Language`` class for some language codes.

.. _paragraph-style:

Expand All @@ -64,7 +66,7 @@ Paragraph
Available Paragraph style options:

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

- ``alignment``. Supports all alignment modes since 1st Edition of ECMA-376 standard up till ISO/IEC 29500:2012.
See ``\PhpOffice\PhpWord\SimpleType\JcTable`` and ``\PhpOffice\PhpWord\SimpleType\Jc`` classes for the details.
See ``\PhpOffice\PhpWord\SimpleType\JcTable`` and ``\PhpOffice\PhpWord\SimpleType\Jc`` classes for the details.
- ``bgColor``. Background color, e.g. '9966CC'.
- ``border(Top|Right|Bottom|Left)Color``. Border color, e.g. '9966CC'.
- ``border(Top|Right|Bottom|Left)Size``. Border size in twips.
Expand All @@ -106,7 +108,8 @@ Available Cell style options:
- ``border(Top|Right|Bottom|Left)Color``. Border color, e.g. '9966CC'.
- ``border(Top|Right|Bottom|Left)Size``. Border size in twips.
- ``gridSpan``. Number of columns spanned.
- ``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``
- ``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``
- ``valign``. Vertical alignment, *top*, *center*, *both*, *bottom*.
- ``vMerge``. *restart* or *continue*.
- ``width``. Cell width in twips.
Expand All @@ -133,7 +136,7 @@ Numbering level
Available NumberingLevel style options:

- ``alignment``. Supports all alignment modes since 1st Edition of ECMA-376 standard up till ISO/IEC 29500:2012.
See ``\PhpOffice\PhpWord\SimpleType\Jc`` class for the details.
See ``\PhpOffice\PhpWord\SimpleType\Jc`` class for the details.
- ``font``. Font name.
- ``format``. Numbering format bullet\|decimal\|upperRoman\|lowerRoman\|upperLetter\|lowerLetter.
- ``hanging``. See paragraph style.
Expand Down
11 changes: 11 additions & 0 deletions samples/Sample_01_SimpleText.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
<?php
use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWord\Style\Font;

include_once 'Sample_Header.php';

// New Word Document
echo date('H:i:s') , ' Create new PhpWord object' , EOL;

$languageEnGb = new \PhpOffice\PhpWord\Style\Language(\PhpOffice\PhpWord\Style\Language::EN_GB);

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->getSettings()->setThemeFontLang($languageEnGb);

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

// $pStyle = new Font();
// $pStyle->setLang()
$section->addText('Ce texte-ci est en français.', array('lang' => \PhpOffice\PhpWord\Style\Language::FR_BE));

// Two text break
$section->addTextBreak(2);

Expand Down
2 changes: 1 addition & 1 deletion samples/Sample_10_EastAsianFontStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
$section = $phpWord->addSection();
$header = array('size' => 16, 'bold' => true);
//1.Use EastAisa FontStyle
$section->addText('中文楷体样式测试', array('name' => '楷体', 'size' => 16, 'color' => '1B2232'));
$section->addText('中文楷体样式测试', array('name' => '楷体', 'size' => 16, 'color' => '1B2232', 'lang' => array('latin' => 'en-US', 'eastAsia' => 'zh-CN')));

// Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers);
Expand Down
4 changes: 2 additions & 2 deletions src/PhpWord/ComplexType/TrackChangesView.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ public function hasFormatting()
/**
* Set Display Formatting Revisions
*
* @param boolean $insDel
* @param boolean|null $formatting
* Set to true to show formatting revisions
*/
public function setFormatting($formatting)
public function setFormatting($formatting = null)
{
$this->formatting = $formatting === null ? true : $formatting;
}
Expand Down
2 changes: 1 addition & 1 deletion src/PhpWord/Element/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public function setElementIndex($value)
/**
* Get element unique ID
*
* @return string
* @return integer
*/
public function getElementId()
{
Expand Down
2 changes: 0 additions & 2 deletions src/PhpWord/Element/Bookmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ class Bookmark extends AbstractElement
*/
public function __construct($name)
{

$this->name = CommonText::toUTF8($name);
return $this;
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/PhpWord/Element/CheckBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class CheckBox extends Text
* @param string $text
* @param mixed $fontStyle
* @param mixed $paragraphStyle
* @return self
*/
public function __construct($name = null, $text = null, $fontStyle = null, $paragraphStyle = null)
{
Expand Down
1 change: 0 additions & 1 deletion src/PhpWord/Element/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public function __construct($author, $date, $initials)
{
parent::__construct($author, $date);
$this->initials = $initials;
return $this;
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/PhpWord/Element/FormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class FormField extends Text
/**
* Form field name
*
* @var string
* @var string|bool|int
*/
private $name;

Expand Down Expand Up @@ -70,7 +70,6 @@ class FormField extends Text
* @param string $type
* @param mixed $fontStyle
* @param mixed $paragraphStyle
* @return self
*/
public function __construct($type, $fontStyle = null, $paragraphStyle = null)
{
Expand Down
1 change: 0 additions & 1 deletion src/PhpWord/Element/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public function __construct($source, $text = null, $fontStyle = null, $paragraph
$this->fontStyle = $this->setNewStyle(new Font('text'), $fontStyle);
$this->paragraphStyle = $this->setNewStyle(new Paragraph(), $paragraphStyle);
$this->internal = $internal;
return $this;
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/PhpWord/Element/PreserveText.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class PreserveText extends AbstractElement
* @param string $text
* @param mixed $fontStyle
* @param mixed $paragraphStyle
* @return self
*/
public function __construct($text = null, $fontStyle = null, $paragraphStyle = null)
{
Expand Down
1 change: 0 additions & 1 deletion src/PhpWord/Element/SDT.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class SDT extends Text
* @param string $type
* @param mixed $fontStyle
* @param mixed $paragraphStyle
* @return self
*/
public function __construct($type, $fontStyle = null, $paragraphStyle = null)
{
Expand Down
4 changes: 2 additions & 2 deletions src/PhpWord/Element/TOC.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class TOC extends AbstractElement
/**
* Font style
*
* @var \PhpOffice\PhpWord\Style\Font|array|string
* @var \PhpOffice\PhpWord\Style\Font|string
*/
private $fontStyle;

Expand Down Expand Up @@ -121,7 +121,7 @@ public function getStyleTOC()
/**
* Get Font Style
*
* @return \PhpOffice\PhpWord\Style\Font
* @return \PhpOffice\PhpWord\Style\Font|string
*/
public function getStyleFont()
{
Expand Down
2 changes: 1 addition & 1 deletion src/PhpWord/Element/TrackChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class TrackChange extends AbstractContainer
* Create a new TrackChange Element
*
* @param string $author
* @param DateTime $date
* @param \DateTime $date
*/
public function __construct($author, \DateTime $date)
{
Expand Down
28 changes: 28 additions & 0 deletions src/PhpWord/Metadata/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use PhpOffice\PhpWord\ComplexType\ProofState;
use PhpOffice\PhpWord\SimpleType\Zoom;
use PhpOffice\PhpWord\ComplexType\TrackChangesView;
use PhpOffice\PhpWord\Style\Language;

/**
* Setting class
Expand Down Expand Up @@ -100,6 +101,13 @@ class Settings
*/
private $evenAndOddHeaders = false;

/**
* Theme Font Languages
*
* @var Language
*/
private $themeFontLang;

/**
* Radix Point for Field Code Evaluation
*
Expand Down Expand Up @@ -291,6 +299,26 @@ public function setZoom($zoom)
}
}

/**
* Returns the Language
*
* @return Language
*/
public function getThemeFontLang()
{
return $this->themeFontLang;
}

/**
* sets the Language for this document
*
* @param Language $themeFontLang
*/
public function setThemeFontLang($themeFontLang)
{
$this->themeFontLang = $themeFontLang;
}

/**
* Returns the Radix Point for Field Code Evaluation
*
Expand Down
29 changes: 17 additions & 12 deletions src/PhpWord/Reader/Word2007/AbstractPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,18 +314,20 @@ protected function readParagraphStyle(XMLReader $xmlReader, \DOMElement $domNode

$styleNode = $xmlReader->getElement('w:pPr', $domNode);
$styleDefs = array(
'styleName' => array(self::READ_VALUE, 'w:pStyle'),
'alignment' => array(self::READ_VALUE, 'w:jc'),
'basedOn' => array(self::READ_VALUE, 'w:basedOn'),
'next' => array(self::READ_VALUE, 'w:next'),
'indent' => array(self::READ_VALUE, 'w:ind', 'w:left'),
'hanging' => array(self::READ_VALUE, 'w:ind', 'w:hanging'),
'spaceAfter' => array(self::READ_VALUE, 'w:spacing', 'w:after'),
'spaceBefore' => array(self::READ_VALUE, 'w:spacing', 'w:before'),
'widowControl' => array(self::READ_FALSE, 'w:widowControl'),
'keepNext' => array(self::READ_TRUE, 'w:keepNext'),
'keepLines' => array(self::READ_TRUE, 'w:keepLines'),
'pageBreakBefore' => array(self::READ_TRUE, 'w:pageBreakBefore'),
'styleName' => array(self::READ_VALUE, 'w:pStyle'),
'alignment' => array(self::READ_VALUE, 'w:jc'),
'basedOn' => array(self::READ_VALUE, 'w:basedOn'),
'next' => array(self::READ_VALUE, 'w:next'),
'indent' => array(self::READ_VALUE, 'w:ind', 'w:left'),
'hanging' => array(self::READ_VALUE, 'w:ind', 'w:hanging'),
'spaceAfter' => array(self::READ_VALUE, 'w:spacing', 'w:after'),
'spaceBefore' => array(self::READ_VALUE, 'w:spacing', 'w:before'),
'widowControl' => array(self::READ_FALSE, 'w:widowControl'),
'keepNext' => array(self::READ_TRUE, 'w:keepNext'),
'keepLines' => array(self::READ_TRUE, 'w:keepLines'),
'pageBreakBefore' => array(self::READ_TRUE, 'w:pageBreakBefore'),
'contextualSpacing' => array(self::READ_TRUE, 'w:contextualSpacing'),
'bidi' => array(self::READ_TRUE, 'w:bidi'),
);

return $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);
Expand Down Expand Up @@ -369,6 +371,9 @@ protected function readFontStyle(XMLReader $xmlReader, \DOMElement $domNode)
'subScript' => array(self::READ_EQUAL, 'w:vertAlign', 'w:val', 'subscript'),
'fgColor' => array(self::READ_VALUE, 'w:highlight'),
'rtl' => array(self::READ_TRUE, 'w:rtl'),
'font-latin' => array(self::READ_VALUE, 'w:font', 'w:val'),
'font-eastAsia' => array(self::READ_VALUE, 'w:font', 'w:eastAsia'),
'font-bidi' => array(self::READ_VALUE, 'w:font', 'w:bidi'),
);

return $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);
Expand Down
Loading

0 comments on commit 0beeb27

Please sign in to comment.