Combined all F# code into one project

Less to migrate, less to maintain, and I'll never swap these out as
components; might as well get the ease of managing them all in one
project.
This commit is contained in:
Daniel J. Summers
2016-12-04 22:05:50 -06:00
parent f5b65d320e
commit 33dccf5822
43 changed files with 249 additions and 328 deletions

View File

@@ -0,0 +1,42 @@
module MyWebLog.Resources.Strings
open MyWebLog
open Newtonsoft.Json
open System.Collections.Generic
open System.Reflection
/// The locales we'll try to load
let private supportedLocales = [ "en-US" ]
/// The fallback locale, if a key is not found in a non-default locale
let private fallbackLocale = "en-US"
/// Get an embedded JSON file as a string
let private getEmbedded locale =
use rdr =
new System.IO.StreamReader
(typeof<AppConfig>.GetTypeInfo().Assembly.GetManifestResourceStream(sprintf "MyWebLog.App.%s.json" locale))
rdr.ReadToEnd()
/// The dictionary of localized strings
let private strings =
supportedLocales
|> List.map (fun loc -> loc, JsonConvert.DeserializeObject<Dictionary<string, string>>(getEmbedded loc))
|> dict
/// Get a key from the resources file for the given locale
let getForLocale locale key =
let getString thisLocale =
match strings.ContainsKey thisLocale with
| true -> match strings.[thisLocale].ContainsKey key with
| true -> Some strings.[thisLocale].[key]
| _ -> None
| _ -> None
match getString locale with
| Some xlat -> Some xlat
| _ when locale <> fallbackLocale -> getString fallbackLocale
| _ -> None
|> function Some xlat -> xlat | _ -> sprintf "%s.%s" locale key
/// Translate the key for the current locale
let get key = getForLocale System.Globalization.CultureInfo.CurrentCulture.Name key