private string FindBestTranslation(IList<string> preferredLocales, string key, out CultureInfo culture)
{
culture = null;
TranslationTable match = null;
for (int jj = 0; jj < preferredLocales.Count; jj++)
{
// parse the locale.
string language = preferredLocales[jj];
int index = language.IndexOf('-');
if (index != -1)
{
language = language.Substring(0, index);
}
// search for translation.
string translatedText = null;
for (int ii = 0; ii < m_translationTables.Count; ii++)
{
TranslationTable translationTable = m_translationTables[ii];
// all done if exact match found.
if (translationTable.Locale.Name == preferredLocales[jj])
{
if (translationTable.Translations.TryGetValue(key, out translatedText))
{
culture = translationTable.Locale;
return translatedText;
}
}
// check for matching language but different region.
if (match == null && translationTable.Locale.TwoLetterISOLanguageName == language)
{
if (translationTable.Translations.TryGetValue(key, out translatedText))
{
culture = translationTable.Locale;
match = translationTable;
}
continue;
}
}
// take a partial match if one found.
if (match != null)
{
return translatedText;
}
}
// no translations available.
return null;
}