SlideShare uma empresa Scribd logo
1 de 9
Baixar para ler offline
Internationalization (i18n)
of Web Page
William.L
wiliwe@gmail.com
2014-04-28
Index
Localization(l10n) & Internationalization(i18n)................................................................................................ 3
Butterfat Internationalization (b28n) Project.................................................................................................... 4
Usage of b28n JavaScript Library....................................................................................................................... 5
Testing b28n........................................................................................................................................................... 7
Localization(l10n) & Internationalization(i18n)
L10N is the act of translation of messages from one language to another to meet cultural or other requirements
of a target market or a locale.
I18N is the act of making the code be able to use l10n easily for users that vary in culture, region, or language.
Butterfat Internationalization (b28n) Project
A project created by Butterfat to ease i18n work on web page. Internationalization is implemented in a
JavaScript library(b28n.js) that reads XML files to do the translations.
Project Site (Unavailable Now)
http://www.butterfat.net/wiki/Projects/b28n
b28n JavaScript file download site:
https://mmbase.org/CMSContainer/tags/v1_2_1/CMSContainer/cmsc/utilities/src/webapp/editors/js/Butterfat/b28n.js
Usage of b28n JavaScript Library
1. For each language, create a file named <domain>_<lang>.xml, where lang is one of the two letter
abbreviations found on http://www.loc.gov/standards/iso639-2/langcodes.html (for instance,
messages_en.xml is a good start).
The script will first look for a cookie with the name "language", and if it is not set then it uses the browser
langague (using JavaScript Navigator object’s attributes navigator.language or
navigator.browserLanguage).
Here, creates two language table XML files, main_en.xml for English and main_tw.xml for Regular Chinese.
Its contents are as below:
Note:
PO means “Portable Object”. See http://en.wikipedia.org/wiki/Gettext for more details.
2. In the web page wants to do i18n, include b28n JavaScript library “b28n.js” using HTML <script> tag.
Ex: describe the path to “b28n.js” file.
<script type="text/javascript" src="/lang/b28n.js"></script>
3. In <header> block, put following code in <script> block
Butterlate.setTextDomain("main","lang");
, “main” is the name of <domain> to of “<domain>_<lang>.xml” file and “lang” is the location of
“<domain>_<lang>.xml” file.
3. Write a JavaScript function(name “init” for example) to be invoked when HTML body is loaded.
<body onload=” init()”>
In this function, set value of “innerHTML” HTML DOM property of elements that need to do translation as
below. The string between “_(“ and “)” symbols is the value of “msgid” defined in language file,
“<domain>_<lang>.xml”.
<po>
<message msgid="translate apply" msgstr="Switch Lang" />
<message msgid="translate chip" msgstr="Chip" />
<message msgid="translate computer" msgstr="Computer" />
<message msgid="translate program" msgstr="Program" />
</po>
<po>
<message msgid="translate apply" msgstr="切換語言" />
<message msgid="translate chip" msgstr="晶片" />
<message msgid="translate computer" msgstr="計算機" />
<message msgid="translate program" msgstr="程式" />
</po>
var e = document.getElementById("chip");
e.innerHTML = _("translate chip");
e = document.getElementById("computer");
e.innerHTML = _("translate computer");
e = document.getElementById("program");
e.innerHTML = _("translate program");
4. Design a mechanism to trigger the act setting “language” into cookie and reload current web page.
Here use button for example.
Note:
In original b28n.js, getLang() function returns current language code retrieving from web browser’s attribute
and it would return a code that let b28n.js fail to find correct language table XML file(<domain>_<lang>.xml)
when it is FIRST time to load this web page, so I modified code that it will return “en” code when there has no
“language” key in cookie.
P.S: Do not eat too much Butter or you will get to be Fat !
this.getLang = function() {
var one, two, end;
if((one=document.cookie.indexOf("language"))==-1)
return "en"; /*((navigator.language) ? navigator.language : navigator.browserLanguage).substring(0,2);
2014-04-27, William.L changed to use "en" as default language code. */
end = (document.cookie.indexOf(';',one)!=-1) ? document.cookie.indexOf(';',one) : document.cookie.length;
return unescape(document.cookie.substring(one+9,end));
};
<script>
function setLanguage()
{
var langValue = Current-Radio-Button-Value;
document.cookie = "language="+langValue+"; path=/"; /* store new chosen language type into cookie */
window.parent.location.reload(); /* Reload this web page */
}
</script>
…..
<form name="Lang">
…
<input type=”button” onclick="javascript:setLanguage()">
…
</form>
Testing b28n
O.S: MS-Windows XP/7
Web Server: Apache Win32
1. Down Apache for MS-Windows from following site, install and invoke it.
http://apache.stu.edu.tw//httpd/binaries/win32/
If Apache run normally, it could see Apache icon in Windows system tray.
In this test, Apache is installed under “E” drive, and its path will be
“E:Apache Software FoundationApache2.2”
2. Download sample code from GitHub
https://github.com/wiliwe/web-i18n-butterfat.git
Using Git tool to fork this repo:
git clone https://github.com/wiliwe/web-i18n-butterfat.git
3. Uncompress sample code to Apache Document Root.
You could see “DocumentRoot” path in Apache configuration file, httpd.conf locateing under “conf” folder
of your Apache installation path .
After uncompressing sample to Apache Document Root folder, it will be as below.
P.S :
Overwrite original index.html file built by Apache server.
4. Now, it could try to connect to Apache and see test result.
Open a web browser(here uses Google Chrome web browser for example) and enter URL http://localhost:80
to connect.
“80” in URL is the Apache web server listening port number and it could be found in Apache configuration
file, httpd.conf, search “Listen” variable to see the actual port number.
In testing web page (index.html), there has two radio buttons for choosing language.
You can invoke Developer Tool of web browser and see Cookie’s value as below figure.
Internationalization(i18n) of Web Page

