mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 18:57:19 +02:00
[Localization] Move PowerToys Run string resources from xaml files to resx (#6165)
* Removed xaml files, added resx file and removed references for PowerLauncher project * Added resx file for wox.plugin * Moved Calculator resources to resx * Migrated resources for Folder and Indexer plugins * Migrated resources for Program and Shell plugin * Migrated resources for URI and Window Walker * Removed GetTranslation, tests need to be refactored * Removed internationalization classes * Removed Wox.Core.Resource references * Fixed Programs plugin tests * Fixed tests * Removed language xaml files from installer * Added locProject.json files * Fixed resource not found error * Reverted addition of resx file for Wox.Plugin
This commit is contained in:
@@ -1,73 +0,0 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Wox.Core.Resource
|
||||
{
|
||||
internal static class AvailableLanguages
|
||||
{
|
||||
public static Language English { get; set; } = new Language("en", "English");
|
||||
|
||||
public static Language Chinese { get; set; } = new Language("zh-cn", "中文");
|
||||
|
||||
public static Language Chinese_TW { get; set; } = new Language("zh-tw", "中文(繁体)");
|
||||
|
||||
public static Language Ukrainian { get; set; } = new Language("uk-UA", "Українська");
|
||||
|
||||
public static Language Russian { get; set; } = new Language("ru", "Русский");
|
||||
|
||||
public static Language French { get; set; } = new Language("fr", "Français");
|
||||
|
||||
public static Language Japanese { get; set; } = new Language("ja", "日本語");
|
||||
|
||||
public static Language Dutch { get; set; } = new Language("nl", "Dutch");
|
||||
|
||||
public static Language Polish { get; set; } = new Language("pl", "Polski");
|
||||
|
||||
public static Language Danish { get; set; } = new Language("da", "Dansk");
|
||||
|
||||
public static Language German { get; set; } = new Language("de", "Deutsch");
|
||||
|
||||
public static Language Korean { get; set; } = new Language("ko", "한국어");
|
||||
|
||||
public static Language Serbian { get; set; } = new Language("sr", "Srpski");
|
||||
|
||||
public static Language Portuguese_BR { get; set; } = new Language("pt-br", "Português (Brasil)");
|
||||
|
||||
public static Language Italian { get; set; } = new Language("it", "Italiano");
|
||||
|
||||
public static Language Norwegian_Bokmal { get; set; } = new Language("nb-NO", "Norsk Bokmål");
|
||||
|
||||
public static Language Slovak { get; set; } = new Language("sk", "Slovenský");
|
||||
|
||||
public static Language Turkish { get; set; } = new Language("tr", "Türkçe");
|
||||
|
||||
public static List<Language> GetAvailableLanguages()
|
||||
{
|
||||
List<Language> languages = new List<Language>
|
||||
{
|
||||
English,
|
||||
Chinese,
|
||||
Chinese_TW,
|
||||
Ukrainian,
|
||||
Russian,
|
||||
French,
|
||||
Japanese,
|
||||
Dutch,
|
||||
Polish,
|
||||
Danish,
|
||||
German,
|
||||
Korean,
|
||||
Serbian,
|
||||
Portuguese_BR,
|
||||
Italian,
|
||||
Norwegian_Bokmal,
|
||||
Slovak,
|
||||
Turkish,
|
||||
};
|
||||
return languages;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,230 +0,0 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Windows;
|
||||
using Wox.Core.Plugin;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.Infrastructure.UserSettings;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.Core.Resource
|
||||
{
|
||||
public class Internationalization
|
||||
{
|
||||
public Settings Settings { get; set; }
|
||||
|
||||
private const string Folder = "Languages";
|
||||
private const string DefaultFile = "en.xaml";
|
||||
private const string Extension = ".xaml";
|
||||
private readonly List<string> _languageDirectories = new List<string>();
|
||||
private readonly List<ResourceDictionary> _oldResources = new List<ResourceDictionary>();
|
||||
|
||||
public Internationalization()
|
||||
{
|
||||
AddPluginLanguageDirectories();
|
||||
LoadDefaultLanguage();
|
||||
|
||||
// we don't want to load /Languages/en.xaml twice
|
||||
// so add wox language directory after load plugin language files
|
||||
AddWoxLanguageDirectory();
|
||||
}
|
||||
|
||||
private void AddWoxLanguageDirectory()
|
||||
{
|
||||
var directory = Path.Combine(Constant.ProgramDirectory, Folder);
|
||||
_languageDirectories.Add(directory);
|
||||
}
|
||||
|
||||
private void AddPluginLanguageDirectories()
|
||||
{
|
||||
foreach (var plugin in PluginManager.GetPluginsForInterface<IPluginI18n>())
|
||||
{
|
||||
var location = Assembly.GetAssembly(plugin.Plugin.GetType()).Location;
|
||||
var dir = Path.GetDirectoryName(location);
|
||||
if (dir != null)
|
||||
{
|
||||
var pluginThemeDirectory = Path.Combine(dir, Folder);
|
||||
_languageDirectories.Add(pluginThemeDirectory);
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.Error($"|Internationalization.AddPluginLanguageDirectories|Can't find plugin path <{location}> for <{plugin.Metadata.Name}>");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadDefaultLanguage()
|
||||
{
|
||||
LoadLanguage(AvailableLanguages.English);
|
||||
_oldResources.Clear();
|
||||
}
|
||||
|
||||
public void ChangeLanguage(string languageCode)
|
||||
{
|
||||
languageCode = languageCode.NonNull();
|
||||
Language language = GetLanguageByLanguageCode(languageCode);
|
||||
ChangeLanguage(language);
|
||||
}
|
||||
|
||||
private Language GetLanguageByLanguageCode(string languageCode)
|
||||
{
|
||||
var lowercase = languageCode.ToLower();
|
||||
var language = AvailableLanguages.GetAvailableLanguages().FirstOrDefault(o => o.LanguageCode.ToLower() == lowercase);
|
||||
if (language == null)
|
||||
{
|
||||
Log.Error($"|Internationalization.GetLanguageByLanguageCode|Language code can't be found <{languageCode}>");
|
||||
return AvailableLanguages.English;
|
||||
}
|
||||
else
|
||||
{
|
||||
return language;
|
||||
}
|
||||
}
|
||||
|
||||
public void ChangeLanguage(Language language)
|
||||
{
|
||||
language = language.NonNull();
|
||||
|
||||
Settings.Language = language.LanguageCode;
|
||||
|
||||
RemoveOldLanguageFiles();
|
||||
if (language != AvailableLanguages.English)
|
||||
{
|
||||
LoadLanguage(language);
|
||||
}
|
||||
|
||||
UpdatePluginMetadataTranslations();
|
||||
}
|
||||
|
||||
public bool PromptShouldUsePinyin(string languageCodeToSet)
|
||||
{
|
||||
var languageToSet = GetLanguageByLanguageCode(languageCodeToSet);
|
||||
|
||||
if (Settings.ShouldUsePinyin)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (languageToSet != AvailableLanguages.Chinese && languageToSet != AvailableLanguages.Chinese_TW)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (MessageBox.Show("Do you want to turn on search with Pinyin?", string.Empty, MessageBoxButton.YesNo) == MessageBoxResult.No)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void RemoveOldLanguageFiles()
|
||||
{
|
||||
var dicts = Application.Current.Resources.MergedDictionaries;
|
||||
foreach (var r in _oldResources)
|
||||
{
|
||||
dicts.Remove(r);
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadLanguage(Language language)
|
||||
{
|
||||
var dicts = Application.Current.Resources.MergedDictionaries;
|
||||
var filename = $"{language.LanguageCode}{Extension}";
|
||||
var files = _languageDirectories
|
||||
.Select(d => LanguageFile(d, filename))
|
||||
.Where(f => !string.IsNullOrEmpty(f))
|
||||
.ToArray();
|
||||
|
||||
if (files.Length > 0)
|
||||
{
|
||||
foreach (var f in files)
|
||||
{
|
||||
var r = new ResourceDictionary
|
||||
{
|
||||
Source = new Uri(f, UriKind.Absolute),
|
||||
};
|
||||
dicts.Add(r);
|
||||
_oldResources.Add(r);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<Language> LoadAvailableLanguages()
|
||||
{
|
||||
return AvailableLanguages.GetAvailableLanguages();
|
||||
}
|
||||
|
||||
public string GetTranslation(string key)
|
||||
{
|
||||
var translation = Application.Current.TryFindResource(key);
|
||||
if (translation is string)
|
||||
{
|
||||
return translation.ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.Error($"|Internationalization.GetTranslation|No Translation for key {key}");
|
||||
return $"No Translation for key {key}";
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdatePluginMetadataTranslations()
|
||||
{
|
||||
foreach (var p in PluginManager.GetPluginsForInterface<IPluginI18n>())
|
||||
{
|
||||
if (!(p.Plugin is IPluginI18n pluginI18N))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
p.Metadata.Name = pluginI18N.GetTranslatedPluginTitle();
|
||||
p.Metadata.Description = pluginI18N.GetTranslatedPluginDescription();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Exception($"|Internationalization.UpdatePluginMetadataTranslations|Failed for <{p.Metadata.Name}>", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string LanguageFile(string folder, string language)
|
||||
{
|
||||
if (Directory.Exists(folder))
|
||||
{
|
||||
string path = Path.Combine(folder, language);
|
||||
if (File.Exists(path))
|
||||
{
|
||||
return path;
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.Error($"|Internationalization.LanguageFile|Language path can't be found <{path}>");
|
||||
string english = Path.Combine(folder, DefaultFile);
|
||||
if (File.Exists(english))
|
||||
{
|
||||
return english;
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.Error($"|Internationalization.LanguageFile|Default English Language path can't be found <{path}>");
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
namespace Wox.Core.Resource
|
||||
{
|
||||
public static class InternationalizationManager
|
||||
{
|
||||
private static Internationalization instance;
|
||||
private static object syncObject = new object();
|
||||
|
||||
public static Internationalization Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (instance == null)
|
||||
{
|
||||
lock (syncObject)
|
||||
{
|
||||
if (instance == null)
|
||||
{
|
||||
instance = new Internationalization();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
namespace Wox.Core.Resource
|
||||
{
|
||||
public class Language
|
||||
{
|
||||
public Language(string code, string display)
|
||||
{
|
||||
LanguageCode = code;
|
||||
Display = display;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// E.g. En or Zh-CN
|
||||
/// </summary>
|
||||
public string LanguageCode { get; set; }
|
||||
|
||||
public string Display { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user