10 Replies Latest reply on Nov 7, 2012 3:20 PM by DawMatt

    Localized conversion from string to number?

    johnrellis Most Valuable Participant

      Neither the SDK nor the core Lua library appears to provide localized conversions from strings to numbers, e.g. accepting "1.23" in the US and "1,23" in other countries, as appropriate.  Of course, I can substittue "." for "," before invoking tonumber(), but have I missed a better, more robust approach?

        • 1. Re: Localized conversion from string to number?
          johnrellis Most Valuable Participant

          Also, how does one go about installing/configurating LR versions for other countries, for testing purposes?

          • 2. Re: Localized conversion from string to number?
            areohbee Level 5

            Good questions John. I don't even know how to determine locale (is there a way?) - sorry I'm not much help...

            • 3. Re: Localized conversion from string to number?
              johnrellis Most Valuable Participant

              You can find the current language via LrLocalization.currentLanguage().

              • 4. Re: Localized conversion from string to number?
                areohbee Level 5

                Thanks John .

                 

                I too would like to know how to configure for testing other locales. - maybe Chet Drarvik will chime in (it's possible).

                 

                Rob

                • 5. Re: Localized conversion from string to number?
                  DawMatt Level 3

                  Can't remember which platform you use as your primary development platform. Mac OS/X has a way of running any application up under a given locale regardless of the system locale. Not aware of any way to do the same on Windows. An example of the Mac command line args was found here:

                   

                  Internet wrote:

                   

                  The user's localization preferences are set using the

                  AppleLanguages default.  This is a standard user default, and can be

                  set globally or on a per-app basis, or as a command-line argument, like

                  other defaults.  The relevant preference pane sets the global

                  preferences.  If you want to run an application using some other

                  preferences, you can for example launch it from the command line e.g.

                  /Applications/TextEdit.app/Contents/MacOS/TextEdit -AppleLanguages

                  "(fr, en)"

                   

                   

                  I'm not aware of any Lightroom or general Lua mechanisms for performing the localised string to number conversion for you. I had been hoping there was a spinner (not just a slider) control, because that would be inherently numeric and the Lightroom team might have already had to deal with the localisation issues for us. Couldn't find one during my quick scan of LrView.

                   

                  Matt

                  • 6. Re: Localized conversion from string to number?
                    johnrellis Most Valuable Participant

                    Thanks, I'll give that a try on my Mac and report back how/if it works.

                    • 7. Re: Localized conversion from string to number?
                      jarnoh Level 1

                      I think there's precision property for numeric edit fields and you can specify the amount how much to increment when up/down cursor is pressed, maybe that is close enough?

                      • 8. Re: Localized conversion from string to number?
                        DawMatt Level 3

                        jarnoh wrote:

                         

                        I think there's precision property for numeric edit fields and you can specify the amount how much to increment when up/down cursor is pressed, maybe that is close enough?

                        Worth a try. The intent was to give Lightroom a hint that it should treat the edit field content as a number, so hopefully it will convert the localised value to a numeric value before storing it to the bound property table. If the eidt field control is locale aware then this might solve John's problem even when the user doesn't use the up or down arrow keys to increment or decrement the number.

                        • 9. Re: Localized conversion from string to number?
                          johnrellis Most Valuable Participant

                          I think there's precision property for numeric edit fields and you can specify the amount how much to increment when up/down cursor is pressed, maybe that is close enough?

                          Though it's a safe bet that an edit_field does local conversions to numeric values, that doesn't solve my issue.  I want to convert strings containing numbers that don't come from a plugin user interface.  In particular, I want to convert values returned by photo:getFormattedMetadata ("subjectDistance"). 

                           

                          Yes, I know the documentation says "The returned value strings are formatted for display; you should not attempt to parse them."  But the SDK is incomplete, and getFormattedMetadata() is the only way to get many metadata and library fields. 

                          • 10. Re: Localized conversion from string to number?
                            DawMatt Level 3

                            John R. Ellis wrote:

                             

                            Though it's a safe bet that an edit_field does local conversions to numeric values, that doesn't solve my issue.  I want to convert strings containing numbers that don't come from a plugin user interface.  

                            If you have a UI available at the time you could always try using the edit_field (but non-visible) to do the conversion but that is a bit of a hack and will only work in some circumstances. So looks like you will need to keep looking for a pre-packaged solution elsewhere or roll your own.

                             

                            Looking through some of the standard libraries out there (stdlib, json, etc) it appears the json library makes use of lpeg to do its number decoding. There might be some potential for you to reuse their approach (and potentially some code) for this purpose. That should make for a more robust solution but it will take a bit of work to understand the existing code and work out how to adapt to work within the Lightroom context.

                             

                            Matt