Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit f0737ca

Browse files
committed
Capitalize first letter for all languages
1 parent d70dcda commit f0737ca

File tree

7 files changed

+1019
-3
lines changed

7 files changed

+1019
-3
lines changed
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
package com.ahmedjazzar.rosetta;
2+
3+
import android.app.Activity;
4+
import android.content.Context;
5+
6+
import androidx.annotation.NonNull;
7+
import androidx.fragment.app.FragmentActivity;
8+
9+
import java.util.HashSet;
10+
import java.util.Locale;
11+
12+
/**
13+
* This class is the application door to this Library. It handles the ongoing and outgoing requests,
14+
* initializations, preferences, ..
15+
* I think that there's no need for logging here because other classes already handle logs for these
16+
* actions based on their returned results.
17+
*
18+
* Created by ahmedjazzar on 1/16/16.
19+
*/
20+
public class LanguageSwitcher {
21+
22+
private Context mContext;
23+
private LocalesPreferenceManager mLocalesPreferences;
24+
private final String TAG = LanguageSwitcher.class.getName();
25+
26+
/**
27+
* A constructor that accepts context and sets the base and first launch locales to en_US
28+
* @param context the context of the dealer
29+
*/
30+
public LanguageSwitcher(@NonNull Context context) {
31+
this(context, Locale.US);
32+
}
33+
34+
/**
35+
* A constructor that accepts context and sets the base and first launch locales to
36+
* firstLaunchLocale.
37+
*
38+
* NOTE: Please do not use unless:
39+
* 1. You wanna set your locales by calling {@link LanguageSwitcher#setSupportedLocales}
40+
* 2. You know for sure that the preferred locale is as same as your base locale
41+
*
42+
* @param context the context of the dealer
43+
* @param firstLaunchLocale the locale that owner wanna use at its first launch
44+
*/
45+
public LanguageSwitcher(@NonNull Context context, Locale firstLaunchLocale) {
46+
this(context, firstLaunchLocale, firstLaunchLocale);
47+
}
48+
49+
/**
50+
* This is supposed to be more specific; It has three parameters cover all owner needs
51+
* @param context the context of the dealer
52+
* @param firstLaunchLocale the locale that owner wanna use at its first launch
53+
* @param baseLocale the locale that used in the main xml strings file (most likely 'en')
54+
*/
55+
public LanguageSwitcher(@NonNull Context context, Locale firstLaunchLocale, Locale baseLocale) {
56+
this.mContext = context.getApplicationContext();
57+
58+
this.mLocalesPreferences =
59+
new LocalesPreferenceManager(context, firstLaunchLocale, baseLocale);
60+
61+
// initializing Locales utils needed objects (detector, preferences)
62+
LocalesUtils.setDetector(new LocalesDetector(this.mContext));
63+
LocalesUtils.setLocalesPreferenceManager(mLocalesPreferences);
64+
65+
// Setting app locale to match the user preferred one
66+
LocalesUtils.setAppLocale(mContext,
67+
mLocalesPreferences
68+
.getPreferredLocale(LocalesPreferenceManager.USER_PREFERRED_LOCALE));
69+
}
70+
71+
/**
72+
* Responsible for displaying Change dialog fragment
73+
*/
74+
public void showChangeLanguageDialog(FragmentActivity activity) {
75+
new LanguagesListDialogFragment()
76+
.show(activity.getSupportFragmentManager(), TAG);
77+
}
78+
79+
/**
80+
*
81+
* @return the application supported locales
82+
*/
83+
public HashSet<Locale> getLocales() {
84+
return LocalesUtils.getLocales();
85+
}
86+
87+
/**
88+
* Sets the app locales from a string Set
89+
* @param sLocales supported locales in a String form
90+
*/
91+
public void setSupportedStringLocales(HashSet<String> sLocales) {
92+
93+
HashSet<Locale> locales = new HashSet<>();
94+
for (String sLocale: sLocales) {
95+
locales.add(new Locale(sLocale));
96+
}
97+
this.setSupportedLocales(locales);
98+
}
99+
100+
/**
101+
* set supported locales from the given Set
102+
* @param locales supported locales
103+
*/
104+
public void setSupportedLocales(HashSet<Locale> locales) {
105+
LocalesUtils.setSupportedLocales(locales);
106+
}
107+
108+
/**
109+
* Sets the supported locales after fetching there availability using fetchAvailableLocales
110+
* method
111+
* @param stringId the string that this library gonna use to detect current app available
112+
* locales
113+
*/
114+
public void setSupportedLocales(int stringId) {
115+
this.setSupportedLocales(this.fetchAvailableLocales(stringId));
116+
}
117+
118+
/**
119+
* Fetching the application available locales inside the resources folder dynamically
120+
* @param stringId the string that this library gonna use to detect current app available
121+
* locales
122+
* @return a set of detected application locales
123+
*/
124+
public HashSet<Locale> fetchAvailableLocales(int stringId) {
125+
return LocalesUtils.fetchAvailableLocales(stringId);
126+
}
127+
128+
/**
129+
* Setting the application locale manually
130+
* @param newLocale the locale in a string format
131+
* @param activity the current activity in order to refresh the app
132+
*
133+
* @return true if the operation succeed, false otherwise
134+
*/
135+
public boolean setLocale(String newLocale, Activity activity) {
136+
return setLocale(new Locale(newLocale), activity);
137+
}
138+
139+
/**
140+
* Setting the application locale manually
141+
* @param newLocale the desired locale
142+
* @param activity the current activity in order to refresh the app
143+
*
144+
* @return true if the operation succeed, false otherwise
145+
*/
146+
public boolean setLocale(Locale newLocale, Activity activity) {
147+
148+
return LocalesUtils.setLocale(newLocale, activity);
149+
}
150+
151+
/**
152+
*
153+
* @return the first launch locale
154+
*/
155+
public Locale getLaunchLocale() {
156+
157+
return LocalesUtils.getLaunchLocale();
158+
}
159+
160+
/**
161+
*
162+
* @return the current locale
163+
*/
164+
public Locale getCurrentLocale() {
165+
166+
return LocalesUtils.getCurrentLocale(this.mContext);
167+
}
168+
169+
/**
170+
* Return to the first launch locale
171+
* @param activity the current activity in order to refresh the app
172+
*
173+
* @return true if the operation succeed, false otherwise
174+
*/
175+
public boolean switchToLaunch(Activity activity) {
176+
177+
return setLocale(getLaunchLocale(), activity);
178+
}
179+
}
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
2+
package com.ahmedjazzar.rosetta;
3+
4+
import android.app.Dialog;
5+
import android.os.Bundle;
6+
import android.widget.Button;
7+
import android.widget.TextView;
8+
9+
import androidx.annotation.NonNull;
10+
import androidx.appcompat.app.AlertDialog;
11+
import androidx.fragment.app.DialogFragment;
12+
import androidx.fragment.app.FragmentActivity;
13+
14+
import java.util.ArrayList;
15+
import java.util.Locale;
16+
import java.util.Objects;
17+
18+
import com.fox2code.mmm.R;
19+
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
20+
21+
/**
22+
* This fragment is responsible for displaying the supported locales and performing any necessary
23+
* action that allows user to select, cancel, and commit changes.
24+
*
25+
* Created by ahmedjazzar on 1/19/16.
26+
*/
27+
28+
public class LanguagesListDialogFragment extends DialogFragment {
29+
30+
private final int DIALOG_TITLE_ID = R.string.language;
31+
private final int DIALOG_POSITIVE_ID = R.string.ok;
32+
private final int DIALOG_NEGATIVE_ID = R.string.cancel;
33+
34+
private int mSelectedLanguage = -1;
35+
private final Logger mLogger;
36+
37+
public LanguagesListDialogFragment() {
38+
String TAG = LanguagesListDialogFragment.class.getName();
39+
this.mLogger = new Logger(TAG);
40+
}
41+
42+
/**
43+
* @return a Dialog fragment
44+
*/
45+
@NonNull
46+
@Override
47+
public Dialog onCreateDialog(Bundle savedInstanceState) {
48+
final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(requireActivity());
49+
mLogger.debug("Building DialogFragment.");
50+
51+
builder.setTitle(getString(DIALOG_TITLE_ID))
52+
.setSingleChoiceItems(
53+
getLanguages(),
54+
getCurrentLocaleIndex(),
55+
(dialogInterface, which) -> onLanguageSelectedLocalized(which))
56+
.setPositiveButton(
57+
getString(DIALOG_POSITIVE_ID).toUpperCase(),
58+
(dialogInterface, which) -> onPositiveClick())
59+
.setNegativeButton(
60+
getString(DIALOG_NEGATIVE_ID).toUpperCase(),
61+
(dialogInterface, which) -> onNegativeClick());
62+
63+
mLogger.verbose("DialogFragment built.");
64+
return builder.create();
65+
}
66+
67+
/**
68+
* @param which the position of the selected locale
69+
*/
70+
protected void onLanguageSelected(int which) {
71+
// just update the selected locale
72+
mSelectedLanguage = which;
73+
}
74+
75+
/**
76+
* Localizing the dialog buttons and title
77+
* @param which the position of the selected locale
78+
*/
79+
protected void onLanguageSelectedLocalized(int which) {
80+
81+
// update the selected locale
82+
mSelectedLanguage = which;
83+
AlertDialog dialog = (AlertDialog) getDialog();
84+
85+
mLogger.debug("Displaying dialog main strings in the selected " +
86+
"locale");
87+
88+
onLanguageSelectedLocalized(
89+
which,
90+
null,
91+
dialog.getButton(AlertDialog.BUTTON_POSITIVE),
92+
dialog.getButton(AlertDialog.BUTTON_NEGATIVE));
93+
}
94+
95+
/**
96+
* the position of the selected locale given the ids
97+
* @param which the position of the selected locale
98+
* @param titleView dialog's title text view
99+
* @param positiveButton positive button
100+
* @param negativeButton negative button
101+
*/
102+
protected void onLanguageSelectedLocalized(int which, TextView titleView, Button positiveButton,
103+
Button negativeButton) {
104+
105+
// update the selected locale
106+
mSelectedLanguage = which;
107+
Locale locale = LocalesUtils.getLocaleFromIndex(mSelectedLanguage);
108+
AlertDialog dialog = (AlertDialog) getDialog();
109+
FragmentActivity activity = getActivity();
110+
111+
mLogger.debug("Displaying dialog main strings in the selected " +
112+
"locale");
113+
114+
assert activity != null;
115+
String LocalizedTitle = LocalesUtils.getInSpecificLocale(activity, locale, DIALOG_TITLE_ID);
116+
if(titleView == null) {
117+
// Display dialog title in the selected locale
118+
assert dialog != null;
119+
dialog.setTitle(LocalizedTitle);
120+
} else {
121+
titleView.setText(LocalizedTitle);
122+
}
123+
124+
// Display positive button text in the selected locale
125+
positiveButton.setText(LocalesUtils.getInSpecificLocale(
126+
activity, locale, DIALOG_POSITIVE_ID));
127+
128+
// Display negative button text in the selected locale
129+
negativeButton.setText(LocalesUtils.getInSpecificLocale(
130+
activity, locale, DIALOG_NEGATIVE_ID));
131+
}
132+
133+
/**
134+
* called when the user approved changing locale
135+
*/
136+
protected void onPositiveClick() {
137+
138+
// if the user did not select the same locale go ahead, else ignore
139+
if (mSelectedLanguage != -1 &&
140+
mSelectedLanguage != LocalesUtils.getCurrentLocaleIndex()) {
141+
142+
// Try changing the locale
143+
if (LocalesUtils.setAppLocale(
144+
getActivity(), mSelectedLanguage)) {
145+
146+
mLogger.info("App locale changed successfully.");
147+
LocalesUtils.refreshApplication(requireActivity());
148+
} else {
149+
mLogger.error("Unsuccessful trial to change the App locale.");
150+
// TODO: notify the user that his request not placed
151+
}
152+
} else {
153+
dismiss();
154+
}
155+
}
156+
157+
/**
158+
* called when the user discarded changing locale
159+
*/
160+
protected void onNegativeClick() {
161+
mLogger.verbose("User discarded changing language.");
162+
mLogger.debug("Return to the original locale.");
163+
this.onLanguageSelectedLocalized(this.getCurrentLocaleIndex());
164+
}
165+
166+
/**
167+
*
168+
* @return available languages
169+
*/
170+
protected String[] getLanguages() {
171+
ArrayList<String> languages = LocalesUtils.getLocalesWithDisplayName();
172+
return languages.toArray(new String[languages.size()]);
173+
}
174+
175+
/**
176+
*
177+
* @return the index of the locale that app is using now
178+
*/
179+
protected int getCurrentLocaleIndex() {
180+
return LocalesUtils.getCurrentLocaleIndex();
181+
}
182+
183+
}

0 commit comments

Comments
 (0)