Showing posts with label matter. Show all posts
Showing posts with label matter. Show all posts

Wednesday, March 7, 2012

Displaying data using correct culture

I'm working on a report that displays datetime columns and no matter what I set as my currentculture the datetime columns always use the US format: mm/dd/yyyy.

On the report designer I see there's a "Language" property but I cannot find how to set this from the code.

Note that I'm not talking about localizing the report interface. I know there's adowloadable language pack for this.
What I need is the that the data is displayed using the correct formating for numbers and dates for the current active locale.

Any idea?

Yeah, that's Globalization (as you know)... are you returning a DateTime field in a GridView and supplying the DataFormatString yourself... or are you doing a CONVERT(VARCHAR, MyDate, 101) in SQL? (Because if SQL is the one formatting it, you're getting a string... not a DateTime)

Thanks,

|||

I'm using a ReportViewer in LocalMode and the stored proc that returns data does not format it in any way:

select
a.Date,...
from table a
where ...

Where a.Date is a datetime field.

|||

HI,vmasanas :

As localization is not a built-in feature for Reporting Services, people have tried a a variety of techniques for localizing reports. One technique is to use the LocID propertie to create a version of the RDL in each language. Another approach is to have a single report and create a custom assembly to load the strings for each label. Here is an relatively easy technique for providing localized reports using hidden parameters.

First, you will need to create a table to hold the translations. The following T-SQL will create the table and add a few labels for translating the Product Line Sales sample that is included with the product.

CREATE TABLE [dbo].[Translations](
[Label] [nvarchar](150) NOT NULL,
[Language] [nvarchar](10) NOT NULL,
[Translation] [nvarchar](150) NOT NULL
)
GO
INSERT [dbo].[Translations]
VALUES ('Top Stores','fr-fr','Les meilleurs magasins')
INSERT [dbo].[Translations]
VALUES ('Top Employees','fr-fr','Les meilleurs employés')
INSERT [dbo].[Translations]
VALUES ('Top Stores','en-US','Top Stores')
INSERT [dbo].[Translations]
VALUES ('Top Employees','en-US','Top Employees')
INSERT [dbo].[Translations]
VALUES ('Top Stores','de-DE','Oberseite Speicher')
INSERT [dbo].[Translations]
VALUES ('Top Employees','de-DE','Obere Angestellte')
GO

Now, create a query that will return the set of labels for a given language. Add a dataset named 'Labels' that has the following query:

SELECT Label, Language, Translation
FROM Translations
WHERE (Language = @.Language)

By default, a new report parameter will be created named Language. If you would like to automatically bind the translations to the user's language, you can delete this parameter and bind it to User!Language in the dataset properties dialog. However, for testing purposes it is easier to just type it in for preview.

Next, you will need to create a hidden, multi-valued parameter called 'Labels'. Set the Available Values to the Labels dataset, the Value field to 'Labels' and the Label field to 'Translation'. Set the default values to the same dataset and the Value field to 'Labels'. This is important as you don't have access to the available values from within the report, only the actual values. When the user runs the report, the parameter value will contain all of the labels.

Now, add a function to the report (from the code tab of the Report->Report Properties menu item)

Public Function GetLabel(P as Parameter, Label as String) as String
Dim i As Integer
For i = 0 to Ubound(P.Value)
If (P.Value(i) = Label) Then Return P.Label(i)
Next i
Return Label
End Function

This function will find the translated label within the supplied multi-valued parameter. If the label is not found, the passed in value is returned. This is important as you may have a user language for which you have not created the translations.

The only thing left is to change the static labels in your report to use this new function. For example, to translate the Top Employees label, use the function

=Code.GetLabel(Parameters!Labels,"Top Employees")

If i misunderstand you about your question, please feel free to correct me and i will try to help you with more information.

I hope the above information will be helpful. If you have any issues or concerns, please let me know. It's my pleasure to be

of assistance

|||

Rex,

thanks for you comments, but what I was asking for is slightly different. What I need is that special data types are formated accordingly to the current culture. For example if I get this data:

SELECT aDate, aNumber FROM ...

I'd expect that when the user is using a en-US locale the date is displayed as mm/dd/yyyy and the number "##,###.##". Otherwise, if current culture is es-ES I'd expect to see dd/mm/yyyy and "##.###,##" respectively.

Either I'm doing something wrong or I'm missing a point because this does not work for me. I have a language selector on the page and no matter what I use I always see the data formated as en-US. And, no, I don't do any formating, convertion or modification on these fields. They come right from the db.

|||

As for theReportingService 2005, it does support some localization
features, they include:

1)the localization of the built-in UI components, such as SSRS's
htmlviewer, report designer

2) some simple localization on the SSRS report's data

For 1), the SSRS has done the work for us already, for example, when we
visit the html report, the UI elements on the htmlviewer(button or other UI
element's text) will render the localized representation according to
client-side browser's user-language setting.

For 2), if we want to do some simple localization on the static data/text
displayed on our report, we can dynamically format them according to the
"User!Language" parameter in our report expression. Or you can even build
custom assembly that has custom code logic to generate localizaed text(from
net resource ) accordin to this parameter.

You can find all the localization support of SSRS 2005 in the BOL:

#International Considerations forReporting Services
http://msdn2.microsoft.com/en-us/library/ms156493.aspx


While leveraging User!Language certainly will work in terms of allowing you localize your reports, doing so is admittedly a pain in the tail – you essentially have to replace all your label text with expressions that call into code that you write to do "label localization" ala:

= MyNameSpace.MyConvertingClass.MyConverter("someLabelName", User!Language)

Your code takes the name of a label and the language to use, then convert the date or the symbol to be the the currect one and return it

|||

HI,vmasanas:

We are marking this issue as "Answered". If you have any new findings or concerns, please feel free to unmark the issue.
Thank you for your understanding!

|||

Rex,

thanks for your support but I think we are talking of different things here.

What I'm trying to get is that a datetime column displays it's data using a format accordingly to the current culture selected by the user at the time.

When I'm designing a report I see a property called "Language" when, if changed, displays data correctly. What I would need is access to this property at runtime so the report is configured dinamically depending on the user preferences.

I've not been able to figure where in the object hierarchy I can get access to this property.

|||

I had a similar problem and here is how to solve it:

CultureInfo ci;
// Format the current date and time in various ways.

// Display the thread current culture, which is used to format the values.
ci = Thread.CurrentThread.CurrentCulture;
ci = new CultureInfo("en-IE");
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci; //I guess you did'nt set this one

Hanin

|||

I wish you were right but no :(

Protected Overrides Sub OnInit(ByVal eAs System.EventArgs)
' Set the current culture
Thread.CurrentThread.CurrentUICulture = PageCulture
Thread.CurrentThread.CurrentCulture = PageCulture
' Localize portalsettings
Services.Localization.Localization.LocalizePortalSettings()
MyBase.OnInit(e)
End Sub

I'm trying to use this inside aDotNetNuke module but seems there's something missing here since I've never had any problem with localization inside DNN.