Skip to main content
January 18, 2007
Answered

Problem with UTF-8 encoding

  • January 18, 2007
  • 23 replies
  • 3250 views
The problem is that although I have finally gotten the static text to display right the dynamitic which is queried from a mySQL database is not being displayed correctly,

I have set checked the database the Spanish, French, and other translations for the contents are there with the correct lettering. I have updated the mySQL drivers to 5.0 as recommended by Adobe, I have placed in the URL string of the JDBC the ?useUnicode=true&characterEncoding=UTF-8 as suggested by another forum. I have even checked all the pages properties to make sure that they are in UTF-8 encoding format, below is a sample of the code I am using what is wrong with the code, or what do I need to change to fix this problem. You may check the site at www.scoringag.com and try the languages translations to see further examples of the problem.

We are using MX7 MySQL4.1 Jconnect5.0

Sample code below:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>

<!---
**
* CF MX Admin "Application.cfm" file
* This file establishes the cfadmin application, as well as creates handles
* to the services using the factory via CFOBJECT.
*
* Copyright (c) 2001 Macromedia. All Rights Reserved.
* DO NOT REDISTRIBUTE THIS SOFTWARE IN ANY WAY WITHOUT THE EXPRESSED
* WRITTEN PERMISSION OF MACROMEDIA.
--->

<!--- Set multi-language utf-8 values here
---------------------------------------------------------------------->
<cfprocessingdirective pageencoding="utf-8">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<cfset URLenChar = "utf-8" >

<!--- Set encoding to utf-8. --->
<cfset setEncoding("URL", "utf-8")>
<cfset setEncoding("Form", "utf-8")>

<cfparam name="url.login" default="">

<!--- Set the output encoding to utf-8 --->
<cfcontent type="text/html; charset=utf-8">

</head>

<cfset SESSION.locale='es'>

<!--- <div id="home_contents"> --->
<style type="text/css">
<!--
.style2 {color: #ff0000}
-->
</style>

<div id="content">
<table align="center" width="100%">

<tr><center>

<div align="center" style="width:100%; font-size:13px; font-weight:500; color:#000000; "><br />
<a href=" http://www.cfsan.fda.gov/~dms/fsbtac23.html" target="_blank" class="style2" >*** Important Information (please read)! ***<br />
FDA Fact Sheet ScoringAg has the Solution! </a><br />
<a href="Public/docs/Acciones de la FDA en la nueva legislacion del Bioterrorismo.pdf" target="_blank" class="style2">Haga clic para aquí ver
los Hechos de los USA FDA - en Español</a> <br />
<br />
<cfscript>ssite.translate('#SESSION.Locale#', 1, 111);</cfscript></div><br />

This topic has been closed for replies.
Correct answer
And while you two are debating the issue I removed the Dateformat tag in the copyright clause at the bottom of the page, problem fixed. Don't ask why, I don't know but it works now, go figure, now I move to my next problem, real time video feed of a cow walking, don't ask I just do, just do :)

David Gamache

23 replies

January 18, 2007
Sabaidee: here is the the ssite.translate function, the Session.locale is used to lookup the locale and return the translation of the text for example locale is es will return spanish text for all d-text in the site. as for the <!--- Set encoding to utf-8. --->
<cfset setEncoding("URL", "utf-8")>
<cfset setEncoding("Form", "utf-8")> I placed them in that page just to see if it will work, hey sometimes you have to try something stupid to get to the correct answer.

Dags

<cfcomponent>

<cfprocessingdirective pageencoding="utf-8">

<cffunction name="translate" returntype="string" output = "no">
<cfargument name="locale" default="en">
<cfargument name="t_id">
<cfargument name="page_id">

<!---<cftry>--->
<cfquery name="translations" datasource="#request.tracebackDSN#">
SELECT #locale# as locale
FROM translations
WHERE 0 = 0
AND translation_id = #t_id#
AND page_id = #page_id#
</cfquery>
<!---<cfcatch type="any">Translation Error</cfcatch>
</cftry>--->

<!--- Remove any leading and trailing spaces SM@05/13/2005 --->
<cfset tValue = #translations.locale#>
<cfset tValue = trim(tValue)>

<cfreturn tValue>
</cffunction>

<cffunction name="btranslate">
<cfargument name="locale" default="en">
<cfargument name="t_id">
<cfargument name="page_id">

<cftry>
<cfquery name="translations" datasource="#request.tracebackDSN#">
SELECT #locale# as locale
FROM translations
WHERE 0 = 0
AND translation_id = #t_id#
AND page_id = #page_id#
</cfquery>
<cfcatch type="any">Translation Error</cfcatch>
</cftry>

<!--- Remove any leading and trailing spaces SM@05/13/2005 --->
<cfset tValue = #translations.locale#>
<cfset tValue = trim(tValue)>

<cfreturn trim(tValue)>
</cffunction>
</cfcomponent>
Inspiring
January 18, 2007
one other thing you can do with your code is move your
<!--- Set encoding to utf-8. --->
<cfset setEncoding("URL", "utf-8")>
<cfset setEncoding("Form", "utf-8")>

as well as

<!--- Set the output encoding to utf-8 --->
<cfcontent type="text/html; charset=utf-8">

into your Application.cfm file. You will then not need to have that in any other pages, only <cfprocessingdirective pageencoding="utf-8">. And remember yo incude that line as first line of EVERY page, even if a page is only used through <cfinclude>!!!

Inspiring
January 18, 2007
could the problem be in the ssite.translate function? can you post the code for it? how does it use the Session.locale?
another thing to check would be if the spanish text in the db is stored correctly. could be you are seing exactly what is stored in the db...
one other thing to try: add &characterSetResults=UTF-8 to the end of the JDBC URL in CF Admin. I have just finished a website in Lao language with CFMX7 and MySQL, and that little line has made it possible...