Boolean values in local language

霸气de小男生 提交于 2021-02-07 12:40:42

问题


This question has been asked long time ago on serverfault but no working awnser. I'm hoping somebody has encountered it and found a solution since then.

Example:

<%
Response.Write True
Response.Write "<hr>"
Response.Write "test:" & True
%>

Output:

True
--------------
test:Waar

As you can see, as soon as you combine the output, its turned into a local string ('Waar' is dutch for true). I need it to stay "True".

How can I change this? I dont mind putting some code at the beginning of the pages, but I cannot change all instances of True in the entire code. So creating a function like below to return the correct string wont do.

Function PB(pVal)
  If pVal Then
    PB = "true"
  Else
    PB = "false"
  End If
End Function

回答1:


Strange, I cannot get my IIS to output a boolean value in my local language no matter which locale is set.

Did you find this KB article already?

There is a description of a registry setting that could do the trick:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\OLEAUT

I don't want to mess with my production server, so I didn't try this yet.


FIRST ANSWER:

You could 'trick' VB to use your own functions. If you add these functions to the top of your page, VB will prefer these over the builtin ones, in this case CStr and Trim. Maybe this helps.

    Function PB(pVal)
        If pVal Then
            PB = "true"
        Else
            PB = "false"
        End If
    End Function

    Function CStr(pVal)
        CStr = PB(pVal)
    End Function

    Function Trim(pVal)
        Trim = PB(pVal)
    End Function

Output:

    True
    --------------
    test:true
    --------------
    test:true
    --------------
    test:true



回答2:


I do something like this, but currently don't have a machine to test your case:

<%
Response.Write FormatDateTime(Now) & "<br />"
oldLocale = SetLocale(1026) 'Bulgarian
Response.Write FormatDateTime(Now) & "<br />"
SetLocale oldLocale 
Response.Write FormatDateTime(Now) & "<br />"
%>

... from another SO question, it looks the above doesn't work.

What hapopens if you try these: Response.Write "test:" & CStr(True) or Response.Write "test:" & Trim(True) with or w/o SetLocale?




回答3:


that is just like string concatenation works in vbscript.

from the vbscript language reference:

"Whenever an expression is not a string, it is converted to a String subtype. If both expressions are Null, result is also Null. However, if only one expression is Null, that expression is treated as a zero-length string ("") when concatenated with the other expression. Any expression that is Empty is also treated as a zero-length string."




回答4:


If the SetLocale didn't work, then I think your best bet is to create a function.

Function TF(B)
   If B Then
      TF = "True"
   Else
      TF = "False"
   End If
End Function

Then instead of saying this:

Response.Write "test:" & True

You'd say this:

Response.Write "test:" & TF(True)



回答5:


I was "caught" too with this "issue". Indeed, this is not an issue :

THE PROBLEM (EXAMPLE)

I had the same problem when using a boolean data in a SQL Statement. On my French server, my SQL statement was the following :

<%
'Set my boolean value
Dim myBoolean
myBoolean = True

'Set my SQL Statement
Dim MySQLStatement
MySQLStatement = "SELECT * FROM MyTable WHERE MyBooleanField = " & myBoolean
'=> Here, as MySQLStatement is a STRING, the boolean data was "converted/rendered" as a localized string. So that leads to this output :
'=> SELECT * FROM MyTable WHERE MyBooleanField = Vrai
'Obviously, that SQL Statement is incorrect, because the SQL Engine does NOT understand what is the "Vrai" word - It should be "True" instead.
%>

THE EXPLANATIONS :

  • It does not matter which regional settings are set on your Windows System : Nothing happens to the underlying data. A boolean data type is STILL a BOOLEAN, in English, or French, or German, Russian, Thai, ..or any language you want.
  • The fact is that the data is simply being RENDERED as a localized STRING (like dates).

THE SOLUTION

After a lot of reading over forums threads, the solution is not to change regional settings on the Windows system, nor changing Registry keys, nor changing Session.LCID, ... The absolute and code-only solution is to convert the Boolean value (True|False) to an Integer (0|1). Then, this Integer will be safely usable in a string, and will remain (0|1).