Mais conteúdo relacionado

Destaque

Remote monitoring system
Remote monitoring systemRemote monitoring system
Remote monitoring system
Pk Doctors
 
Video Streaming
Video StreamingVideo Streaming
Video Streaming
Videoguy
 

Destaque (20)

Streaming Media Server Setup Manual
Streaming Media Server Setup ManualStreaming Media Server Setup Manual
Streaming Media Server Setup Manual
 
Cygwin Install How-To (Chinese)
Cygwin Install How-To (Chinese)Cygwin Install How-To (Chinese)
Cygwin Install How-To (Chinese)
 
Re-identification of Anomized CDR datasets using Social networlk Data
Re-identification of Anomized CDR datasets using Social networlk DataRe-identification of Anomized CDR datasets using Social networlk Data
Re-identification of Anomized CDR datasets using Social networlk Data
 
Timing over packet demarcation
Timing over packet demarcationTiming over packet demarcation
Timing over packet demarcation
 
Nosql redis-mongo
Nosql redis-mongoNosql redis-mongo
Nosql redis-mongo
 
2011_12_4K research in PSNC
2011_12_4K research in PSNC2011_12_4K research in PSNC
2011_12_4K research in PSNC
 
RTSP Streaming Server - Demo Streaming RTSP Protocol Over IPv6 Network
RTSP Streaming Server - Demo Streaming RTSP Protocol Over IPv6 NetworkRTSP Streaming Server - Demo Streaming RTSP Protocol Over IPv6 Network
RTSP Streaming Server - Demo Streaming RTSP Protocol Over IPv6 Network
 
Remote monitoring system
Remote monitoring systemRemote monitoring system
Remote monitoring system
 
RTSP Protocol Analysis Of A Streaming Thorugh IPv6 Network
RTSP Protocol Analysis Of A Streaming Thorugh IPv6 NetworkRTSP Protocol Analysis Of A Streaming Thorugh IPv6 Network
RTSP Protocol Analysis Of A Streaming Thorugh IPv6 Network
 
Hello world在那邊?背景說明
Hello world在那邊?背景說明Hello world在那邊?背景說明
Hello world在那邊?背景說明
 
FFMPEG TOOLS
FFMPEG TOOLSFFMPEG TOOLS
FFMPEG TOOLS
 
Realtime video streaming the open source way
Realtime video streaming the open source wayRealtime video streaming the open source way
Realtime video streaming the open source way
 
Asterisk (IP-PBX) CDR Log Rotation
Asterisk (IP-PBX) CDR Log RotationAsterisk (IP-PBX) CDR Log Rotation
Asterisk (IP-PBX) CDR Log Rotation
 
GNU AS簡介
GNU AS簡介GNU AS簡介
GNU AS簡介
 
from Source to Binary: How GNU Toolchain Works
from Source to Binary: How GNU Toolchain Worksfrom Source to Binary: How GNU Toolchain Works
from Source to Binary: How GNU Toolchain Works
 
UPnP 1.0 簡介
UPnP 1.0 簡介UPnP 1.0 簡介
UPnP 1.0 簡介
 
Trace 程式碼之皮
Trace 程式碼之皮Trace 程式碼之皮
Trace 程式碼之皮
 
