Showing posts with label Smarty. Show all posts
Showing posts with label Smarty. Show all posts

Saturday, June 20, 2015

[PrestaShop][Language] เจาะลึกกับ Translations ในส่วนของ Module และ ระบบ

[PrestaShop][Language] เจาะลึกกับ Translations ในส่วนของ Module และ ระบบ (ภาคจบ)
          ใน Folder Translations และ Lang นั้น มีโครงสร้างที่แตกต่างกัน วันนี้จะขออธิบายโครงสร้างทั้งหมด เพื่อให้ทุกท่านได้ลองใช้งานกันได้ถูกต้องและเป็นอัตโนมัติ หากใช้งานคู่กับ Prestashop Automatic Translations with Google

โครงสร้าง translations จากระบบ ตัวอย่างเช่น \translations\en\admin.php
global $_LANGADM; 
$_LANGADM 
= array(); 

$_LANGADM
['AdminAddressesd3b206d196cd6be3a2764c1fb90b200f'] = 'Delete selected'; 
$_LANGADM
['AdminAddressese25f0ecd41211b01c83e5fec41df4fe7'] = 'Delete selected items?'; 
$_LANGADM
['AdminAddressesb718adec73e04ce3ec720dd11a06a308'] = 'ID'; 

หากเป็นตัวแปรของ admin หรือทำหน้าที่แสดงข้อความจากส่วน Backend นั้นจะใช้ ตัวแปรชื่อว่า $_LANGADM โดยมีส่วนประกอบดังนี้
$_LANGADM['AdminAddressesd3b206d196cd6be3a2764c1fb90b200f'] = 'Delete selected'; 

$_LANGADM คือ ชื่อตัวแปร
AdminAddresses คือ ชื่อโมดูลที่แสดงผลในแต่ละส่วน
d3b206d196cd6be3a2764c1fb90b200f คือ  MD5 ของข้อความ ‘Delete selected’
'Delete selected' คือ ข้อความที่ต้องการแสดงผล
ทั้งนี้ยังมีอีกหลาย ๆ ตัวแปรสำหรับส่วนของ admin ไม่ว่าจะเป็น $_ERRORS, $_FIELDS, $_LANGPDF ยกเว้นแต่ $_TABS จะระบุ Key ที่ไม่ต้องการ Encrypt ด้วย MD5

โครงสร้าง translations จากโมดูล ตัวอย่างเช่น fbconnect_psb\translations
global $_MODULE; 
$_MODULE 
= array(); 
$_MODULE
['<{fbconnect_psb}prestashop>registration_fb_6335a00a08fde0fbb8f6d6630cdadd92'] = 'ข้อมูลส่วนบุคคล'; 
$_MODULE
['<{fbconnect_psb}prestashop>registration_fb_b78a3223503896721cca1303f776159b'] = 'คำนำหน้า'; 

จะสังเกตุได้ว่า โครงสร้าง Translations ของ โมดูลนั้น จะมีตัวแปรที่แตกต่างออกไป ซึ่งระบบใน Prestashop เลือกใช้เป็นตัวแปรชื่อ $_MODULE โดยมีรายละเอียดดังนี้
$_MODULE['<{fbconnect_psb}prestashop>registration_fb_6335a00a08fde0fbb8f6d6630cdadd92'] = 'ข้อมูลส่วนบุคคล'; 
$_MODULE คือ ตัวแปรสำหรับเก็บภาษา
<{fbconnect_psb}prestashop>registration_fb_ คือ Key ของการแสดงผลของภาษาในแต่ละส่วน โดยจะมีส่วนประกอบย่อยดังนี้
·       {fbconnect_psb} ชื่อโมดูล
·       <prestashop> คือ Tag
·       registration_fb คือ Action หรือ template File(*.tpl) ของแต่ละหน้าจอของโมดูลนั้น ๆ
เมื่อ 3 ส่วนนี้ประกอบเข้าด้วยกัน จะรองรับ Module ที่มี Template หลาย ๆ หน้าจอได้ ด้วย translation ไฟล์เดียว
** ข้อสำคัญ ถึงแม้ว่าจะมีคำ ๆ เดียวกัน แต่ต่างกันระหว่าง Template จำเป็นจะต้องเขียน Key ที่แตกต่างกันด้วย
*** สำหรับ ไฟล์ Template (*.tpl) จะเห็นวิธีการเขียนการแสดงผลของข้อความเป็นดังนี้
{l s='Your personal information' mod='fbconnect_psb'}
หมายความว่า l(anguage) ที่มีข้อความ s(tring) ชื่อ Your personal information ในส่วนของ mod(ule) ชื่อว่า fbconnect_psb ซึ่งจะสัมพันธ์กับ en.php หรือ th.php ในคำอธิบายด้านบน

