[Mono-aspnet-list] How to list all the currently available languages?

Antonio Anzivino anzivino at studenti.unina.it
Mon Mar 29 14:14:04 EDT 2010


Hello,

I want to develop a control that allows the user to switch language for the
website, while its default is still detected using browser's headers. I want
it to scan the current assembly to find if there have been deployed
satellite assemblies in order to display the user only the list of languages
actually available, without recompiling the code every time a new language
is added. I do localization using local resources and satellite assemblies,
the simplest way.

 

What I'm stuck in is the method that does the scan: its behaviour differs
between Visual Studio development server and Mono (I'll consider opening a
bug ticket if you tell me that my code is correct).

 

Basically, when you compile your Console/WinForms application either under
Windows or with xbuild, satellite assemblies are deployed as
Appname.resources.dll inside a subdirectory of bin directory named as
language code (ie. bin/fr/Appname.resources.dll for French).

When you compile an ASP.NET application with Visual Studio the same happens.

However, I found that xbuild does not create satellite assemblies, however
localization still works on my app.

 

So, let's come to the code and its differences in behaviour.

 

public static string[] ListAvailableLocalizations()

        {

            List<string> ret = new List<string>();

            //Add first language

 
ret.Add(Assembly.GetExecutingAssembly().GetName().CultureInfo.ToString());

 

            string assemblyName =
Path.GetFileNameWithoutExtension(typeof(LanguageUtilities).Assembly.CodeBase
);

            

            string resourceFileName = assemblyName + ".resources.dll";

 

            // CREDIT
http://adilakhter.wordpress.com/2007/12/17/get-current-executing-assemblys-d
irectory/

            // ASP.NET assemblies under Windows are deployed elsewhere

            string binDirectoryPath =
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetN
ame().CodeBase).Substring(6);

            foreach (string subdirectory in
Directory.GetDirectories(binDirectoryPath))

            {

                if (File.Exists(Path.Combine(subdirectory,
resourceFileName)))

 
ret.Add(subdirectory.Substring(subdirectory.LastIndexOf(Path.DirectorySepara
torChar) + 1));

 

            }

            return ret.ToArray();

        }

 

First of all, I add the current assembly's language, usually English (J), to
the list, then start scanning the CodeBase directory. I use substring(6)
because CodeBase is expressed as URI (file://...) and it won't be accepted
by Path class. If there exists Appname.resources.dll I add the directory's
name to the list.

 

Now, I develop under Windows using a local directory such as E:\MyApp. This
code WORKS FINE, as the codebase coincides with the bin/ directory of the
website, and the code is also reusable for Winforms applications.

When running the same code on Mono, using an independent instance of
mod-mono-server2 serving Apache via TCP socket, I found that the codebase is
moved to /tmp/djechelon-temp-aspnet-0 and its subdirectories rather than the
application's path as seen by mod-mono. This alters the codebase, and I also
found, by exploring it, that assemblies are deployed to leaf directories.

 

Is there any other smarter way to scan for available languages? And also, is
my code correct? It works fine under VS development server, not under Mono.
You could build a test case using an ASPX page with a few translations, then
trying to list all the available languages on-screen using this method.

 

Thank you.

Antonio Anzivino

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-aspnet-list/attachments/20100329/6b9ac6d2/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 4496 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-aspnet-list/attachments/20100329/6b9ac6d2/attachment.bin 


More information about the Mono-aspnet-list mailing list