Video Streaming
Video StreamingVideo Streaming
Video Streaming
 
GNU ld的linker script簡介
GNU ld的linker script簡介GNU ld的linker script簡介
GNU ld的linker script簡介
 
Remote Management of Embedded Android Devices
Remote Management of Embedded Android DevicesRemote Management of Embedded Android Devices
Remote Management of Embedded Android Devices
 

Semelhante a Internationalization(i18n) of Web Page

I18n
I18nI18n
I18n
soon
 
Client side scripting using Javascript
Client side scripting using JavascriptClient side scripting using Javascript
Client side scripting using Javascript
Bansari Shah
 

Semelhante a Internationalization(i18n) of Web Page (20)

Java script
Java scriptJava script
Java script
 
Java scipt
Java sciptJava scipt
Java scipt
 
Learn to Internationalize your Applications - Sun Tech Days 2009
Learn to Internationalize your Applications - Sun Tech Days 2009Learn to Internationalize your Applications - Sun Tech Days 2009
Learn to Internationalize your Applications - Sun Tech Days 2009
 
Learn to Internationalize your Applications - Sun Tech Days 2009
Learn to Internationalize your Applications - Sun Tech Days 2009Learn to Internationalize your Applications - Sun Tech Days 2009
Learn to Internationalize your Applications - Sun Tech Days 2009
 
Learn to Internationalize your Applications - Sun Tech Days 2009
Learn to Internationalize your Applications - Sun Tech Days 2009Learn to Internationalize your Applications - Sun Tech Days 2009
Learn to Internationalize your Applications - Sun Tech Days 2009
 
Learn to Internationalize your Application
Learn to Internationalize your ApplicationLearn to Internationalize your Application
Learn to Internationalize your Application
 
I18n
I18nI18n
I18n
 
