Re: Localized Internals of AppleScript (date Class for example)
Re: Localized Internals of AppleScript (date Class for example)
- Subject: Re: Localized Internals of AppleScript (date Class for example)
- From: Brian <email@hidden>
- Date: Sun, 17 Apr 2005 01:52:55 +0200
Hello all,
Le 15 avr. 05, à 19:09, Paul Berkowitz a écrit :
What would be useful would be some sort of 'as local text' coercion:
weekday of someDate
--> Thursday
weekday of someDate as local text
--> "jeudi"
Thursday as local text
--> "jeudi"
Totally agree with you Paul, that would be the better solution.
In waiting for that, I made an ugly script that try to translate constant in the 'local language'.
It works with several languages / custom settings, but not with the ones that requires an Unicode application...
property monthList : {{January, ""}, {February, ""}, {March, ""}, {April, ""}, {May, ""}, {June, ""}, {July, ""}, {August, ""}, {September, ""}, {October, ""}, {November, ""}, {December, ""}}
property weekdayList : {{Monday, ""}, {Tuesday, ""}, {Wednesday, ""}, {Thursday, ""}, {Friday, ""}, {Saturday, ""}, {Sunday, ""}}
-- set the properties monthList and weekDayList with the translated constant
-- Unfortunately, this not working with some country/languages
on initTranslation()
-- Making some dates for comparison
set aDate to (current date)
-- Set aDate to April 28, 1980 (a Monday, and also my birthday)
set year of aDate to 1980
set month of aDate to April
set day of aDate to 28
-- Set bDate to April 29, 1980
set bDate to aDate + 86400
-- Set cDate to April 28, 2008 (a Monday too)
set cDate to (bDate - 86400)
set year of cDate to 2008
-- Convert those dates to string, in 'local settings' style
set aString to date string of aDate
set bString to date string of bDate
set cString to date string of cDate
-- Get the position of the weekday and month name in the local style string
-- In fact, it get the place of everythings (year, day of month ect...)
-- but, maybe I'm wrong, there is only the month name and weekday to translate
-- because this not work with Unicode settings
set x to count word of (date string of aDate)
repeat with i from 1 to x
if (word i of aString = "1980") or (word i of aString = "80") then
-- set yearPos to i
else if word i of aString = "28" then
-- set dayPos to i
else if ((word i of aString) = (word i of cString)) and ((word i of aString) is not (word i of bString)) then
set weekdayPos to i
else if ((word i of aString) = (word i of bString)) and ((word i of aString) = (word i of cString)) then
try
set y to word i of aString as number
-- set monthPos to i
on error
set monthNamePos to i
end try
end if
end repeat
-- Get the translation for the 'days of the week'
set day of aDate to 27
repeat with i from 1 to 7
set item 2 of item i of weekdayList to word weekdayPos of (date string of (aDate + i * 86400))
end repeat
-- Get the translation for the month's name
set aDate to (current date)
set year of aDate to 1980
set month of aDate to January
set day of aDate to 1
repeat with i from 1 to 12
set month of aDate to item 1 of item i of monthList
set item 2 of item i of monthList to word monthNamePos of (date string of aDate)
end repeat
end initTranslation
on stuff)
-- Check if the translation table exists, else, make it
if (item 2 of item 1 of weekdayList) is "" then
initTranslation()
end if
-- It's just to get what you want to be translated without having to set a second parameter
-- like translateStuff(stuff,DayOrMonth), it's that or some try/on error things...
set constantWeekdayList to {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}
set constantMonthList to {January, February, March, April, May, June, July, August, September, October, November, December}
-- If stuff is a weekday
if stuff is in constantWeekdayList then
repeat with i from 1 to 7
if item 1 of item i of weekdayList is stuff then
return item 2 of item i of weekdayList
end if
end repeat
-- If stuff is a month name
else if stuff is in constantMonthList then
repeat with i from 1 to 12
if item 1 of item i of monthList is stuff then
return item 2 of item i of monthList
end if
end repeat
end if
-- If stuff is from E.T. calendar
return "Can't translate that, sorry..."
end translateDate
Remember : I say ugly... but it works (here...).
It takes a month or a weekday constant as parameter and return the local translation as a string.
Just change your settings in the "International" preference panel to play with it. You will have to call the
initTranslation() command after you change settings, because the translations are in properties.
Because of it's length, I was searching a way to call it from another script and I only found that :
-- The path to the folder where you put 'DateTranslator.scpt', don't forget the last ':'
set pathToFolder to "Documents:AppleScript:Translators:"
-- This is just a way for loading the functions, you can put the handlers directly in your scripts
-- so you can access to those stuff with a simple call to the function 'translateDate(Month or weekday)'
set DateTranslator to (load script file (pathToFolder & "DateTranslator.scpt"))
tell DateTranslator
-- Uncomment next line if you want to play with settings
-- initTranlation()
set MyDate to (current date)
set aTest to (((weekday of MyDate) as string) & " -> " & weekday of MyDate) ¬
& return & (month of MyDate) as string) & " -> " & month of MyDate)
end tell
Is there a way to do an "include" command in AppleScript ?
Hope that help, and sorry for the long mail,
Brian
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden