Coder Social home page Coder Social logo

iamjazzar / rosetta Goto Github PK

View Code? Open in Web Editor NEW
66.0 4.0 20.0 1.26 MB

(Help wanted) Android library for multi-language support

Home Page: https://www.ahmedjazzar.com

License: MIT License

Java 98.43% Makefile 1.57%
android rosetta-stone android-library i18n l10n languages language

rosetta's Introduction

Rosetta Maven Central Android Gems

Rosetta is an Android library that helps your app supporting multiple languages and switching between them without causing any headaches.

First screenshot

How to use

1. Add these lines to your build.gradle

You can use Jcenter() or mavenCentral()

repositories {
    mavenCentral()
}

dependencies {
  compile "com.ahmedjazzar.rosetta:rosetta:1.0.1"
}

2. Identify your locales, or let the library do it for you!

At your launch activity or MainApplication.java -if exists-, the Library offers two ways to identify and set the supported locales:

  • The first one requires you to know what languages your app gonna use. It's more efficient, predictable, and easy to use. You can use this method if you have a closed source app, or your app has a specific supported locales:
// This is the locale that you wanna your app to launch with.
Locale firstLaunchLocale = new Locale("ar");

// You can use a HashSet<String> instead and call 'setSupportedStringLocales()' :)
HashSet<Locale> supportedLocales = new HashSet<>();
supportedLocales.add(Locale.US);
supportedLocales.add(Locale.CHINA);
supportedLocales.add(firstLaunchLocale);

// You can make the following object static so you can use the same reference in all app's
// classes. static is much stable.
LanguageSwitcher ls = new LanguageSwitcher(this, firstLaunchLocale);
ls.setSupportedLocales(supportedLocales);
  • The second one asking the library to do its magic and searches inside the app for available locale. May fail! *:
// This is the locale that you wanna your app to launch with.
Locale firstLaunchLocale = new Locale("ar");

// IMPORTANT: this is the locale of the main strings.xml file. -- most developers write it
// in English, so if you wrote it in another locale specify it here.
Locale baseLocale = Locale.ENGLISH;

// stringId: the id of a string that's occurred in every locale with a different characters.
// For instance if you have 3 locales: US, UK, and Arabic the perfect fit would be the word
// 'color' -if exist for sure- because it has different form in each locale:
// Locale.US: color, Locale.UK: colour, ar: لون
int stringId = R.string.nice_string;

LanguageSwitcher ls = new LanguageSwitcher(this, firstLaunchLocale, baseLocale);
ls.setSupportedLocales(stringId);
  • The second methodology is gonna fetch the available locales in your app then set them as the support locales since there's no supported locales provided.

  • You can see what locales gonna be fetched before setting them by calling:

     ls.fetchAvailableLocales(stringId);

    and it's gonna return a HashSet of the detected locales.

    • It may fails because even if you have a folder named values-ar it doesn't mean you have any resources there, so if you have values-ar and you provided the LanguageSwitcher with a string that's not listed in, or it has the same characters in the same order; i.e nice in US and nice in UK, then your locale, unfortunately, will not detected.

3. Add a tab in your activity_settings.xml

Using this tab the user will be able to change the application language

<LinearLayout
    android:id="@id/language_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@id/languageText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Language"
        android:textSize="13sp" />

    <TextView
        android:id="@id/current_language"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="11sp"
        tools:text="English" />
</LinearLayout>

4. Add the following functionality to your SettingsActivity.java

LinearLayout languageView = (LinearLayout) layout.findViewById(R.id.language_layout);
languageView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    	// If you have a static declared switcher You can call it using the
    	// following line:
    	// MainApplication.languageSwitcher.showChangeLanguageDialog(this);
        new LanguageSwitcher(getActivity()).showChangeLanguageDialog();
    }
});

// The following 3 lines are extra and not needed :)
TextView language_tv = (TextView) layout.findViewById(R.id.current_language);
Locale currentLocale = getResources().getConfiguration().locale;
language_tv.setText(currentLocale.getDisplayName(currentLocale));

5. Cheers!

Yes! You did it. Your application now supports all Android supported locales.