6335a00a08fde0fbb8f6d6630cdadd92 คือ MD5 ของข้อความ ข้อมูลส่วนบุคคล
'ข้อมูลส่วนบุคคล'คือ ข้อความที่ต้องการแสดงผล

โครงสร้าง lang จาก theme ตัวอย่างเช่น \themes\ap_office\lang ซึ่งเป็นตัวอย่างสุดท้าย
global $_LANG; 
$_LANG 
= array(); 

$_LANG
['404_d0fbda9855d118740f1105334305c126'] = 'Page not found'; 
$_LANG
['404_44b1d05764b093cb00c25b8228cb87d3'] = 'This page is not available'; 
$_LANG
['addresses_d95cf4ab2cbf1dfb63f066b50558b07d'] = 'My account'; 
$_LANG
['addresses_e45be0a0d4a0b62b15694c1a631e6e62'] = 'My addresses'; 

สำหรับ language ที่ได้เขียนผ่าน theme นั้น จะมีลักษณะ ตัวแปรใหม่ ซึ่งเราจะเห็นว่าใช้ตัวแปรชื่อ $_LANG โดยที่ตัวแปรนี้จะควบคุมการแสดงผลทั้งหมดของ THEME โดยมีรายละเอียดดังนี้
$_LANG['addresses_e45be0a0d4a0b62b15694c1a631e6e62'] = 'My addresses'; 
$_LANG คือตัวแปร
addresses_ คือ Action หรือ Template File(*.tpl) ในการกำหนดความสัมพันธ์ของการแสดงผลในหน้าจอ เช่นตัวอย่างนี้จะสัมพันธ์กับ addesses.tpl ซึ่งภายในไฟล์จะเขียนคำสั่งในการแสดงผลในตัวอย่างนี้
{l s='My addresses'} จะเห็นได้ว่าเป็น Language ย่อส่วนโดยไม่ต้องระบุ mod(ule) ต่อท้าย เพราะใช้ไฟล์ language โดยรวมของ theme ไปแล้ว

e45be0a0d4a0b62b15694c1a631e6e62 คือ MD5 ของ My addresses
'My addresses' คือ ค่าที่ต้องการแสดงข้อความผ่านหน้าจอต่าง ๆ


          สรุปโดยรวม จะเห็นได้ว่า การออกแบบ Prestashop นั้นได้ถูกออกแบบมาเพื่อรองรับ Multilingual หรือการทำเว็บไซต์หลายภาษามาเป็นอย่างดี อย่างไรก็ตามจะมี Language อื่น ๆ ที่ไม่ได้กล่าวถึง แต่ก็มีส่วนสำคัญคือ Language ใน Database แต่ที่ไม่กล่าวถึงเนื่องจาก ทุกท่านสามารถบันทึกผ่านหน้าจอของระบบได้เลย โดยที่ระบบจะมีให้เลือกบันทึกข้อความผ่าน language แต่ละส่วนด้วยเช่นกัน เพื่อให้ เนื้อหาที่แสดงในหน้าจอนั้นมีความครบถ้วนในทุก ๆ ภาษาที่ผู้เขียนต้องการจะสื่อ


หวังเป็นอย่างยิ่งว่า บทความนี้จะทำให้ทุกท่านเข้าใจใน กลไก การทำงานหลายภาษาของ Prastashop ได้เป็นอย่างดี หากมีข้อสงสัย ส่งข้อความสอบถามได้ครับ ยินดีต้อนรับทุกท่าน

Saturday, June 06, 2015

[PrestaShop][EN] Describing more detail in structure of PrestaShop for people who want to write PrestaShop in Multilanguage Part 2


          The latest blog, you have known about structure in PrestaShop for using Multilanguage or dynamic language after that I will explain more information in this structure that how the structure is.  For understanding before you customize the PrestaShop by yourself

