Get languages spoken by an assembly

/// <summary>  
/// Returns the languages a given assembly "speaks".  
/// </summary>  
/// <param name="asm">The asm to analyze</param>  
/// <returns>Array of cultures installed for the assembly</returns>  
static CultureInfo[] GetSupportedLanguages(Assembly asm)  
{ 
    ArrayList al = new ArrayList(); 
    DirectoryInfo dir = new DirectoryInfo( 
        Path.GetDirectoryName(asm.Location) 
        ); 
    Regex sattelitePattern = new Regex(@"(^w{2}-w{2}$)|(^w{2}$)"); 
    bool englishFound = false; 
    foreach(DirectoryInfo subDir in dir.GetDirectories()) 
    { 
        if (sattelitePattern.IsMatch(subDir.Name)) 
        { 
            // has cultureinfo form 
            CultureInfo ci = new CultureInfo(subDir.Name); 
            if (ci!=null) 
            { 
                // it's a culture 
                string strCultureFileName = 
                    String.Format(CultureInfo.InvariantCulture, "{0}.resources.dll", 
                    Path.GetFileNameWithoutExtension(asm.Location)); 
                if (File.Exists( 
                    Path.Combine(subDir.FullName, 
                    strCultureFileName))) 
                { 
                    try 
                    { 
                        asm.GetSatelliteAssembly(ci); 
                        // it's for the specific assembly 
                        al.Add(ci); 
                        if (ci.LCID == 1033) 
                            englishFound = true; 
                    } 
                    catch(FileLoadException e) 
                    { 
                        // invalid version/strong name 
                    } 
                    catch(BadImageFormatException e) 
                    { 
                        // Not a resource 
                    } 
                } 
            } 
        } 
    } 
    if (!englishFound) 
        al.Add(new CultureInfo(1033)); // add default language (english) if it wasn't found as satelites 
    return (CultureInfo[]) al.ToArray(typeof(CultureInfo)); 
}
Google
m@kli.dk @klinkby RSS feed  GitHub