Android Question Really dumb question...

kepler

Active Member
Licensed User
Longtime User
Hi,

I have the following sample code:

B4X:
Dim lang As String = "english"
Dim code As String
Dim language As String = lang.ToLowerCase

If(Regex.IsMatch("portuguese",language) = True) Then code = "PT"
If(Regex.IsMatch("english",language) = True) Then code = "EN":Log("EN"):Log(code) 'Prints EN - correct and PT - WRONG
   
Log(code) 'Prints PT - WRONG
Log(Regex.IsMatch("english",language)) 'Prints True!!! - Correct

What the hell am I missing here...?....

Regards,

Kepler
 

sorex

Expert
Licensed User
Longtime User
the : in the if then is probably causing the second command to go outside the if/then.

put some enters after the then, the commands and add an end if and see if that works

why don't you use select language and a few case statements instead?
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
why don't you use select language and a few case statements instead?

Anyway, I would use a Map.


Probably both the answer above are wrong: why he uses Regex?

To use a Map or a Select Case, the variable Language must contain exactly the language "name".

Moreover, it is very likely that the library AHLocale can solve every problem.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
I've tested your sample code and prints EN as expected. You should recheck your code, whether it is identical to the one you posted.
 
Upvote 0

kepler

Active Member
Licensed User
Longtime User
Hi,

Everyone's right... lang is defined as a literal string. BUT, I'm actually atributing to lang the result of a function:

B4X:
Sub GetDefaultLanguage As String
   Dim r As Reflector
   r.Target = r.RunStaticMethod("java.util.Locale", "getDefault", Null, Null)
   Return r.RunMethod("getDisplayName")
End Sub

If I define lang as string = "english" I get good results. But if I give lang the value of GetDefaultLanguage (and then put lang to lower case), I get nothing. Even if the Log() is right (gives the lang as "english..." instead of "English...")

Thoughts?

Kepler
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

kepler

Active Member
Licensed User
Longtime User
Good morning,

No, not even uppercase works DonManfred... I've managed to put it to work, by making the following (example):

B4X:
If language.CompareTo("english")>=0 Then
         code = "EN"
    End If

How about that...?

Kepler
 
Upvote 0
Top