Skip to content

Commit

Permalink
Fixed null/empty/unsupported CultureInfos and AppTranslations
Browse files Browse the repository at this point in the history
  • Loading branch information
Neonalig committed May 22, 2021
1 parent 05dd682 commit 7ac44a6
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 39 deletions.
2 changes: 0 additions & 2 deletions Commands/CommandsMediaInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Resources;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using QMediaVSIX.i18n;
Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.1.2.0")]
[assembly: AssemblyFileVersion("0.1.2.0")]
[assembly: AssemblyVersion("0.1.3.0")]
[assembly: AssemblyFileVersion("0.1.3.0")]

[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
8 changes: 6 additions & 2 deletions QMediaVSIX.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,12 @@
<VSCTCompile Include="Commands\QMediaVSIXPackage.ja-JP.vsct">
<ResourceName>Menus.ctmenu</ResourceName>
</VSCTCompile>
<Content Include="i18n\ja-JP\Extension.vsixlangpack" />
<Content Include="i18n\en-US\Extension.vsixlangpack" />
<Content Include="i18n\ja-JP\Extension.vsixlangpack">
<IncludeInVSIX>true</IncludeInVSIX>
</Content>
<Content Include="i18n\en-US\Extension.vsixlangpack">
<IncludeInVSIX>true</IncludeInVSIX>
</Content>
<Content Include="i18n\ja-JP\gpl-3.0.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<IncludeInVSIX>true</IncludeInVSIX>
Expand Down
19 changes: 3 additions & 16 deletions QMediaVSIXPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Threading;

using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Threading;

using QMediaVSIX.Commands;

using Task = System.Threading.Tasks.Task;

#endregion
Expand All @@ -40,12 +36,7 @@ namespace QMediaVSIX {
/// To get loaded into VS, the package must be referred by &lt;Asset Type="Microsoft.VisualStudio.VsPackage" ...&gt; in .vsixmanifest file.
/// </para>
/// </remarks>
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true),
Guid(PackageGuidString),
ProvideMenuResource("Menus.ctmenu", 1),
ProvideOptionPage(typeof(OptionPageGrid_English), "QMediaVSIX", "English", 0, 0, true),
ProvideOptionPage(typeof(OptionPageGrid_Japanese), "QMediaVSIX", "日本語", 0, 0, true)
]
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)][Guid(PackageGuidString)][ProvideMenuResource("Menus.ctmenu", 1)][ProvideOptionPage(typeof(OptionPageGrid_English), "QMediaVSIX", "English", 0, 0, true)][ProvideOptionPage(typeof(OptionPageGrid_Japanese), "QMediaVSIX", "日本語", 0, 0, true)]
public sealed class QMediaVSIXPackage : AsyncPackage {
/// <summary>
/// QMediaVSIXPackage GUID string.
Expand Down Expand Up @@ -111,19 +102,15 @@ public abstract class OptionPageGrid_i18n : DialogPage {
}

public class OptionPageGrid_English : OptionPageGrid_i18n {
[Category("English"),
DisplayName("Fallback Delay"),
Description("The delay before the extension falls back to media keys.\nIf controlling a media source fails, the extension waits for some time to ensure it wasn't just lagging, and then manually invokes keyboard media keys (which may control the wrong media source by accident.)\n\nSet to '0' to use exclusively media keys, and any negative values to disable the fallback entirely.")]
[Category("English")][DisplayName("Fallback Delay")][Description("The delay before the extension falls back to media keys.\nIf controlling a media source fails, the extension waits for some time to ensure it wasn't just lagging, and then manually invokes keyboard media keys (which may control the wrong media source by accident.)\n\nSet to '0' to use exclusively media keys, and any negative values to disable the fallback entirely.")]
public override int FallbackDelay {
get => Int_FallbackDelay;
set => Int_FallbackDelay = value;
}
}

public class OptionPageGrid_Japanese : OptionPageGrid_i18n {
[Category("日本語"),
DisplayName("フォールバック遅延"),
Description(">この設定は拡張機能がメディアキーにフォールバックするまでの遅延時間です。\nボタンコマンドが失敗した場合、エクステンションは遅延していないことを確認するためにしばらく待ちます。遅延後、メディアからの応答がない場合、拡張機能は手動でキーボードキーを押します。\nこれは、誤ったメディアソースを制御する可能性があるため、通常は避けます。\n\nキーボードキーのみを使用する場合は「0」に設定します。\nまた、負の値を設定すると、フォールバックを完全に無効にすることができます。")]
[Category("日本語")][DisplayName("フォールバック遅延")][Description(">この設定は拡張機能がメディアキーにフォールバックするまでの遅延時間です。\nボタンコマンドが失敗した場合、エクステンションは遅延していないことを確認するためにしばらく待ちます。遅延後、メディアからの応答がない場合、拡張機能は手動でキーボードキーを押します。\nこれは、誤ったメディアソースを制御する可能性があるため、通常は避けます。\n\nキーボードキーのみを使用する場合は「0」に設定します。\nまた、負の値を設定すると、フォールバックを完全に無効にすることができます。")]
public override int FallbackDelay {
get => Int_FallbackDelay;
set => Int_FallbackDelay = value;
Expand Down
11 changes: 9 additions & 2 deletions i18n/AppTranslations.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
#region Copyright (C) 2017-2020 Starflash Studios
#region Copyright (C) 2017-2021 Starflash Studios
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License (Version 3.0)
// as published by the Free Software Foundation.
//
// More information can be found here: https://www.gnu.org/licenses/gpl-3.0.en.html
#endregion

#region Using Directives

using System;
using System.Collections.Generic;
using System.Reflection;

#endregion

namespace QMediaVSIX.i18n {
public abstract class AppTranslations {
// ReSharper disable once EmptyConstructor //Ensures inherited classes must have an empty constructor
Expand Down Expand Up @@ -42,7 +46,7 @@ static AppTranslations_Helper() {
Type AppT = typeof(AppTranslations);
// ReSharper disable once LoopCanBePartlyConvertedToQuery
foreach(Type T in Assembly.GetExecutingAssembly().GetTypes()) {
if (AppT.IsAssignableFrom(T) && AppT != T) {
if (T != null && AppT.IsAssignableFrom(T) && AppT != T) {
AppTranslations Obj = T.GetConstructor(Type.EmptyTypes).Invoke(null) as AppTranslations;
AllTrans.Add(Obj.In_Lang, Obj);
}
Expand Down Expand Up @@ -91,6 +95,9 @@ static AppTranslations_Helper() {
}
}
// ReSharper restore ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator

if (!FoundUI) { UITrans = Found ? Trans : new AppTranslations_English(); }
if (!Found) { Trans = FoundUI ? UITrans : new AppTranslations_English(); }
}
}
}
40 changes: 30 additions & 10 deletions i18n/TransUtil.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#region Copyright (C) 2017-2020 Starflash Studios
#region Copyright (C) 2017-2021 Starflash Studios
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License (Version 3.0)
// as published by the Free Software Foundation.
Expand All @@ -8,23 +8,43 @@

#nullable enable

#region Using Directives

using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
//using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
//using System.Diagnostics;

#endregion

namespace QMediaVSIX.i18n {
public static class TransUtil {
public static readonly Lang Current = new Lang(CultureInfo.CurrentCulture);
public static readonly Lang CurrentUI = new Lang(CultureInfo.CurrentUICulture);
public static readonly Lang En = new Lang("en");
public static readonly Lang EnUS = new Lang("en-us");
public static readonly Lang EnGB = new Lang("en-gb");
public static readonly Lang EnAU = new Lang("en-au", EnGB);
public static readonly Lang JaJP = new Lang("ja-jp");

// ReSharper disable twice ConstantConditionalAccessQualifier
public static readonly Lang Current = /*TryGetCulture(CultureInfo.CurrentCulture?.Name, out CultureInfo CurrentCult) ? new Lang(CurrentCult) : En*/new Lang("pt-br");
public static readonly Lang CurrentUI = /*TryGetCulture(CultureInfo.CurrentUICulture?.Name, out CultureInfo CurrentCultUI) ? new Lang(CurrentCultUI) : En*/new Lang("pt-br");

public static bool TryGetCulture(string? Code, out CultureInfo Found) {
if (Code == null || string.IsNullOrWhiteSpace(Code)) {
Found = CultureInfo.CurrentCulture;
return false;
}
try {
Found = CultureInfo.GetCultureInfo(Code);
return true;
} catch (CultureNotFoundException) {
Found = CultureInfo.CurrentCulture;
return false;
}
}
}

public readonly struct Trans {
Expand Down Expand Up @@ -114,6 +134,8 @@ public Trans( string DefKey, ReadOnlyCollection<TransKey>? Keys = null ) {
/// </remarks>
/// <returns>A translated string.</returns>
public string Get( Lang CurrentLang ) {
if (CurrentLang == null || string.IsNullOrWhiteSpace(CurrentLang.Family)) { return DefKey; }

//All languages in the same family (i.e. 'en', 'en-au', 'en-gb', etc.)
List<TransKey> Fam = new List<TransKey>();
//The root of the language (i.e. 'en')
Expand Down Expand Up @@ -143,8 +165,6 @@ public string Get( Lang CurrentLang ) {
//Debug.WriteLine("\t\tFamily.");
Fam.Add(Key);
}
} else {
//Debug.WriteLine("\t\tDifferent Family.");
}

if (CurrentLang.Variants.Contains(Key.Expected)) {
Expand Down Expand Up @@ -206,7 +226,7 @@ public string Get( Lang CurrentLang ) {
public Lang[] Variants { get; }
public Lang[] Related { get; }

public Lang( string Family, string Tag, params Lang[] Variants ) : this(Family: Family, Tag: Tag, Related: null, Variants: Variants) { }
public Lang( string Family, string Tag, params Lang[] Variants ) : this(Family, Tag, null, Variants) { }

public Lang( string Family, string Tag, Lang[]? Related, params Lang[] Variants ) {
this.Family = Family.ToUpperInvariant();
Expand All @@ -215,7 +235,7 @@ public Lang( string Family, string Tag, Lang[]? Related, params Lang[] Variants
this.Variants = Variants;
}

public Lang( string FullTag, params Lang[] Variants ) : this(FullTag: FullTag, Related: null, Variants: Variants) { }
public Lang( string FullTag, params Lang[] Variants ) : this(FullTag, Related: null, Variants: Variants) { }

public Lang( string FullTag, Lang[]? Related, params Lang[] Variants ) {
int HyphInd = FullTag.IndexOf('-');
Expand All @@ -231,7 +251,7 @@ public Lang( string FullTag, Lang[]? Related, params Lang[] Variants ) {
this.Variants = Variants;
}

public Lang( CultureInfo Cult, params Lang[] Variants ) : this(Cult: Cult, Related: null, Variants: Variants) { }
public Lang( CultureInfo Cult, params Lang[] Variants ) : this(Cult, null, Variants) { }
public Lang( CultureInfo Cult, Lang[]? Related, params Lang[] Variants ) : this(Cult.Name, Related, Variants) { }

public bool IsSameFamily( Lang L ) => Family == L.Family;
Expand Down
4 changes: 2 additions & 2 deletions i18n/en-US/AppTranslations_English.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#region Copyright (C) 2017-2020 Starflash Studios
#region Copyright (C) 2017-2021 Starflash Studios
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License (Version 3.0)
// as published by the Free Software Foundation.
//
// More information can be found here: https://www.gnu.org/licenses/gpl-3.0.en.html
#endregion

namespace QMediaVSIX.i18n.en_US {
namespace QMediaVSIX.i18n {
public class AppTranslations_English : AppTranslations {

public override Lang In_Lang => TransUtil.EnAU;
Expand Down
4 changes: 2 additions & 2 deletions i18n/ja-JP/AppTranslations_Japanese.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#region Copyright (C) 2017-2020 Starflash Studios
#region Copyright (C) 2017-2021 Starflash Studios
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License (Version 3.0)
// as published by the Free Software Foundation.
//
// More information can be found here: https://www.gnu.org/licenses/gpl-3.0.en.html
#endregion

namespace QMediaVSIX.i18n.ja_JP {
namespace QMediaVSIX.i18n {
public class AppTranslations_Japanese : AppTranslations {

public override Lang In_Lang => TransUtil.JaJP;
Expand Down
2 changes: 1 addition & 1 deletion source.extension.vsixmanifest
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="QMediaVSIX.51b65c0a-fbc0-4cee-8587-b7324a1a0028" Version="0.1.2.0" Language="en-US" Publisher="Starflash Studios" />
<Identity Id="QMediaVSIX.51b65c0a-fbc0-4cee-8587-b7324a1a0028" Version="0.1.3.0" Language="en-US" Publisher="Starflash Studios" />
<DisplayName>QMediaVSIX</DisplayName>
<Description xml:space="preserve">Quickly play, pause and skip your currently playing media. </Description>
<MoreInfo>https://github.com/starflash-studios/QMediaVSIX</MoreInfo>
Expand Down

0 comments on commit 7ac44a6

Please sign in to comment.