Extra features

  1. To get a HashSet of the supported locales: ls.getLocales().
  2. To set the supported locales using a HashSet<String> instead of HashSet<Locale>: ls.setSupportedStringLocales(supportedLocales).
  3. To return to your launch locale (helpful when you have non-localized views): ls.switchToLaunch(SomeActivity.this)
  4. To use your own DialogFragment with your custom design:
    1. Extend LanguagesListDialogFragment and override onCreateDialog! For example:
    public class ChangeLanguageDialogFragment extends LanguagesListDialogFragment	{
    	.
    	.
    	.
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            .
            .
            .
            return builder.create();
        }
    
    	.
    	.
    	.
    }
    1. In positive button on click listener call: onPositiveClick();
    2. In OnItemClickListener use onLanguageSelected(position);, or if you wanna localize the dialog title, positive button, and negative button texts call onLanguageSelectedLocalized(position, titleTextView, positiveBtn, negativeBtn);.
    3. You can use other helpful methods available in the parent class like:
      1. getLanguages() to get a display-ready languages.
      2. getCurrentLocaleIndex() to get the index of the locale that your app is displaying right now.

What is Rosetta?

Rosetta is the first name of Rosetta Stone. The Rosetta Stone is a granodiorite stele inscribed with a decree issued at Memphis, Egypt, in 196 BC on behalf of King Ptolemy V. The decree appears in three scripts: the upper text is Ancient Egyptian hieroglyphs, the middle portion Demotic script, and the lowest Ancient Greek. Because it presents essentially the same text in all three scripts (with some minor differences among them), the stone provided the key to the modern understanding of Egyptian hieroglyphs.

  • Continue reading here

License

The MIT License (MIT) Copyright (c) 2016 Ahmed Jazzar [email protected]

NOTE: This library does not translate your application localized strings, it's just helping you switching between them.

rosetta's People

Contributors

iamjazzar avatar

Stargazers

the Netsurfer avatar Der_Googler avatar  avatar Rsmouki avatar Avi Parshan avatar Zeeshan rasool avatar iBR avatar  avatar  avatar Hasan Küçük avatar Arslan Maqbool avatar mohammad moein abdi avatar  avatar Y.Es avatar Alexander Steller avatar Ema Fazillah Abu Bakar avatar  avatar Mustafa Bayram avatar Arda Çebi avatar  avatar 行一 avatar alphater avatar Masoud Mohammadi avatar Anartz Mugika Ledo avatar  avatar Bradley K. Ochola avatar Mutlu Celep avatar Evgenii Shishkin avatar Silvana Dorantes avatar  avatar Kishan Vaghela avatar Leonardo Deleon avatar alphaDroid89 avatar Sandeep Kurne avatar Porramate Lim avatar 0xEwoks avatar  avatar hazem habeb avatar Maximilian Keppeler avatar Juan Labrador avatar Basi avatar Daniel Cardenas avatar  avatar Viktor Ponomarenko avatar Raj Kiran avatar Santosh Suresh Gaikar avatar Pranav Lathigara avatar Hoa Nguyen avatar Vlad Ivanov avatar Hocine Hamdi avatar Rainer-Lang avatar Mahmoud Abdurrahman avatar Matej Vukosav avatar Gürsu Aşık avatar Rab Ross avatar Jungle avatar Pinkal Patel avatar Suraj Singh Pawar avatar Xiaohai.lin avatar Mohamed Nabil avatar Wajahat Karim avatar Filipe Barroso avatar  avatar Saed Yousef avatar  avatar Moh'd Muthanna avatar

Watchers

Gürsu Aşık avatar Basi avatar  avatar RufinoSalgadoJr avatar

rosetta's Issues

Language doesn't change immediately

After choosing language from ChangeLanguageDialog, language doesn't change immediately. But it changes in next app launch after killing the current app.

Orientation change switches the language

I have downloaded the sample APK of yours when i switch the orientation from portrait to landscape the app looses language and gets back to english language if you have switched language to Arabic before in portrait mode

Edit Documentation

Would anyone mind if I propose to edit Readme.md? I am taking a technical writing course in university and our assignment involves editing github project documentations to practice real world examples.
The goal is not to significantly change or remove any part of the technical information but to improve the structure of the writing or fix typos and grammatical issues. Edits may involve aspects like clarity and concision too.
It will really help me out if you allow me to contribute, make a few changes and submit a PR. Thanks!