admin.php File from folder translations
$_LANGADM['AdminCartRulesd3b206d196cd6be3a2764c1fb90b200f'] = 'Delete selected';

Explaining the structure
$_LANGADM is a variable called Array
'AdminCartRulesd3b206d196cd6be3a2764c1fb90b200f' is the key of the Array it is a part of module and md5 string
d3b206d196cd6be3a2764c1fb90b200f is MD5 of string such as Delete selected = d3b206d196cd6be3a2764c1fb90b200f

‘Delete selected’; is value of the word or statement from Array
For example;
If you change the word from ‘Delete selected’ to ‘selected to delete’, the word on the screen display will be changed. Absolutely, if you want to translate other language you can do this.

For example: changing the language
$_LANGADM['AdminCartRulesd3b206d196cd6be3a2764c1fb90b200f'] = ' Sil seçili '; // turkish

P.S. It is more important, when you change value of key of the Array, you must not change md5 key that the key is ‘Delete selected’ encrypted.
On next paragraph, I will explain more detail for key md5 (read more ***)

a template file from modules, in this case, I choose blockuserinfo module. when you open folder blockuserinfo on ‘\modules\blockuserinfo’ you will look .tpl file at that folder, surely, filename .tpl is a PHP Smarty template that is on views folder or outside that folder.

For example: PHP Smarty Template
<p id="header_user_info">
                        {l s='Welcome' mod='blockuserinfo'}
                        {if $logged}
                                     <a href="{$link->getPageLink('my-account', true)|escape:'html'}" title="{l s='View my customer account' mod='blockuserinfo'}" class="account" rel="nofollow"><span>{$cookie->customer_firstname} {$cookie->customer_lastname}</span></a>
                                     <a href="{$link->getPageLink('index', true, NULL, "mylogout")|escape:'html'}" title="{l s='Log me out' mod='blockuserinfo'}" class="logout" rel="nofollow">{l s='Sign out' mod='blockuserinfo'}</a>
                        {else}
                                     <a href="{$link->getPageLink('my-account', true)|escape:'html'}" title="{l s='Log in to your customer account' mod='blockuserinfo'}" class="login" rel="nofollow">{l s='Sign in' mod='blockuserinfo'}</a>
                        {/if}
            </p>
 

In the structure .tpl, the file that contains tag for display Multilanguage on screen as follows;
{l s='Welcome' mod='blockuserinfo'}
{l s='View my customer account' mod='blockuserinfo'}
{l s='Log me out' mod='blockuserinfo'}
{l s='Log in to your customer account' mod='blockuserinfo'}
{l s='Sign in' mod='blockuserinfo'}


Describing:
{} is a tag for display from PHP smarty
l is a language
s is a string or sentence
mod is a module name

Normally, writing tag in smarty template I will always write in an English for standardization, so it’s meaning I have never written en.php file. Even though, some module write tag smarty not standardization I need to write en.php file too.
Be careful, writing “ l s=’String key’ from native language you must encrypt md5 from that language.


Next session, I will explain a relation of translations and module with .tpl and .php

Tags:
Admin, Introduction, Language, Module, Multilanguage, PHP, PrestaShop, Smarty, Translation, 

Writer:

WindowsDNA

[PrestaShop] การอธิบายเพิ่มเติม ในเนื้อหาภายในโครงสร้างว่ามีลักษณะอย่างไร สำหรับการเขียนเว็บไซต์หลายภาษา ด้วย PrestaShop ตอนที่ 2

[PrestaShop] หลังจากที่ทราบโครงสร้างขอ PrestaShop สำหรับการทำเว็บไซต์หลายภาษาแล้ว ทั้งนี้จะอธิบายเพิ่มเติม ในเนื้อหาภายในโครงสร้างว่ามีลักษณะอย่างไร เพื่อความเข้าใจก่อนการปรับแต่งให้เหมาะสมกับเว็บไซต์ของตัวเอง

ไฟล์ admin.php จาก Folder Translations มีโครงสร้างดังนี้
$_LANGADM['AdminCartRulesd3b206d196cd6be3a2764c1fb90b200f'] = 'Delete selected';

