Created
August 9, 2011 13:34
-
-
Save AzureKitsune/1134051 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import strutils | |
# CONST | |
const | |
loc = "<locale.h>" | |
winnls = "Winnls.h" | |
stdlib = "<stdlib.h>" | |
# FUNCTIONS RELATED TO GETTING LOCALE NAME | |
when defined(windows): | |
# The following aren't defined for windows. | |
type | |
Tlocv{.final, header: loc, importc: "struct lconv".} = object | |
decimal_point*: cstring | |
thousands_sep*: cstring | |
grouping*: cstring | |
int_curr_symbol* : cstring | |
currency_symbol : cstring | |
mon_decimal_point : cstring | |
mon_thousands_sep : cstring | |
mon_grouping : cstring | |
positive_sign* : cstring | |
negative_sign* : cstring | |
int_frac_digits : char | |
frac_digits : char | |
p_cs_precedes : char | |
p_sep_by_space : char | |
n_cs_precedes : char | |
n_sep_by_space : char | |
p_sign_posn* : char | |
n_sign_posn : char | |
int_p_cs_precedes : char | |
int_n_cs_precedes : char | |
int_p_sep_by_space : char | |
int_n_sep_by_space : char | |
int_p_sign_posn : char | |
int_n_sign_posn : char | |
Plocv = ptr TLocv | |
# WRAPPERS AROUND C FUNCTIONS FOR SETTING APPLICATION LOCALE. | |
var LC_ALL {.header: loc, importc: "LC_ALL".}: int | |
proc localeconv(): Plocv {.header: loc, importc: "localeconv".} | |
proc setLocale(category: int, other: cstring): cstring {.header: loc, importc: "setlocale".} | |
proc setLocale(other: cstring): cstring = | |
result = setLocale(LC_ALL, other) | |
proc getLocale(): string = | |
result = $(setLocale(LC_ALL, nil)) | |
import windows | |
type | |
TLocaleName = enum #Numbers are based on hex codes for their respective language in the WinAPI. See MSDN -> http://msdn.microsoft.com/en-us/library/dd318693(v=VS.85).aspx | |
Chinese = 4, | |
German = 7, | |
English = 9, | |
Spanish = 10, | |
Japanese = 11, | |
French = 12, | |
Italian = 16, | |
Polish = 21 | |
proc GetLocaleFromLangID(): TLocaleName = | |
var id = GetUserDefaultLangID() | |
var lang = (id and 0x00FF) | |
return TLocaleName(lang) | |
else: | |
import postix | |
#TODO for *nix. MUST be named GetLocale and return name of the language. | |
proc getenv*(name: cstring): cstring {.header: stdlib, importc: "getenv".} | |
proc GetLocaleFromEnv(): string = | |
var LANG = getenv("LANG") | |
if lang is nil: | |
# Language not set. | |
return "Unknown" | |
else: | |
var langstr = $LANG | |
var langname = split(langstr, '.')[0] | |
case langname | |
of "en": return "English" | |
of "fr": return "French" | |
of "de": return "German" | |
of "io": return "Ido" | |
of "it": return "Italian" | |
of "ja": return "Japanese" | |
of "pt": return "Portuguese" | |
of "ru": return "Russian" | |
of "es": return "Spanish" | |
of "sv": return "Swedish" | |
of "pl": return "Polish" | |
else: return "Unknown: " & langname | |
type | |
TLocaleManager* = object #Future object used for localization via xml. | |
proc GetLocaleName*(locale: var TLocaleManager): string = | |
when defined(windows): | |
return $GetLocaleFromLangID() | |
elif defined(postix): | |
#TODO for *nix | |
return GetLocaleFromEnv() | |
else: #TODO | |
var | |
LocaleManager: TLocaleManager | |
# DEBUGING TEST | |
when isMainModule: | |
var locv = localeconv() | |
echo(LocaleManager.GetLocaleName()) #Prints English | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment