Thursday, August 11, 2022

#1 2012-08-14 02:11:10 am

alastor933
Member
From:: Utrecht, NL
Registered: 2008-09-12
Posts: 611

Localised weekday?

Just out of curiosity: are localised weekday names available somewhere in the system?
I was modifying a datestamping routine to include those, when that came up as a possible way of getting them.
What I did use was simple enough:

Applescript:

-- full day name
set localisedWeekDay to word 1 of ((current date) as text)
-- or abbreviated
set localisedWeekDay to (text items 1 thru 2 of ((current date) as text)) as text
-- memo to self: must respect TIDs here, or face tuttuttutting from peers

Another way would be a lookup table with day numbers and names (which that system file is, conceptually).

Offline

 

#2 2012-08-14 03:38:02 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2809
Website

Re: Localised weekday?

It is defined in the the nl_NL.utf-8 directory in /usr/share/locale. The file LC_TIME is the file with the weekday names in it. I'm assuming that your system locale is set to Dutch.

Last edited by DJ Bazzie Wazzie (2012-08-14 03:41:41 am)

Offline

 

#3 2012-08-14 05:37:11 am

mouramartins
Member
From:: Algarve
Registered: 2010-11-21
Posts: 156

Re: Localised weekday?

Bad news if you expected it to be a bit general:

Applescript:


current date
-->    date "14 de Agosto de 2012 12:17:11"

date string of (current date)

--> "terça-feira, 14 de agosto de 2012"

http://www.kareprints.com/?p=652

Offline

 

#4 2012-08-14 06:49:40 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2809
Website

Re: Localised weekday?

Something like this?

Correct modified code below. Aware that link opens in new window/tab, you can scroll to post #12.

Last edited by DJ Bazzie Wazzie (2012-08-14 02:20:59 pm)

Offline

 

#5 2012-08-14 09:17:23 am

alastor933
Member
From:: Utrecht, NL
Registered: 2008-09-12
Posts: 611

Re: Localised weekday?

mouramartins wrote:

Applescript:

--> "terça-feira, 14 de agosto de 2012"