Language is not getting changed in Android 6.0

The locale is not getting changed in Android 6.0 (Huawei Honor 5X). It works perfect on Nougat phones.

Logs while changing Language.

D/com.ahmedjazzar.rosetta.LanguagesListDialogFragment: Displaying dialog main strings in the selected locale
D/com.ahmedjazzar.rosetta.LanguagesListDialogFragment: Displaying dialog main strings in the selected locale
I/HwSecImmHelper: mSecurityInputMethodService is null
V/AudioManager: playSoundEffect   effectType: 0
V/AudioManager: querySoundEffectsEnabled...
D/com.ahmedjazzar.rosetta.LocalesDetector: Setting the layout direction
I/com.ahmedjazzar.rosetta.LocalesDetector: Locale preferences updated to: ta
I/com.ahmedjazzar.rosetta.LanguagesListDialogFragment: App locale changed successfully.
D/com.ahmedjazzar.rosetta.LocalesDetector: Refreshing the application: com.pureh.merry
D/com.ahmedjazzar.rosetta.LocalesDetector: Finishing current activity.
D/com.ahmedjazzar.rosetta.LocalesDetector: Start the application
D/com.ahmedjazzar.rosetta.LocalesDetector: Application refreshed
V/ActivityThread: ActivityThread,callActivityOnCreate

Logs During App Startup after selecting another Language.

V/com.ahmedjazzar.rosetta.LocalesDetector: Object from com.ahmedjazzar.rosetta.LocalesDetector has been created.
V/com.ahmedjazzar.rosetta.LocalesDetector: Object from com.ahmedjazzar.rosetta.LocalesDetector has been created.
D/com.ahmedjazzar.rosetta.LocalesDetector: Setting the layout direction
I/com.ahmedjazzar.rosetta.LocalesDetector: Locale preferences updated to: ta
D/com.ahmedjazzar.rosetta.LocalesDetector: Validating given locales..
D/com.ahmedjazzar.rosetta.LocalesDetector: passing validated locales.
D/com.ahmedjazzar.rosetta.LocalesDetector: Locales have been changed

Language changing before pressing the okay button.

Hi,
Suppose there are two languages A,B and default selected one is A. I opened the the ChangeLanguageDialog. I selected the language B but pressed the Cancel. Still when I try to get strings I should get String in language A, but I am getting in Language B. After reloading the activity It is loading the String of A language.

System strings messed up when changing BaseLocale, requires app uninstall to fix.

So I noticed a sort-of-bug :
Suppose I have my Rosetta config defined as:

Locale firstLaunchLocale = Locale.getDefault();
Locale baseLocale = Locale.US;

HashSet<Locale> supportedLocales = new HashSet<>();
supportedLocales.add(new Locale("ml"));
supportedLocales.add(new Locale("ar"));
supportedLocales.add(new Locale("hi"));
supportedLocales.add(baseLocale);

LanguageSwitcher ls = new LanguageSwitcher(this,firstLaunchLocale,baseLocale);
ls.setSupportedLocales(supportedLocales);

and I publish the app.

Later, I noticed that the language switcher dialog was showing English(US), but I preferred to show just English. So I changed the baseLocale to Locale.ENGLISH.

Now, the issue is when I change the firstLaunchLocale and/or baseLocale, and then push an update, users who update the app from the previous version experience this in most of the system provided strings:

The only solution I found is to uninstall the older version and do a fresh install of the new version.

So is there any way to rectify this?

Feedback rosetta BUG

I set the preferred language is (automatically detect the system language),But if I changed the language of the phone system, and then open the program, the program language will not automatically switch

Code:
firstLaunchLocale = Locale.getDefault();
supportedLocales = new HashSet<>();
supportedLocales.add(Locale.CHINESE);
supportedLocales.add(Locale.ENGLISH);
supportedLocales.add(firstLaunchLocale);
languageSwitcher = new LanguageSwitcher(this, firstLaunchLocale);
languageSwitcher.setSupportedLocales(supportedLocales);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.