Skip to content
This repository has been archived by the owner on Apr 3, 2022. It is now read-only.

Commit

Permalink
Merge locale.d into common.d
Browse files Browse the repository at this point in the history
  • Loading branch information
spikespaz committed Jul 6, 2020
1 parent 284ab50 commit 2821f4e
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 119 deletions.
118 changes: 111 additions & 7 deletions source/common.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@ module common;

import core.sys.windows.windows: CommandLineToArgvW, GetCommandLineW, CreateProcessW, MessageBox,
MB_ICONERROR, MB_ICONWARNING, MB_YESNO, IDYES, MB_OK, HWND, DETACHED_PROCESS, CREATE_UNICODE_ENVIRONMENT, STARTUPINFO_W, PROCESS_INFORMATION;
import std.string: strip, splitLines, indexOf, indexOfAny, stripLeft, replace, endsWith;
import core.sys.windows.winnt: LCID, LANGID, LPWSTR, LPCWSTR, DWORD, MAKELCID, SORT_DEFAULT;
import core.sys.windows.winnls: GetLocaleInfoW;

import std.string: strip, split, splitLines, indexOf, indexOfAny, startsWith, stripLeft, replace, endsWith, toLower, fromStringz;
import std.windows.registry: Registry, RegistryException, Key, REGSAM;
import std.file: FileException, exists, readText, thisExePath;
import std.path: isValidFilename, buildPath, dirName;
import std.file: FileException, exists, readText, thisExePath, dirEntries, SpanMode;
import std.path: isValidFilename, buildPath, dirName, baseName;
import std.process: browse, ProcessException;
import std.net.curl: get, CurlException;
import std.typecons: Tuple, tuple;
import std.utf: toUTF16z, toUTFz;
import std.uri: encodeComponent;
import std.uri: encodeComponent, decodeComponent;
import std.algorithm: canFind;
import std.format: format;
import std.range: repeat;
import std.array: join;
import std.conv: to;

import locale: Translator;
import std.regex: matchFirst;

debug import std.stdio: writeln;
debug import std.string: format;

/// File name of the executable to download and run to install an update.
enum string SETUP_FILENAME = "SearchDeflector-Installer.exe";
Expand All @@ -38,6 +40,106 @@ enum string WIKI_URL = "https://github.com/spikespaz/search-deflector/wiki";
/// URL of the wiki's thank-you page.
enum string WIKI_THANKS_URL = WIKI_URL ~ "/Thanks-for-using-Search-Deflector!";

/// Missing Windows API exports
extern (Windows) {
/// https://docs.microsoft.com/en-us/windows/win32/api/winnls/nf-winnls-lcidtolocalename
int LCIDToLocaleName(LCID, LPWSTR, int, DWORD);
/// https://docs.microsoft.com/en-us/windows/win32/api/winnls/nf-winnls-localenametolcid
LCID LocaleNameToLCID(LPCWSTR lpName, DWORD dwFlags);
/// https://docs.microsoft.com/en-us/windows/win32/api/winnls/nf-winnls-getuserdefaultuilanguage
LANGID GetUserDefaultUILanguage();

/// https://docs.microsoft.com/en-us/windows/win32/intl/locale-names
/// https://www.magnumdb.com/search?q=LOCALE_NAME_MAX_LENGTH
enum uint LOCALE_NAME_MAX_LENGTH = 0x55;
/// https://docs.microsoft.com/en-us/windows/win32/intl/locale-allow-neutral-names
/// https://www.magnumdb.com/search?q=LOCALE_ALLOW_NEUTRAL_NAMES
enum uint LOCALE_ALLOW_NEUTRAL_NAMES = 0x08000000;
/// https://docs.microsoft.com/en-us/windows/win32/intl/locale-slocalized-constants
/// https://www.magnumdb.com/search?q=LOCALE_SLOCALIZEDDISPLAYNAME
enum uint LOCALE_SLOCALIZEDDISPLAYNAME = 0x00000002;
}


/// Static object for translation file loading
static struct Translator {
private static string[string] translationMap;
private static string[string] langFileMap;
private static string langKey;

static this() {
foreach (string fileName; dirEntries(buildPath(thisExePath().dirName(), "lang"), SpanMode.shallow)) {
debug writeln("Found locale file: " ~ fileName);
langFileMap[fileName.baseName().toLower()[0 .. $ - 4]] = fileName;
}
}

static string getUserDefaultLangKey() {
wchar[LOCALE_NAME_MAX_LENGTH] localeName;
LCIDToLocaleName(getDefaultLCID(), localeName.ptr, LOCALE_NAME_MAX_LENGTH, LOCALE_ALLOW_NEUTRAL_NAMES);
return localeName.ptr.fromStringz().to!string();
}

static uint getDefaultLCID() {
return MAKELCID(GetUserDefaultUILanguage(), SORT_DEFAULT);
}

static string getNameFromLangKey(const string langKey) {
const uint maxNameLen = 255;
wchar[maxNameLen] langName;

GetLocaleInfoW(
LocaleNameToLCID(langKey.toUTF16z(), LOCALE_ALLOW_NEUTRAL_NAMES),
LOCALE_SLOCALIZEDDISPLAYNAME,
langName.ptr,
maxNameLen
);

string langName0 = langName.ptr.fromStringz().to!string();
debug writeln("langName: " ~ langName0);
return langName0;
}

static string[] getLangKeys() {
return langFileMap.keys;
}

/// Load translations from file by specified langKey
static bool load(const string langKey) {
Translator.langKey = langKey;
return load();
}

/// Load the file corresponding to the current langKey
static bool load() {
bool success = true;
string filePath = langFileMap.get(langKey, null);

if (filePath is null) {
debug writeln("Requested user default locale not found!");
filePath = langFileMap["en-us"];
success = false;
}

translationMap = parseConfig(readText(filePath));
return success;
}

/// Load the user's default language, returns success
static bool loadDefault() {
return load(getUserDefaultLangKey());
}

/// Return the translation by key
static string text(const string key) {
debug writeln("Getting translation for key: " ~ key);

if (key !in translationMap)
debug writeln("Key not in translations: " ~ key);

return translationMap.get(key, "UNKNOWN STRING");
}
}

/// Struct representing the settings to use for deflection.
static struct DeflectorSettings {
Expand Down Expand Up @@ -349,6 +451,8 @@ string getBrowserArgs(const string browserPath, const bool useProfile, const str

/// Try to fetch the engine presets from the repository, if it fails, read from local.
string[string] getEnginePresets() {
import std.net.curl: get, CurlException; // Must use local import because of name conflict with associative arrays 'get' method

string[string] engines = parseConfig(readText(buildPath(thisExePath().dirName(), "engines.txt")));

try
Expand Down
112 changes: 0 additions & 112 deletions source/locale.d

This file was deleted.

0 comments on commit 2821f4e

Please sign in to comment.