Oh dear.. No, I wasn't aiming for locale-agnostic code. Thanks for reminding me..
[Might have found out anyway - I'm signed up for a course in Portuguese, starting in a few weeks. I'm confident I'll know how to say 'next tuesday' pretty soon wink ]

Offline

 

#6 2012-08-14 10:10:47 am

alastor933
Member
From:: Utrecht, NL
Registered: 2008-09-12
Posts: 611

Re: Localised weekday?

DJ Bazzie Wazzie wrote:

It is defined in the the nl_NL.utf-8 directory in /usr/share/locale. The file LC_TIME is the file with the weekday names in it.


Mmyes. Not exactly a lookup table. Just a list of strings; I don't think I would want to rely on their order.

I think your handler should be used like so:

Applescript:

timeLocalStrings((weekday of (current date) as text), "en_US", "nl_NL")

In a way, this uses the two LC_TIME files as one lookup table.
It should work with abbreviated names too, presumably. I don't think you can get those with AppleScript, so for that you'd need a lookup table in the script.
Maybe hard-code the en_US file, and loose the 2nd parameter, to avoid mistakes.
Thank you, gentlemen.

Last edited by alastor933 (2012-08-14 10:17:04 am)

Offline

 

#7 2012-08-14 10:17:37 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2809
Website

Re: Localised weekday?

You're correct that you can't depend on the order without a base. Every file has the same order so when you lookup the line in one file you can print the line in another file. LC_TIME files work this way because they are faster and saves space compared to localization strings, but the principle remains the same.

Offline

 

#8 2012-08-14 11:16:10 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4622

Re: Localised weekday?

Hello

I'm using 10.8.
When I run the posted script with the calling instruction :

timeLocalStrings((weekday of (current date) as text), "en_US", "nl_NL")

I get :

…
sl_SI
sr_YU
sv_SE
tr_TR
uk_UA
zh_CN
zh_HK
zh_TW"
    do shell script "cat '/usr/share/locale/en_US.UTF-8/LC_TIME' | sed -n '/Tuesday/ ='"
        --> "34"
Résultat :
error "Il est impossible de régler ln of 34 à \"/usr/share/locale/nl_NL.UTF-8/LC_TIME\"." number -10006 from «class LN  » of 34

Am I making something wrongly ?

Yvan KOENIG (VALLAURIS, France) mardi 14 août 2012 19:15:33

Offline

 

#9 2012-08-14 12:35:49 pm

alastor933
Member
From:: Utrecht, NL
Registered: 2008-09-12
Posts: 611

Re: Localised weekday?

Yvan, no. It seems we are both using an osax DJ does not have. Change his ln variable, and she'll work. Satimage?

DJ, just for fun I tried the script with an abbreviated day name. And learned something about sed - I think.
With an abbreviated name you get 2 line numbers, so it seems to apply a 'contains' condition, not "is equal to".
I just looked at the sed manpage for the 1st time ever, which didn't me much good.

Last edited by alastor933 (2012-08-14 12:51:51 pm)

Offline

 

#10 2012-08-14 12:43:15 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4622

Re: Localised weekday?

Thanks

You are right. Satimage is installed on my machine.
I use it from time to time to test script which I am asked to debug.

I'm wondering why I missed that because, the script editor display ln as other functions, not as a variable name.

Yvan KOENIG (VALLAURIS, France) mardi 14 août 2012 20:43:05

Offline

 

#11 2012-08-14 01:19:15 pm

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Localised weekday?

Is there any other way to get the current locale (vanilla AS) , than using the code below?

And this isn't safe on Tiger at least, as I think I remember?

Applescript:

set theLocale to (do shell script " echo $LC_ALL" )

alastor933 wrote:

With an abbreviated name you get 2 line numbers, so it seems to apply a 'contains' condition, not "is equal to".
I just looked at the sed manpage for the 1st time ever, which didn't me much good.


In this case when an abbrev is sought for you can surround it by ^ and $, then you won't get more, as sed is greedy…

Last edited by McUsr (2012-08-14 01:27:44 pm)


Mercurial vcs is a joy to use for scripting.


Filed under: locale

Offline

 

#12 2012-08-14 02:14:32 pm

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2809
Website

Re: Localised weekday?

Regulus and McUsr are both right. Some entries are multiple times in the same file. So we need to tell sed also to quit on first result. Also it should work with satimage users which, correctly mentioned, I don't have installed.

Applescript:


timeLocalStrings("ma", "nl_NL", "en_US")

on timeLocalStrings(str, _from, _to)
   set validLocales to every paragraph of (do shell script "ls /usr/share/locale | grep -i '.utf-8$' | awk -F. '{print $1}'")
   if _from is not in validLocales or _to is not in validLocales then return str
   set fPath to "/usr/share/locale/" & _from & ".UTF-8/LC_TIME"
   set tPath to "/usr/share/locale/" & _to & ".UTF-8/LC_TIME"
   set lineNumber to (do shell script "cat " & quoted form of fPath & " | sed -n '/^" & str & "$/ {=;q;}'") as integer
   if lineNumber = 0 then return str
   return do shell script "cat " & quoted form of tPath & " | sed -n -e '" & lineNumber & "," & lineNumber & "p'"
end timeLocalStrings

Last edited by DJ Bazzie Wazzie (2012-08-14 02:21:57 pm)

Offline

 

#13 2012-08-14 07:10:17 pm

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Localised weekday?

Hello!

Not that it matters much, but awk is notoriously slow, and you are really shooting a sparrow with a cannon when return a field like that with awk. I first tried sed, and that was as hard it seemed, as to change newlines! smile Then I remembered cut!

I am really fond of awk too. And there is nothing wrong with your code, just an alternate, and possibley much faster way to get that field returned. smile I think it is generally a better way way to return just the first field of a list of a record separated by some delimiter, (period in this case), when no processing of data is needed!

[code]cut -f 1 -d \.[/code]

Last edited by McUsr (2012-08-14 07:52:30 pm)


Mercurial vcs is a joy to use for scripting.


Filed under: locale

Offline

 

#14 2012-08-15 02:59:07 am

alastor933
Member
From:: Utrecht, NL
Registered: 2008-09-12
Posts: 611

Re: Localised weekday?

McUsr wrote:

Is there any other way to get the current locale (vanilla AS) , than using the code below?

Applescript:

set theLocale to (do shell script " echo $LC_ALL" )


No 100% vanilla, unless you want to write a plist parser in plain A/S.
It's defaults or System Events otherwise.
And:
I can't find that file on my 10.6.8 system. It's a system file anyway (right?), to get the current user's language/locale you'd read one of his plists:

Applescript:

do shell script "defaults read -g AppleLanguages"
-- or
do shell script "defaults read -g AppleLocale"

Offline

 

#15 2012-08-15 03:28:11 am

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Localised weekday?

alastor933 wrote:

No 100% vanilla, unless you want to write a plist parser in plain A/S.


Yes, System Events is really as vanilla as AsObjC Runner, but you'd expect it to be there! smile


Mercurial vcs is a joy to use for scripting.


Filed under: Vanilla

Offline

 

#16 2012-08-15 03:29:58 am

alastor933
Member
From:: Utrecht, NL
Registered: 2008-09-12
Posts: 611

Re: Localised weekday?

DJ Bazzie Wazzie wrote:

Some entries are multiple times in the same file. So we need to tell sed also to quit on first result.


From my first run of the script I guessed that sed does a "contains" match, not "equal to". In that case, quitting at the 1st match for "Tue" would return "Tuesday" when the order in the LC_TIME is reversed (short names after long names).
So, how does sed match? "equal to" or "contains"?
I can't find that out from its manpage. It may be there, but hidden by the terminology.

Offline

 

#17 2012-08-15 03:46:11 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2809
Website

Re: Localised weekday?

McUsr wrote:

Not that it matters much, but awk is notoriously slow, and you are really shooting a sparrow with a cannon when return a field like that with awk. I first tried sed, and that was as hard it seemed, as to change newlines! smile Then I remembered cut!


Slow? LOL

AWK is written to outperform the shell big time and succeeded. Weird is that I would suspect that cut is indeed faster but seems not to be true on every machine. On my machine there is no difference, some machines cut is faster and others awk is faster. AWK is pretty awesome and insanely fast for an interpreter.

These are my results:

time var=$(ls /usr/share/locale | cut -f1 -d \.);

real    0m0.009s
user    0m0.006s
sys    0m0.005s
time var=$(ls /usr/share/locale | awk -F. '{print $1}');

real    0m0.009s
user    0m0.006s
sys    0m0.005s

Last edited by DJ Bazzie Wazzie (2012-08-15 03:46:55 am)

Offline

 

#18 2012-08-15 04:00:25 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2809
Website

Re: Localised weekday?

alastor933 wrote:
DJ Bazzie Wazzie wrote:

Some entries are multiple times in the same file. So we need to tell sed also to quit on first result.


From my first run of the script I guessed that sed does a "contains" match, not "equal to". In that case, quitting at the 1st match for "Tue" would return "Tuesday" when the order in the LC_TIME is reversed (short names after long names).
So, how does sed match? "equal to" or "contains"?
I can't find that out from its manpage. It may be there, but hidden by the terminology.


It is now an exact match.

for instance when you lookup 'Tue' you would normally have an contain match. But when you wrap and ^...$ around it (begins with and ends with) it is an exact match.

Offline

 

#19 2012-08-15 06:50:01 am

alastor933
Member
From:: Utrecht, NL
Registered: 2008-09-12
Posts: 611

Re: Localised weekday?

DJ Bazzie Wazzie wrote:

...when you wrap and ^...$ around it (begins with and ends with) it is an exact match.


Good! Dank je.

Offline

 

#20 2012-08-15 10:10:22 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4622

Re: Localised weekday?

Hello

Here is a version using ASObjC Runner.

Applescript:


--weekday of (current date) as text
"Wed"
timeLocalStrings(result, "en_US", "fr_FR")

on timeLocalStrings(str, _from, _to)
   set vName to path to startup disk as text
   set fPath to vName & "usr:share:locale:" & _from & ".UTF-8:LC_TIME"
   set tPath to vName & ":usr:share:locale:" & _to & ".UTF-8:LC_TIME"
   tell application "System Events"
       {exists disk item fPath, exists disk item tPath}
   end tell
   if result contains false then return str
   paragraphs of (read file fPath)
   tell application "ASObjC Runner"
       set maybe to look in list result matching str
   end tell
   if maybe = {} then return str
   item 1 of maybe
   paragraph result of (read file tPath)
   return result
end timeLocalStrings

Yvan KOENIG (VALLAURIS, France) mercredi 15 août 2012 18:10:15

Offline

 

#21 2012-08-15 01:03:14 pm

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Localised weekday?

DJ Bazzie Wazzie wrote:

AWK is pretty awesome and insanely fast for an interpreter.


I have not timed it towards perl, and maybe it does as good as cut, when it is just one line of input.

I have no doubt, that cut will outperform awk, on any OSX from Tiger onwards if say it is over 100 lines of text that is to be cut.

Most of the time, I use sed when I can, to overcome the slowness of awk. That is, when I don't need a  such a big script language to process input. Sed is also an interpreter, though much smaller, and faster.

How perl perfoms with regards to awk, would be interesting to see. My initial guess would be that it is faster, but I have no knowledge on the matter.


Mercurial vcs is a joy to use for scripting.


Filed under: awk

Offline

 

#22 2012-08-15 01:35:43 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4622

Re: Localised weekday?

Hello

For the fun, I compared three handlers executing them 1000 times.

with handler #1 :

Applescript:


set beg to current date
repeat 1000 times
   --weekday of (current date) as text
   "Wed"
   timeLocalStrings(result, "en_US", "fr_FR")
end repeat
(current date) - beg
--> 460

on timeLocalStrings(str, _from, _to)
   tell application "System Events"
       set validLocales to name of every folder of folder "Macintosh HD:usr:share:locale:" whose name contains ".UTF-8"
   end tell
   {validLocales contains _from & ".UTF-8", validLocales contains _to & ".UTF-8"}
   
   if result contains false then return str
   set vName to path to startup disk as text
   set fPath to vName & "usr:share:locale:" & _from & ".UTF-8:LC_TIME"
   set tPath to vName & ":usr:share:locale:" & _to & ".UTF-8:LC_TIME"
   paragraphs of (read file fPath)
   tell application "ASObjC Runner"
       set maybe to look in list result matching str
   end tell
   if maybe = {} then return str
   item 1 of maybe
   paragraph result of (read file tPath)
   return result
end timeLocalStrings

with handler #2 (DJ Bazzie Wazzie one)

Applescript:



set beg to current date
repeat 1000 times
   --weekday of (current date) as text
   "Wed"
   timeLocalStrings(result, "en_US", "fr_FR")
end repeat
(current date) - beg
--> 37

on timeLocalStrings(str, _from, _to)
   set validLocales to every paragraph of (do shell script "ls /usr/share/locale | grep -i '.utf-8$' | awk -F. '{print $1}'")
   if _from is not in validLocales or _to is not in validLocales then return str
   set fPath to "/usr/share/locale/" & _from & ".UTF-8/LC_TIME"
   set tPath to "/usr/share/locale/" & _to & ".UTF-8/LC_TIME"
   set lineNumber to (do shell script "cat " & quoted form of fPath & " | sed -n '/^" & str & "$/ {=;q;}'") as integer
   if lineNumber = 0 then return str
   return do shell script "cat " & quoted form of tPath & " | sed -n -e '" & lineNumber & "," & lineNumber & "p'"
end timeLocalStrings

with handler #3

Applescript:



set beg to current date
repeat 1000 times
   --weekday of (current date) as text
   "Wed"
   timeLocalStrings(result, "en_US", "fr_FR")
end repeat
(current date) - beg
--> 9

on timeLocalStrings(str, _from, _to)
   set vName to path to startup disk as text
   set fPath to vName & "usr:share:locale:" & _from & ".UTF-8:LC_TIME"
   set tPath to vName & ":usr:share:locale:" & _to & ".UTF-8:LC_TIME"
   tell application "System Events"
       {exists disk item fPath, exists disk item tPath}
   end tell
   if result contains false then return str
   paragraphs of (read file fPath)
   tell application "ASObjC Runner"
       set maybe to look in list result matching str
   end tell
   if maybe = {} then return str
   item 1 of maybe
   paragraph result of (read file tPath)
   return result
end timeLocalStrings

You read well

handler #1 --> 460 seconds
handler #2 --> 37 seconds
handler #1 --> 9 seconds

Thank you Shane Stanley.

Yvan KOENIG (VALLAURIS, France) mercredi 15 août 2012 21:35:36

Offline

 

#23 2012-08-15 02:00:11 pm

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2809
Website

Re: Localised weekday?

McUsr wrote:
DJ Bazzie Wazzie wrote:

AWK is pretty awesome and insanely fast for an interpreter.


I have not timed it towards perl, and maybe it does as good as cut, when it is just one line of input.

I have no doubt, that cut will outperform awk, on any OSX from Tiger onwards if say it is over 100 lines of text that is to be cut.

Most of the time, I use sed when I can, to overcome the slowness of awk. That is, when I don't need a  such a big script language to process input. Sed is also an interpreter, though much smaller, and faster.

How perl perfoms with regards to awk, would be interesting to see. My initial guess would be that it is faster, but I have no knowledge on the matter.


First of all there are different awks, teh byte code awk is fastest but not implemented on OS X. Unbelievable but byte code version of AWK is faster than compiled code, I'm still amazed about that. But unfortunately not distributed with Mac OS X. No we have to work with the 'one and only true' AWK (designed by Aho, Weinberger and Kernighan), because Kernighan was also the designer of C we don't have to worry if the C code is properly written tongue.

No sed is a good tool but remember that Sed was there first, AWK was designed to extend, or at least, to do things sed isn't able to. For instance AWK supports extended regular expressions, also you have C-style conditions and controls which sed also doesn't have. Also AWK has bult-in field separator which ignores surrounding white spaces which cut nor sed have.

Later when the limits of AWK came up Perl was designed to do things which can't be done with AWK like system call. Perl is also extensible which AWK and sed both aren't.

So performance-wise AWK should be the middle, perl the slowest and Sed the fastest between these three.

When to use which?

- sed for simple text processing
- awk for more complex processing
- perl for more complex processing and system calls are needed.

Offline

 

#24 2012-08-15 03:05:16 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5551

Re: Localised weekday?

Yvan Koenig wrote:

handler #1 --> 460 seconds
handler #2 --> 37 seconds
handler #1 --> 9 seconds


And then, of course, there's handler #4 ….  smile

Applescript:


set beg to current date
repeat 1000 times
   --weekday of (current date) as text
   "Wed"
   timeLocalStrings(result, "en_US", "fr_FR")
end repeat
(current date) - beg
--> 1

on timeLocalStrings(str, _from, _to)
   set fPath to "/usr/share/locale/" & _from & ".UTF-8/LC_TIME"
   set tPath to "/usr/share/locale/" & _to & ".UTF-8/LC_TIME"
   try
       set lookup1 to linefeed & (read fPath as «class utf8») & linefeed
       set astid to AppleScript's text item delimiters
       set AppleScript's text item delimiters to linefeed & str & linefeed
       if ((count lookup1's text items) is 1) then
           set AppleScript's text item delimiters to astid
           error
       end if
       set lineNumber to (count paragraphs of text item 1 of lookup1)
       if (lineNumber is 0) then set lineNumber to 1 -- Special-case the first line in the file.
       set AppleScript's text item delimiters to astid
       set outStr to paragraph lineNumber of (read tPath as «class utf8»)
   on error
       set outStr to str
   end try
   
   return outStr
end timeLocalStrings

Edit: Incorporated a fix by alastor933 for a problem he discovered some months later which occurs when the 'str' term is the first line in the '_from' file.

Last edited by Nigel Garvey (2013-02-26 12:14:02 pm)


NG

Offline

 

#25 2012-08-15 03:46:53 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4622

Re: Localised weekday?

Hello Nigel

with handler #5

Applescript:


set beg to current date
repeat 10000 times
   --weekday of (current date) as text
   "Wed"
   timeLocalStrings(result, "en_US", "fr_FR")
end repeat
(current date) - beg
--> 20 -- Edited

on timeLocalStrings(str, _from, _to)
   set fPath to "/usr/share/locale/" & _from & ".UTF-8/LC_TIME"
   set tPath to "/usr/share/locale/" & _to & ".UTF-8/LC_TIME"
   try
       paragraphs of (read fPath as «class utf8») -- Edited
       tell application "ASObjC Runner"
           set maybe to look in list result matching str
       end tell
       --if maybe = {} then return str
       item 1 of maybe
       paragraph result of (read tPath) -- Edited
       return result
   on error
       return str
   end try
end timeLocalStrings

I ran with handler #4 with 10 000 pass too and got 2 seconds

I think that yours is the best answer to the original question as the OP wished a plain Applescript one.

Yvan KOENIG (VALLAURIS, France) mercredi 15 août 2012 23:45:49

As Shane Stanley pointed, there was two extraneous words « file » in the original code.
After removing them, the 10 000 pass require 20 seconds ;-(

Last edited by Yvan Koenig (2012-08-16 03:48:10 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)