From 18fea5478faa92f1f58cfc14de97098c7560b990 Mon Sep 17 00:00:00 2001 From: Ykkrosh Date: Wed, 1 Sep 2004 19:48:03 +0000 Subject: [PATCH] Updated i18n code This was SVN commit r1094. --- source/i18n/BufferVariable.cpp | 10 +- source/i18n/CLocale.h | 2 +- source/i18n/DataTypes.h | 10 +- source/i18n/Interface.cpp | 4 +- source/i18n/Interface.h | 3 +- source/i18n/ScriptInterface.cpp | 234 ++++++++++++++++++++++++-------- source/i18n/ScriptInterface.h | 2 +- source/i18n/StringBuffer.h | 2 +- source/ps/i18n.cpp | 4 +- 9 files changed, 203 insertions(+), 68 deletions(-) diff --git a/source/i18n/BufferVariable.cpp b/source/i18n/BufferVariable.cpp index 10d05b9a30..236a60391d 100755 --- a/source/i18n/BufferVariable.cpp +++ b/source/i18n/BufferVariable.cpp @@ -16,9 +16,13 @@ namespace I18n { template<> BufferVariable* NewBufferVariable(int v) { return new BufferVariable_int(v); } template<> BufferVariable* NewBufferVariable(double v) { return new BufferVariable_double(v); } - template<> BufferVariable* NewBufferVariable(const wchar_t* v) { return new BufferVariable_string(v); } - template<> BufferVariable* NewBufferVariable(const char* v) { return new BufferVariable_string(v); } - template<> BufferVariable* NewBufferVariable(I18n::Name v) { return new BufferVariable_rawstring(v.value); } + template<> BufferVariable* NewBufferVariable(Noun v) { return new BufferVariable_string(v.value); } + template<> BufferVariable* NewBufferVariable(Name v) { return new BufferVariable_rawstring(v.value); } + + // + // Don't allow plain strings -- people *must* specify whether it's going + // to be automatically translated (I18n::Noun) or not (I18n::Name/Raw) + // } StrImW BufferVariable_int::ToString(CLocale*) diff --git a/source/i18n/CLocale.h b/source/i18n/CLocale.h index a1124bdee5..a5858b9000 100755 --- a/source/i18n/CLocale.h +++ b/source/i18n/CLocale.h @@ -78,7 +78,7 @@ namespace I18n #ifdef _MSC_VER # pragma warning (disable: 4355) #endif - CLocale(JSContext* context) : Script(this, context), CacheAge(0) {} + CLocale(JSContext* context, JSObject* scope) : Script(this, context, scope), CacheAge(0) {} #ifdef _MSC_VER # pragma warning (default: 4355) #endif diff --git a/source/i18n/DataTypes.h b/source/i18n/DataTypes.h index 44930f45dd..6f5d88dceb 100755 --- a/source/i18n/DataTypes.h +++ b/source/i18n/DataTypes.h @@ -5,8 +5,16 @@ namespace I18n { + // Use for names of objects that should be translated, e.g. + // translate("Construct $obj")< Noun(T d) : value(d) {} + StrImW value; + }; + // Allow translate("Hello $you")< "Name", value => argv[0] } */ \ + \ + JSObject* object = JS_NewObject(cx, NULL, NULL, obj); \ + JS_ASSERT(object, "Failed to create i18n value object"); \ + \ + /* TODO: More error checking */ \ + jsval type = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, #x)); \ + jsval value = STRING_TO_JSVAL(JS_ValueToString(cx, argv[0])); \ + JS_SetProperty(cx, object, "type", &type); \ + JS_SetProperty(cx, object, "value", &value); \ + \ + *rval = OBJECT_TO_JSVAL(object); \ + \ + return JS_TRUE; \ } + TYPE(Name) + TYPE(Raw) + TYPE(Noun) + #undef TYPE + + static JSClass JSI_class = { + "JSI_i18n", JSCLASS_HAS_PRIVATE, + JS_PropertyStub, JS_PropertyStub, + JS_PropertyStub, JS_PropertyStub, + JS_EnumerateStub, JS_ResolveStub, + JS_ConvertStub, JS_FinalizeStub, + NULL, NULL, NULL, NULL + }; + + #define TYPE(x) {#x, Create_##x, 1, 0, 0} + static JSFunctionSpec JSI_funcs[] = { + TYPE(Name), + TYPE(Raw), + TYPE(Noun), + {0,0,0,0,0}, + }; + #undef TYPE +} + + +static JSBool JSFunc_LookupWord(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) +{ + JS_ASSERT(argc == 2, "Incorrect number of parameters to lookup(dictionary, word) function"); // Get the strings JSString* dictname = JS_ValueToString(cx, argv[0]); JSString* word = JS_ValueToString(cx, argv[1]); - if (!dictname || !word) - { - JS_ReportError(cx, "lookup() failed to convert parameters to strings"); - return JS_FALSE; - } + JS_ASSERT(dictname && word, "lookup() failed to convert parameters to strings"); + // and the characters from the strings jschar* dictname_chars = JS_GetStringChars(dictname); jschar* word_chars = JS_GetStringChars(word); - if (!dictname_chars || !word_chars) - { - JS_ReportError(cx, "lookup() failed to get parameter string data"); - return JS_FALSE; - } + // (can't fail) + // and convert those characters into to wstrings Str dictname_str, word_str; StringConvert::jschars_to_wstring(dictname_chars, JS_GetStringLength(dictname), dictname_str); @@ -117,12 +143,9 @@ JSBool JSFunc_LookupWord(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, } // Create an object to be returned, containing enough data to access properties of the found word - JSObject* wordobj = JS_NewObject(cx, &LookedupWord::JSI_class, NULL, obj); - if (!wordobj) - { - JS_ReportError(cx, "lookup() failed to create object"); - return JS_FALSE; - } + JSObject* wordobj = JS_NewObject(cx, &JSI_LookedupWord::JSI_class, NULL, obj); + JS_ASSERT(wordobj, "lookup() failed to create object"); + // Associate the looked-up word data with the JS object JS_SetPrivate(cx, wordobj, (void*)lookedup); @@ -130,13 +153,94 @@ JSBool JSFunc_LookupWord(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, return JS_TRUE; } -static JSFunctionSpec JSFunc_list[] = { +static JSBool JSFunc_Translate(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) +{ + JS_ASSERT(argc >= 1, "Too few parameters to translate() function"); + + JSString* phrase_str = JS_ValueToString(cx, argv[0]); + JS_ASSERT(phrase_str, "translate() failed to convert first parameter to string"); + + CStrW phrase (JS_GetStringChars(phrase_str)); + + jsval locale_objval; + JS_ASSERT(JS_GetProperty(cx, obj, "i18n", &locale_objval), "translate() failed to find i18n object in current scope"); + JSObject* locale_obj = JSVAL_TO_OBJECT(locale_objval); + CLocale* locale = (CLocale*)JS_GetPrivate(cx, JSVAL_TO_OBJECT(locale_objval)); + + StringBuffer sb = locale->Translate(phrase.c_str()); + for (uintN i=1; i