55import android .content .Intent ;
66import android .content .res .TypedArray ;
77import android .graphics .Canvas ;
8+ import android .net .Uri ;
89import android .util .AttributeSet ;
910import android .util .Log ;
1011import android .view .LayoutInflater ;
1920
2021import com .google .android .material .button .MaterialButton ;
2122import com .google .android .material .card .MaterialCardView ;
23+ import com .localazy .android .Localazy ;
24+ import com .localazy .android .LocalazyLocale ;
2225import com .paget96 .localazycard .utils .LocaleUtils ;
2326import com .paget96 .localazycard .utils .UiUtils ;
2427
2528import java .util .AbstractMap ;
29+ import java .util .Arrays ;
2630import java .util .HashMap ;
2731import java .util .List ;
32+ import java .util .Locale ;
2833import java .util .Map ;
2934
3035public class LocalazyCard extends MaterialCardView {
@@ -35,20 +40,26 @@ public class LocalazyCard extends MaterialCardView {
3540 private View rootView ;
3641 private TextView titleTextView , summaryTextView ;
3742 private ImageView iconImageView ;
38- private MaterialButton inviteButton , translateButton ;
43+ public MaterialButton inviteButton , translateButton ;
3944 private TextView language ;
4045 private ImageView arrowDown ;
4146 private LinearLayout selectLanguageLayout ;
4247 private MaterialCardView selectLanguage ;
48+ private Uri localazyTranslationLink ;
4349 private Activity activity ;
44- private Map <String , String > languages ;
50+ private Map <String , String > languagesInternal ;
51+ private Map <LocalazyLocale , String > languagesLocalazy ;
4552
4653 public void setActivity (Activity activity ) {
4754 this .activity = activity ;
4855 }
4956
5057 public void setLanguages (String language , String languageCountry ) {
51- languages .put (language , languageCountry );
58+ languagesInternal .put (language , languageCountry );
59+ }
60+
61+ private void setLanguages (LocalazyLocale language , String localeName ) {
62+ languagesLocalazy .put (language , localeName );
5263 }
5364
5465 public LocalazyCard (final Context context ) {
@@ -61,7 +72,9 @@ public LocalazyCard(Context context, AttributeSet attrs) {
6172
6273 public LocalazyCard (Context context , AttributeSet attrs , int defStyleAttr ) {
6374 super (context , attrs , defStyleAttr );
64- languages = new HashMap <>();
75+ languagesInternal = new HashMap <>();
76+ languagesLocalazy = new HashMap <>();
77+ localazyTranslationLink = Localazy .getProjectUri ();
6578
6679 initializeViews (context , attrs , defStyleAttr );
6780 }
@@ -89,9 +102,6 @@ private void initializeViews(Context context, AttributeSet attrs, int defStyleAt
89102 // Set summary
90103 setSummaryText (checkIsStringEmpty (attributes .getString (R .styleable .LocalazyCard_localazy_summary ), context .getString (R .string .localazy_default_summary )));
91104
92- // Open link on a button press
93- setTranslateButton (checkIsStringEmpty (attributes .getString (R .styleable .LocalazyCard_localazy_app_translation_link ), "https://localazy.com" ));
94-
95105 attributes .recycle ();
96106 }
97107
@@ -107,24 +117,40 @@ private void setLanguageInitialization() {
107117 language = rootView .findViewById (R .id .language );
108118 arrowDown = rootView .findViewById (R .id .expand_arrow );
109119
110- language .setText (LocaleUtils .getLanguage (context ));
120+ LocalazyLocale currentLocale = Localazy .getCurrentLocalazyLocale ();
121+ language .setText (currentLocale .getLocalizedName ());
122+
123+ List <LocalazyLocale > locales = Localazy .getLocales ();
111124
112- if (languages != null ) {
113- if (languages .size () > 1 ) {
125+ for (LocalazyLocale locale : locales ) {
126+ // Returns display name for the locale in its own language - eq. "Čeština (Česko)" for "cs_CZ".
127+ String localizedName = locale .getLocalizedName ();
128+
129+ setLanguages (locale , localizedName );
130+ }
131+
132+ if (languagesLocalazy != null ) {
133+ if (languagesLocalazy .size () > 1 ) {
114134 rootView .setOnClickListener (v -> UiUtils .expandCollapseView (selectLanguageLayout , arrowDown ));
115135
116136 selectLanguage .setOnClickListener (v -> {
117137 AlertDialog .Builder builder = new AlertDialog .Builder (context );
118138 builder .setTitle (context .getString (R .string .localazy_set_language_dialog_title ));
119139
120- String [] langArray , langCountryArray ;
121- langArray = languages .keySet ().toArray (new String [0 ]);
122- langCountryArray = languages .values ().toArray (new String [0 ]);
123- // for (Map.Entry<String, String> pair : languages.entrySet()) {
140+ LocalazyLocale [] langArray ;
141+ String [] langNameArray ;
142+ langArray = languagesLocalazy .keySet ().toArray (new LocalazyLocale [0 ]);
143+ langNameArray = languagesLocalazy .values ().toArray (new String [0 ]);
144+
145+ String [] languageName = new String [langArray .length ];
146+ for (int i = 0 ; i < langArray .length ; i ++) {
147+ languageName [i ] = langArray [i ].getLocalizedName ();
148+ }
149+
150+ builder .setItems (languageName , (dialog , which ) -> {
151+ language .setText (langArray [which ].getLocalizedName ());
124152
125- builder .setItems (langArray , (dialog , which ) -> {
126- language .setText (langArray [which ]);
127- LocaleUtils .setLocale (context , langArray [which ], langCountryArray [which ]);
153+ Localazy .forceLocale (langArray [which ].getLocale (), true );
128154 activity .recreate ();
129155 });
130156
@@ -175,18 +201,18 @@ public void setIcon(int icon) {
175201 iconImageView .setImageDrawable (ContextCompat .getDrawable (context , icon ));
176202 }
177203
178- public void setTranslateButton (String url ) {
179- translateButton .setOnClickListener (v -> UiUtils .openLink (context , url ));
204+ public void setTranslateButton () {
205+ translateButton .setOnClickListener (v -> UiUtils .openLink (context , localazyTranslationLink . toString () ));
180206 }
181207
182- public void setInviteButton (Context context , String textMessage , String url ) {
208+ public void setInviteButton (String textMessage ) {
183209 inviteButton .setVisibility (VISIBLE );
184210 inviteButton .setOnClickListener (v -> {
185211 Intent sendIntent = new Intent ();
186212 sendIntent .setAction (Intent .ACTION_SEND );
187- sendIntent .putExtra (Intent .EXTRA_TEXT , textMessage + " " + url );
213+ sendIntent .putExtra (Intent .EXTRA_TEXT , textMessage + " " + localazyTranslationLink . toString () );
188214 sendIntent .setType ("text/plain" );
189- context .startActivity (sendIntent );
215+ activity .startActivity (sendIntent );
190216 });
191217 }
192218}
0 commit comments