Here is the safe way to use/convert/render a Boolean value in a non-localized form : Use an Integer value.

<%
'Set my boolean value
Dim myBoolean
myBoolean = True

'Set my SQL Statement
Dim MySQLStatement
MySQLStatement = "SELECT * FROM MyTable WHERE MyBooleanField = " & BoolToInt(myBoolean)
'=> Here, as MySQLStatement is a STRING, and as the boolean data was previously "converted/rendered" as an integer, we got this correct SQL Statement :
'=> SELECT * FROM MyTable WHERE MyBooleanField = 1
'This SQL Statement is correct, as the SQL Engine DOES understand that 1 is a boolean.

'This Function Returns an INTEGER value based on a BOOLEAN value
Function BoolToInt(v)
    'INPUT:
    'v      Boolean value    

    'OUTPUT:
    'Integer (0|1)    

    Dim b_OUT
    b_OUT = v    

    'If the Input value is a "True" boolean value (in any language)
    if (b_OUT = True) then
        BoolToInt = cint(1)
    'If the Input value is a "False" boolean value (in any language)
    elseif (b_OUT = False) then
        BoolToInt = cint(0)
    end if
End Function 'BoolToInt
%>

I really hope it save your day !




回答6:


You could try setting Response.LCID or Session.LCID to 1033 at the beginning of your page, but this also influences how others things (likes dates and currency variables) are displayed.

1033 is the Locale Id for the US




回答7:


Attempt 2:

<%
arr=Array("False","True")

Response.Write True
Response.Write "<br /><br />"

Response.Write "" & arr( abs(True) ) & "<br />"
Response.Write "" & arr( abs(False) ) & "<br />"
%>



回答8:


I found a solution if you dont mind changing regional settings to united states one. It involves changing the registry. This has been tested in windows 7 spanish and now it give native True/False instead of Verdadero/Falso.

  1. Navigate to HKEY_USERS/.Default/Control Panel/International and take a nice picture with your phone in case you mess things up. you can also make a backup of the registry keys by clicking "international" and then File/export

  2. Open a notepad and paste this inside (you can alternatively change values by hand one by one in the registry):

----------------- paste what is below this line -------------

Windows Registry Editor Version 5.00

[HKEY_USERS\.DEFAULT\Control Panel\International]
"Locale"="00000409"
"LocaleName"="en-US"
"s1159"=""
"s2359"=""
"sCountry"="United States"
"sCurrency"="$"
"sDate"="/"
"sDecimal"="."
"sGrouping"="3;0"
"sLanguage"="ENU"
"sList"=","
"sLongDate"="dddd, MMMM dd, yyyy"
"sMonDecimalSep"="."
"sMonGrouping"="3;0"
"sMonThousandSep"=","
"sNativeDigits"="0123456789"
"sNegativeSign"="-"
"sPositiveSign"=""
"sShortDate"="M/d/yyyy"
"sThousand"=","
"sTime"=":"
"sTimeFormat"="h:mm:ss tt"
"sShortTime"="h:mm tt"
"sYearMonth"="MMMM, yyyy"
"iCalendarType"="1"
"iCountry"="1"
"iCurrDigits"="2"
"iCurrency"="0"
"iDate"="0"
"iDigits"="2"
"NumShape"="1"
"iFirstDayOfWeek"="6"
"iFirstWeekOfYear"="0"
"iLZero"="1"
"iMeasure"="1"
"iNegCurr"="0"
"iNegNumber"="1"
"iPaperSize"="1"
"iTime"="0"
"iTimePrefix"="0"
"iTLZero"="0"

[HKEY_USERS\.DEFAULT\Control Panel\International\Geo]
"Nation"="244"

----------------- do not paste this line -------------

  1. Save it as english.reg (or whatever.reg)

  2. Double click it.

I didnt have to restart my computer for changes to take effect.

-------- UPDATE ----------

Does not work. If I make a blank asp page with this: <%=isnumeric(6)%> it answers True (in english) but as soon as I concatenate a True in a string it becames Verdadero again.



来源:https://stackoverflow.com/questions/15760035/boolean-values-in-local-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!