Skip to content

Commit

Permalink
Translate to other lang iff NPP is in that lang
Browse files Browse the repository at this point in the history
Ignore the Windows culture of the user to address #82.
  • Loading branch information
molsonkiko committed Nov 5, 2024
1 parent f259bf6 commit a40137e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

1. If a number string is too large or small for a 64-bit floating point number (for example, `-2e700`, `3.5e+450`), the JSON parser will now represent them as `-Infinity` (if they have a leading `-` sign) or `Infinity`, rather than representing them as `NaN`. The linter also has a new message for when this happens.
2. If a [JSON lines document](/docs/README.md#json-lines-documents) is not compliant with the strict JSON specification, the [status bar](/docs/README.md#error-form-and-status-bar) will now reflect that it is JSON lines.
3. Stop considering the user's Windows culture when determining the UI language for JsonTools (fix [issue 82](https://github.com/molsonkiko/JsonToolsNppPlugin/issues/82)). Now JsonTools will be translated into language X if and only if Notepad++ is in language X.

### Fixed

Expand Down
4 changes: 2 additions & 2 deletions JsonToolsNppPlugin/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@
// Build Number
// Revision
//
[assembly: AssemblyVersion("8.1.0.14")]
[assembly: AssemblyFileVersion("8.1.0.14")]
[assembly: AssemblyVersion("8.1.0.15")]
[assembly: AssemblyFileVersion("8.1.0.15")]
7 changes: 7 additions & 0 deletions JsonToolsNppPlugin/Utils/Translator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public static class Translator
public static string languageName { get; private set; } = DEFAULT_LANG;

public const string DEFAULT_LANG = "english";
/// <summary>
/// whether to use the Windows culture (see below) to determine translation language in some cases<br></br>
/// This is currently false because of issue 82 (https://github.com/molsonkiko/JsonToolsNppPlugin/issues/82)
/// </summary>
public const bool CHECK_WINDOWS_CULTURE = false;
public static readonly string translationDir = Path.Combine(Npp.pluginDllDirectory, "translation");

public static void LoadTranslations(bool atStartup = true, string preferredLang = null)
Expand Down Expand Up @@ -52,6 +57,7 @@ public static void LoadTranslations(bool atStartup = true, string preferredLang
//MessageBox.Show($"While attempting to determine native language preference from Notepad++ config XML, got an error:\r\n{ex}");
}
}
#if CHECK_WINDOWS_CULTURE
// as a fallback, try to determine the user's language by asking Windows for their current culture
if (languageName == DEFAULT_LANG || !TryGetTranslationFileName(languageName, out _))
{
Expand All @@ -61,6 +67,7 @@ public static void LoadTranslations(bool atStartup = true, string preferredLang
if (languageName == "Unknown")
languageName = currentCulture.Parent.EnglishName.Split(' ')[0].ToLower();
}
#endif
}
else
{
Expand Down
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,20 +142,16 @@ I expect you could find plenty of other good websites if you did some research.
If you are interested in helping users of JsonTools who don't speak English, JsonTools can be translated to other languages beginning in [v8.0](/CHANGELOG.md#800---2024-06-29).

JsonTools infers your preferred language and attempts to translate in the following way:
1. JsonTools checks your [Notepad++ `nativeLang.xml` config file](https://npp-user-manual.org/docs/binary-translation/#creating-or-editing-a-translation) (at [XPath path](https://www.w3schools.com/xml/xml_xpath.asp) `/NotepadPlus/Native-Langue/@name`) to determine what language you prefer to use, and sets `lowerEnglishName` to the appropriate value and __skips to step 3__. For example, if this file says `galician`, we will attempt to translate JsonTools to `galician`. __If the Notepad++ native language is `english` *or* if JsonTools does not have a translation file for the Notepad++ native language, JsonTools then does the following:__
1. JsonTools looks up your [`current culture`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-culture?view=powershell-7.4) (which can be checked using the `get-culture` command in Powershell).
2. Next, we find the `EnglishName` property of the `current culture` (which can be found by the `$foo = get-culture; $foo.EnglishName` command in Powershell), take the first word, and convert it to lowercase. Call this `lowerEnglishName`.
- Example: the `EnglishName` of the `en-us` culture is `English (United States)`, so `lowerEnglishName` is `english`
- Example: the `EnglishName` of the `it-it` culture is `Italian (Italy)`, so `lowerEnglishName` is `italian`
3. JsonTools then does one of the following:
1. JsonTools checks your [Notepad++ `nativeLang.xml` config file](https://npp-user-manual.org/docs/binary-translation/#creating-or-editing-a-translation) (at [XPath path](https://www.w3schools.com/xml/xml_xpath.asp) `/NotepadPlus/Native-Langue/@name`) to determine what language you prefer to use, and sets `lowerEnglishName` to the appropriate value. For example, if this file says `galician`, we will attempt to translate JsonTools to `galician`.
2. JsonTools then does one of the following:
- If `lowerEnglishName` is `english`, it does nothing (because JsonTools is naturally in English)
- Otherwise, it looks in the `translation` subdirectory of the `JsonTools` plugin folder (where `JsonTools.dll` lives) for a file named `{lowerEnglishName}.json5`
- __NOTE:__ because the translation files are in a subdirectory of the plugin folder, *translation does not work for versions of Notepad++ older than [version 8](https://notepad-plus-plus.org/downloads/v8/),* since those older versions do not have separate folders for each plugin.
4. If JsonTools found `translation\{lowerEnglishName}.json5`, it attempts to parse the file. If parsing fails, a message box will appear warning the user of this.
3. If JsonTools found `translation\{lowerEnglishName}.json5`, it attempts to parse the file. If parsing fails, a message box will appear warning the user of this.
- If no translation file was found, or if parsing failed, the default English will be used.
5. If parsing was successful, JsonTools will use the translation file as described below.
4. If parsing was successful, JsonTools will use the translation file as described below.

JsonTools only attempts to find translation files once, when Notepad++ is starting up. If you change the UI language of Notepad++ or your operating system's UI culture, you will have to close Notepad++ and reopen it before this change will apply to JsonTools.
JsonTools only attempts to find translation files once, when Notepad++ is starting up. If you change the UI language of Notepad++, you will have to close Notepad++ and reopen it before this change will apply to JsonTools.

To be clear, *JsonTools may not be in the same language of the Notepad++ UI.* The steps described above represent my best effort to automatically translate JsonTools into a language that the user will find useful, without requiring the user to select their language from a list of available languages in the settings form.

Expand Down

0 comments on commit a40137e

Please sign in to comment.