แบ่งโครงสร้าง
$_LANGADM เป็น ตัวแปร เรียกว่า Array
'AdminCartRulesd3b206d196cd6be3a2764c1fb90b200fคือ Key ของ Array นั้น ๆ โดยประกอบไปด้วย โมดูล + MD5
d3b206d196cd6be3a2764c1fb90b200คือ MD5 ของข้อความของ Delete selected = d3b206d196cd6be3a2764c1fb90b200f

'Delete selected'; คือ ค่าที่บรรจุลงใน Array ข้างต้น เช่นถ้าเปลี่ยนเป็น ‘selected to delete’; ข้อความที่แสดงผ่านหน้าจอจะเปลี่ยนไป และหากอยากเปลี่ยนเป็นภาษาไทยเลยยังได้ เช่น
$_LANGADM['AdminCartRulesd3b206d196cd6be3a2764c1fb90b200f'] = 'ลบรายการที่เลือก';
*** ที่สำคัญ md5 ที่ได้จะต้องไม่เปลี่ยนแปลง ซึ่งจะยังคงเป็น md5 ของ Delete selected นั่นเอง
ต่อไปจะขยายความอีกครั้งว่าทำไมไม่เปลี่ยนแปลงในส่วน Key (เพิ่มเติม ***)


ไฟล์ template จาก modules โดยเลือก \modules\blockuserinfo เป็นตัวอย่าง จะเห็นไฟล์ .tpl ซึ่งเป็น PHP Smarty Template อาจจะอยู่ภายใต้ Folder views หรืออยู่ข้างนอกก็ได้
ตัวอย่าง PHP Smarty Template
<p id="header_user_info">
                        {l s='Welcome' mod='blockuserinfo'}
                        {if $logged}
                                     <a href="{$link->getPageLink('my-account', true)|escape:'html'}" title="{l s='View my customer account' mod='blockuserinfo'}" class="account" rel="nofollow"><span>{$cookie->customer_firstname} {$cookie->customer_lastname}</span></a>
                                     <a href="{$link->getPageLink('index', true, NULL, "mylogout")|escape:'html'}" title="{l s='Log me out' mod='blockuserinfo'}" class="logout" rel="nofollow">{l s='Sign out' mod='blockuserinfo'}</a>
                        {else}
                                     <a href="{$link->getPageLink('my-account', true)|escape:'html'}" title="{l s='Log in to your customer account' mod='blockuserinfo'}" class="login" rel="nofollow">{l s='Sign in' mod='blockuserinfo'}</a>
                        {/if}
            </p>
 

ในโครงสร้าง *.tpl นั้นจะบรรจุ Tag สำหรับ การแสดงผลในรูปแบบหลายภาษาด้วยตัวอย่างดังนี้
{l s='Welcome' mod='blockuserinfo'}
{l s='View my customer account' mod='blockuserinfo'}
{l s='Log me out' mod='blockuserinfo'}
{l s='Log in to your customer account' mod='blockuserinfo'}
{l s='Sign in' mod='blockuserinfo'}

คำอธิบาย
{} คือ โครงสร้างการแสดงผล Smarty
คือ การแสดงผลภาษา
คือ ข้อความที่ต้องการแสดงผล
mod คือ Module สำหรับการแสดงผลภาษานั้น ๆ

โดยปกติการเขียน Tag ในส่วน Template ของ Smarty นั้นเรามักจะเขียนเป็นภาษาอังกฤษเป็นมาตรฐาน ดังนั้นจะหมายความว่า เราไม่จำเป็นต้องเขียนไฟล์ภาษา en.php เลย หากแต่ว่า มีบางโมดูลไม่ได้เขียนเป็นภาษาอังกฤษก็จำเป็นจะต้องเขียนโครงสร้างไฟล์ภาษา en.php ด้วยเช่นเดียวกัน (***ข้อควรระวังการเขียน l s=’String Key’ ด้วยภาษาอะไร ก็ต้องจัดทำ MD5 ด้วยภาษานั้น ๆ)

ครั้งต่อไปจะพูดถึง Translations สำหรับ Modules โดยดูความสัมพันธ์ของ .tpl และ .php


Tags:

Multilanguage,Translation,PrestaShop,Module,Admin,Smarty,PHP

Writer:
WindowsDNA