<commit_before># Rosetta [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.ahmedjazzar.rosetta/rosetta/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.ahmedjazzar.rosetta/rosetta/badge.svg)

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

![First screenshot](https://s3-us-west-2.amazonaws.com/ahmedjazzar-ahmedjazzarcom/libs/rosetta-v1.0.png)

## How to use

#### 1. Add these lines to your *build.gradle*
You can use *Jcenter()* or *mavenCentral()*

```JAVA
repositories {
    mavenCentral()
}

dependencies {
  compile "com.ahmedjazzar.rosetta:rosetta:1.0"
}
```

#### 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:

```JAVA

// 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!* *:

```JAVA

// 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:

	```JAVA
	ls.fetchAvailableLocales(stringId);
	```

	and it's gonna return a **HashSet<Locale>** 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

```XML
<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**

```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:<commit_msg>Adding travis-ci and gitter badges<commit_after># Rosetta [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.ahmedjazzar.rosetta/rosetta/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.ahmedjazzar.rosetta/rosetta/badge.svg) [![Build Status](https://travis-ci.org/ahmedaljazzar/rosetta.svg?branch=master)](https://travis-ci.org/ahmedaljazzar/rosetta) [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/ahmedaljazzar/localizers?utm_source=badge-link&utm_medium=link&utm_campaign=share-link)


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

![First screenshot](https://s3-us-west-2.amazonaws.com/ahmedjazzar-ahmedjazzarcom/libs/rosetta-v1.0.png)

## How to use

#### 1. Add these lines to your *build.gradle*
You can use *Jcenter()* or *mavenCentral()*

```JAVA
repositories {
    mavenCentral()
}

dependencies {
  compile "com.ahmedjazzar.rosetta:rosetta:1.0"
}
```

#### 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:

```JAVA

// 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!* *:

```JAVA

// 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:

	```JAVA
	ls.fetchAvailableLocales(stringId);
	```

	and it's gonna return a **HashSet<Locale>** 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

```XML
<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**

```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:
{'fraction_non_alphanumeric': 0.10224331320103537, 'fraction_numerical': 0.003451251078515962, 'mean_word_length': 4.4355216881594375, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4163077', 'n_tokens_mistral': 2790, 'n_tokens_neox': 2654, 'n_words': 1014}