Dev fest 2020 taiwan how to debug microservices on kubernetes as a pros (ht...
Dev fest 2020 taiwan   how to debug microservices on kubernetes as a pros (ht...Dev fest 2020 taiwan   how to debug microservices on kubernetes as a pros (ht...
Dev fest 2020 taiwan how to debug microservices on kubernetes as a pros (ht...
 
Client side scripting using Javascript
Client side scripting using JavascriptClient side scripting using Javascript
Client side scripting using Javascript
 
Modern javascript localization with c-3po and the good old gettext
Modern javascript localization with c-3po and the good old gettextModern javascript localization with c-3po and the good old gettext
Modern javascript localization with c-3po and the good old gettext
 
20150812 4시간만에 따라해보는 windows 10 앱 개발
20150812  4시간만에 따라해보는 windows 10 앱 개발20150812  4시간만에 따라해보는 windows 10 앱 개발
20150812 4시간만에 따라해보는 windows 10 앱 개발
 
Test2
Test2Test2
Test2
 
Test2
Test2Test2
Test2
 
Intro to JavaScript
Intro to JavaScriptIntro to JavaScript
Intro to JavaScript
 
IE 8 et les standards du Web - Chris Wilson - Paris Web 2008
IE 8 et les standards du Web - Chris Wilson - Paris Web 2008IE 8 et les standards du Web - Chris Wilson - Paris Web 2008
IE 8 et les standards du Web - Chris Wilson - Paris Web 2008
 
Fullstack workshop
Fullstack workshopFullstack workshop
Fullstack workshop
 
Automatisation in development and testing - within budget
Automatisation in development and testing - within budgetAutomatisation in development and testing - within budget
Automatisation in development and testing - within budget
 
Ibm
IbmIbm
Ibm
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)
 
eXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction TrainingeXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction Training
 

Mais de William Lee

Study of Chromium OS
Study of Chromium OSStudy of Chromium OS
Study of Chromium OS
William Lee
 
GNOME GeoClue - The Geolocation Service in Gnome
GNOME GeoClue - The Geolocation Service in GnomeGNOME GeoClue - The Geolocation Service in Gnome
GNOME GeoClue - The Geolocation Service in Gnome
William Lee
 

Mais de William Lee (20)

Usage Note of Apache Thrift for C++ Java PHP Languages
Usage Note of Apache Thrift for C++ Java PHP LanguagesUsage Note of Apache Thrift for C++ Java PHP Languages
Usage Note of Apache Thrift for C++ Java PHP Languages
 
Usage Note of Qt ODBC Database Access on Linux
Usage Note of Qt ODBC Database Access on LinuxUsage Note of Qt ODBC Database Access on Linux
Usage Note of Qt ODBC Database Access on Linux
 
Usage Note of SWIG for PHP
Usage Note of SWIG for PHPUsage Note of SWIG for PHP
Usage Note of SWIG for PHP
 
Upgrade GCC & Install Qt 5.4 on CentOS 6.5
Upgrade GCC & Install Qt 5.4 on CentOS 6.5 Upgrade GCC & Install Qt 5.4 on CentOS 6.5
Upgrade GCC & Install Qt 5.4 on CentOS 6.5
 
Usage Notes of The Bro 2.2 / 2.3
Usage Notes of The Bro 2.2 / 2.3Usage Notes of The Bro 2.2 / 2.3
Usage Notes of The Bro 2.2 / 2.3
 
Viewing Android Source Files in Eclipse (Chinese)
Viewing Android Source Files in Eclipse  (Chinese)Viewing Android Source Files in Eclipse  (Chinese)
Viewing Android Source Files in Eclipse (Chinese)
 
Usage Note of Microsoft Dependency Walker
Usage Note of Microsoft Dependency WalkerUsage Note of Microsoft Dependency Walker
Usage Note of Microsoft Dependency Walker
 
Usage Note of PlayCap
Usage Note of PlayCapUsage Note of PlayCap
Usage Note of PlayCap
 
Qt4 App - Sliding Window
Qt4 App - Sliding WindowQt4 App - Sliding Window
Qt4 App - Sliding Window
 
GTK+ 2.0 App - Desktop App Chooser
GTK+ 2.0 App - Desktop App ChooserGTK+ 2.0 App - Desktop App Chooser
GTK+ 2.0 App - Desktop App Chooser
 
GTK+ 2.0 App - Icon Chooser
GTK+ 2.0 App - Icon ChooserGTK+ 2.0 App - Icon Chooser
GTK+ 2.0 App - Icon Chooser
 
Note of CGI and ASP
Note of CGI and ASPNote of CGI and ASP
Note of CGI and ASP
 
Moblin2 - Window Manager(Mutter) Plugin
Moblin2 - Window Manager(Mutter) PluginMoblin2 - Window Manager(Mutter) Plugin
Moblin2 - Window Manager(Mutter) Plugin
 
MGCP Overview
MGCP OverviewMGCP Overview
MGCP Overview
 
L.A.M.P Installation Note --- CentOS 6.5
L.A.M.P Installation Note --- CentOS 6.5L.A.M.P Installation Note --- CentOS 6.5
L.A.M.P Installation Note --- CentOS 6.5
 
C Program Runs on Wrong Target Platform(CPU Architecture)
C Program Runs on Wrong Target Platform(CPU Architecture)C Program Runs on Wrong Target Platform(CPU Architecture)
C Program Runs on Wrong Target Platform(CPU Architecture)
 
Notes for SQLite3 Usage
Notes for SQLite3 UsageNotes for SQLite3 Usage
Notes for SQLite3 Usage
 
Android Storage - StorageManager & OBB
Android Storage - StorageManager & OBBAndroid Storage - StorageManager & OBB
Android Storage - StorageManager & OBB
 
Study of Chromium OS
Study of Chromium OSStudy of Chromium OS
Study of Chromium OS
 
GNOME GeoClue - The Geolocation Service in Gnome
GNOME GeoClue - The Geolocation Service in GnomeGNOME GeoClue - The Geolocation Service in Gnome
GNOME GeoClue - The Geolocation Service in Gnome
 

Último

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 

Internationalization(i18n) of Web Page

  • 1. Internationalization (i18n) of Web Page William.L wiliwe@gmail.com 2014-04-28
  • 2. Index Localization(l10n) & Internationalization(i18n)................................................................................................ 3 Butterfat Internationalization (b28n) Project.................................................................................................... 4 Usage of b28n JavaScript Library....................................................................................................................... 5 Testing b28n........................................................................................................................................................... 7
  • 3. Localization(l10n) & Internationalization(i18n) L10N is the act of translation of messages from one language to another to meet cultural or other requirements of a target market or a locale. I18N is the act of making the code be able to use l10n easily for users that vary in culture, region, or language.
  • 4. Butterfat Internationalization (b28n) Project A project created by Butterfat to ease i18n work on web page. Internationalization is implemented in a JavaScript library(b28n.js) that reads XML files to do the translations. Project Site (Unavailable Now) http://www.butterfat.net/wiki/Projects/b28n b28n JavaScript file download site: https://mmbase.org/CMSContainer/tags/v1_2_1/CMSContainer/cmsc/utilities/src/webapp/editors/js/Butterfat/b28n.js
  • 5. Usage of b28n JavaScript Library 1. For each language, create a file named <domain>_<lang>.xml, where lang is one of the two letter abbreviations found on http://www.loc.gov/standards/iso639-2/langcodes.html (for instance, messages_en.xml is a good start). The script will first look for a cookie with the name "language", and if it is not set then it uses the browser langague (using JavaScript Navigator object’s attributes navigator.language or navigator.browserLanguage). Here, creates two language table XML files, main_en.xml for English and main_tw.xml for Regular Chinese. Its contents are as below: Note: PO means “Portable Object”. See http://en.wikipedia.org/wiki/Gettext for more details. 2. In the web page wants to do i18n, include b28n JavaScript library “b28n.js” using HTML <script> tag. Ex: describe the path to “b28n.js” file. <script type="text/javascript" src="/lang/b28n.js"></script> 3. In <header> block, put following code in <script> block Butterlate.setTextDomain("main","lang"); , “main” is the name of <domain> to of “<domain>_<lang>.xml” file and “lang” is the location of “<domain>_<lang>.xml” file. 3. Write a JavaScript function(name “init” for example) to be invoked when HTML body is loaded. <body onload=” init()”> In this function, set value of “innerHTML” HTML DOM property of elements that need to do translation as below. The string between “_(“ and “)” symbols is the value of “msgid” defined in language file, “<domain>_<lang>.xml”. <po> <message msgid="translate apply" msgstr="Switch Lang" /> <message msgid="translate chip" msgstr="Chip" /> <message msgid="translate computer" msgstr="Computer" /> <message msgid="translate program" msgstr="Program" /> </po> <po> <message msgid="translate apply" msgstr="切換語言" /> <message msgid="translate chip" msgstr="晶片" /> <message msgid="translate computer" msgstr="計算機" /> <message msgid="translate program" msgstr="程式" /> </po> var e = document.getElementById("chip"); e.innerHTML = _("translate chip"); e = document.getElementById("computer"); e.innerHTML = _("translate computer"); e = document.getElementById("program"); e.innerHTML = _("translate program");
  • 6. 4. Design a mechanism to trigger the act setting “language” into cookie and reload current web page. Here use button for example. Note: In original b28n.js, getLang() function returns current language code retrieving from web browser’s attribute and it would return a code that let b28n.js fail to find correct language table XML file(<domain>_<lang>.xml) when it is FIRST time to load this web page, so I modified code that it will return “en” code when there has no “language” key in cookie. P.S: Do not eat too much Butter or you will get to be Fat ! this.getLang = function() { var one, two, end; if((one=document.cookie.indexOf("language"))==-1) return "en"; /*((navigator.language) ? navigator.language : navigator.browserLanguage).substring(0,2); 2014-04-27, William.L changed to use "en" as default language code. */ end = (document.cookie.indexOf(';',one)!=-1) ? document.cookie.indexOf(';',one) : document.cookie.length; return unescape(document.cookie.substring(one+9,end)); }; <script> function setLanguage() { var langValue = Current-Radio-Button-Value; document.cookie = "language="+langValue+"; path=/"; /* store new chosen language type into cookie */ window.parent.location.reload(); /* Reload this web page */ } </script> ….. <form name="Lang"> … <input type=”button” onclick="javascript:setLanguage()"> … </form>
  • 7. Testing b28n O.S: MS-Windows XP/7 Web Server: Apache Win32 1. Down Apache for MS-Windows from following site, install and invoke it. http://apache.stu.edu.tw//httpd/binaries/win32/ If Apache run normally, it could see Apache icon in Windows system tray. In this test, Apache is installed under “E” drive, and its path will be “E:Apache Software FoundationApache2.2” 2. Download sample code from GitHub https://github.com/wiliwe/web-i18n-butterfat.git Using Git tool to fork this repo: git clone https://github.com/wiliwe/web-i18n-butterfat.git 3. Uncompress sample code to Apache Document Root. You could see “DocumentRoot” path in Apache configuration file, httpd.conf locateing under “conf” folder of your Apache installation path . After uncompressing sample to Apache Document Root folder, it will be as below.
  • 8. P.S : Overwrite original index.html file built by Apache server. 4. Now, it could try to connect to Apache and see test result. Open a web browser(here uses Google Chrome web browser for example) and enter URL http://localhost:80 to connect. “80” in URL is the Apache web server listening port number and it could be found in Apache configuration file, httpd.conf, search “Listen” variable to see the actual port number. In testing web page (index.html), there has two radio buttons for choosing language. You can invoke Developer Tool of web browser and see Cookie’s value as below figure.