-
Notifications
You must be signed in to change notification settings - Fork 569
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1522 from iamsh4shank/language_support
Feat: Language support
- Loading branch information
Showing
17 changed files
with
250 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
mifosng-android/src/main/java/com/mifos/utils/LanguageHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.mifos.utils; | ||
|
||
import android.annotation.TargetApi; | ||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
import android.content.res.Configuration; | ||
import android.content.res.Resources; | ||
import android.os.Build; | ||
import android.preference.PreferenceManager; | ||
|
||
import com.mifos.mifosxdroid.R; | ||
|
||
import java.util.Locale; | ||
|
||
/** | ||
* Created by shashankpriyadarshi on 20/06/20. | ||
*/ | ||
|
||
public class LanguageHelper { | ||
|
||
public static Context onAttach(Context context) { | ||
String lang = getPersistedData(context, Locale.getDefault().getLanguage()); | ||
return setLocale(context, lang); | ||
} | ||
|
||
public static Context onAttach(Context context, String defaultLanguage) { | ||
String lang = getPersistedData(context, defaultLanguage); | ||
return setLocale(context, lang); | ||
} | ||
|
||
public static Context setLocale(Context context, String language) { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | ||
return updateResources(context, language); | ||
} | ||
|
||
return updateResourcesLegacy(context, language); | ||
} | ||
|
||
private static String getPersistedData(Context context, String defaultLanguage) { | ||
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); | ||
return preferences.getString(context.getString(R.string.language_type), defaultLanguage); | ||
} | ||
|
||
@TargetApi(Build.VERSION_CODES.N) | ||
private static Context updateResources(Context context, String language) { | ||
Locale locale = new Locale(language); | ||
Locale.setDefault(locale); | ||
|
||
Configuration configuration = context.getResources().getConfiguration(); | ||
configuration.setLocale(locale); | ||
configuration.setLayoutDirection(locale); | ||
|
||
return context.createConfigurationContext(configuration); | ||
} | ||
|
||
@SuppressWarnings("deprecation") | ||
private static Context updateResourcesLegacy(Context context, String language) { | ||
Locale locale = new Locale(language); | ||
Locale.setDefault(locale); | ||
|
||
Resources resources = context.getResources(); | ||
|
||
Configuration configuration = resources.getConfiguration(); | ||
configuration.locale = locale; | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { | ||
configuration.setLayoutDirection(locale); | ||
} | ||
|
||
resources.updateConfiguration(configuration, resources.getDisplayMetrics()); | ||
|
||
return context; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.