Phrase is an Android string resource templating library

Related tags

Utility phrase
Overview

Phrase - Android string formatting

license maven build

CharSequence formatted = Phrase.from("Hi {first_name}, you are {age} years old.")
  .put("first_name", firstName)
  .put("age", age)
  .format();

Send your phrase straight into a TextView:

Phrase.from("Welcome back {user}.")
  .put("user", name)
  .into(textView);

Comma-separated lists:

CharSequence formattedList = ListPhrase.from(", ")
  .format(1, 2, 3);
// returns "1, 2, 3"

English sentence-style lists:

ListPhrase listFormatter = ListPhrase.from(
  " and ",
  ", ",
  ", and ");

listFormatter.format(Arrays.asList(1, 2));
// returns "1 and 2"

listFormatter.format(Arrays.asList(1, 2, 3));
// returns "1, 2, and 3"

Download

You can download the latest jar here.

You can also depend on the .jar through Maven:

<dependency>
  <groupId>com.squareup.phrase</groupId>
  <artifactId>phrase</artifactId>
  <version>(insert latest version)</version>
</dependency>

or through Gradle:

dependencies {
  compile 'com.squareup.phrase:phrase:(insert latest version)'
}

License

Copyright 2013 Square, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Comments
  • Uppercase keys support

    Uppercase keys support

    Hi,

    I'm getting a crash when using Phrase.from("This is an {EXAMPLE}"); Throwing a 'Unexpected character 'E'; expected key.'

    Could you add support for uppercase keys? It would help me since i'm not controlling the strings I receive.

    Thank you !

    opened by Christophe668 17
  • Added support for formatting lists.

    Added support for formatting lists.

    It's a common pattern in US English to separate items in a 2-element list with a single "and" – e.g. "one and two". Larger lists may use commas to separate every element but the last, e.g. "one, two, and three".

    ListPhrase takes separator patterns for each of these cases, and formats lists correctly based on their size.

    Since list separators can be defined in strings.xml, they can also be localized by translators.

    opened by zach-klippenstein 7
  • Got exception: IllegalArgumentException: Missing closing brace: }

    Got exception: IllegalArgumentException: Missing closing brace: }

    Hi there,

    The following string and the invocation of Phrase got me this exception every time. Could someone take a look and tell me what went wrong? Thanks! : <string name="example_string">{aaa} (Now {bbb})</string>

    Phrase.from(this, R.string.example_string)
                        .put("aaa", getAaa())
                        .put("bbb", getBbb())
                        .format()
    

    This is the exception:

    Exploded trying to parse content: java.lang.IllegalArgumentException: Missing closing brace: }
        at com.squareup.phrase.Phrase.key(Phrase.java:245)
        at com.squareup.phrase.Phrase.token(Phrase.java:221)
        at com.squareup.phrase.Phrase.<init>(Phrase.java:204)
        at com.squareup.phrase.Phrase.from(Phrase.java:112)
        at com.squareup.phrase.Phrase.from(Phrase.java:103)
        at com.squareup.phrase.Phrase.from(Phrase.java:94)
    
    opened by genius1wjc 5
  • support deferred resource binding

    support deferred resource binding

    It's occasionally useful to be able to populate the Phrase parameter set without having access to Resources, and actually load the template and bind the parameters late. The current API does not support this well.

    opened by loganj 2
  • Can we expect updates in the near future?

    Can we expect updates in the near future?

    Hi,

    would like to adopt the library but things like the camel-case bug #32 or missing plurals support #2 are kind of blockers to me. Some PRs are open for quite a while now. Can we expect updates in the near future?

    opened by chriswiesner 1
  • Update license and remove jdk7 to fix build

    Update license and remove jdk7 to fix build

    Oracle JDK 7 no longer available; openJDK7 is missing an EC crypto provider needed to download gradle plugins, so I just removed the 7 compiler. We could pursue workarounds but they're a bit messy, see https://github.com/uber-java/tally/blob/2cea75ecc2f896dfd5e32b94bff71f211c8bde56/ci/before_install.sh#L6-L12

    and gradle/gradle#2421

    opened by pforhan 1
  • Support Spannable for ListPhrase

    Support Spannable for ListPhrase

    ListPhrase listFormatter = ListPhrase.from(" and ", ", ", and ");
    
    String[] texts=new String[]{"Joe", "Jane", "John"};
    ArrayList<CharSequence> bolds=new new ArrayList<>();
    
    for(String t : texts){
         Spannable b = new SpannableString(t);
         boldString.setSpan(new StyleSpan(BOLD), 0, t.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
         bolds.add(b)
    }
    return listFormatter.join(bolds)
    

    That doesn't show style while Phrase support for Spannable

    opened by zombispormedio 1
  • about 'orderBy' IllegalArgumentException

    about 'orderBy' IllegalArgumentException

    CharSequence formatted = Phrase.from("Hi {first_name}, you are {orderBy} years old.") .put("first_name", firstName) .put("orderBy", age) .format();

    when the string contains 'orderBy' it Throw IllegalArgumentException: Missing closing brace: }

    opened by guitarstar 1
  • Support for colors

    Support for colors

    Something like this?

    <string name="xx"><color="@color/color_id">{a}</color> is {b}<color="#aabbcc">awesome?</color></string>
    
    opened by b22n 1
  • into() : Applying a Phrase to a TextView

    into() : Applying a Phrase to a TextView

    Allows setting text directly from the Phrase chain, similar to how Picasso does it.

    Before:

      CharSequence greeting = Phrase.from("Hello {user}!").put("user", "Quinn").format();
      greetingTextView.setText(greeting);
    

    Now:

      Phrase.from("Hello {user}!")
          .put("user", "Quinn")
          .into(greetingTextView);
    
    opened by quinnjn 1
  • AnnotatedString support

    AnnotatedString support

    Seeing as Jetpack Compose is the new hotness, it would be great to have support for building AnnotatedStrings. Options:

    1. Yolo, add the Compose dependency and people can manually remove it if they care.
    2. Fork
    3. Do the okio/etc thing and change the artifact id to phrase2 and include Compose in that artifact. If somehow there are bugs that need to get fixed, in theory we can do a release of both artifacts
    4. Separate phrase-compose artifact, make keys/keysToValues public, and add a method in the new artifact that reads them and builds an AnnotatedString
    opened by edenman 0
  • API to disable span support

    API to disable span support

    This mode unlocks the ability for consumers to disable span support. This enables writing pure unit tests that do not depend on SpannableStringBuilder which otherwise would require Robolectric to run and make the tests much much slower.

    Fixed all warnings too.

    @loganj @ChrisRenke @rjrjr

    opened by dnkoutso 4
  • enhance tests readability

    enhance tests readability

    enhance tests readability by using ExpectedException instead of try/catch blocks. some brief explanation here : http://turhanoz.com/junit4-testing-exception/

    opened by TurhanOz 0
Owner
Square
Square
Fuzzy string matching for Kotlin (JVM, native, JS, Web Assembly) - port of Fuzzy Wuzzy Python lib

FuzzyWuzzy-Kotlin Fuzzy string matching for Kotlin (JVM, iOS) - fork of the Java fork of of Fuzzy Wuzzy Python lib. For use in on JVM, Android, or Kot

WillowTree, LLC 54 Nov 8, 2022
Format numbers using a string pattern with this simple number formatted like ##-####-##

AndroidPattern Format numbers using a string pattern with this simple number formatted like ##-####-## Installation To get a Git project into your bui

Hussein Habibi Juybari 2 Oct 25, 2021
Android library which makes it easy to handle the different obstacles while calling an API (Web Service) in Android App.

API Calling Flow API Calling Flow is a Android library which can help you to simplify handling different conditions while calling an API (Web Service)

Rohit Surwase 19 Nov 9, 2021
Android Utilities Library build in kotlin Provide user 100 of pre defined method to create advanced native android app.

Android Utilities Library build in kotlin Provide user 100 of pre defined method to create advanced native android app.

Shahid Iqbal 4 Nov 29, 2022
A robust native library loader for Android.

ReLinker A robust native library loader for Android. More information can be found in our blog post Min SDK: 9 JavaDoc Overview The Android PackageMan

Keepsafe 2.9k Dec 27, 2022
Joda-Time library with Android specialization

joda-time-android This library is a version of Joda-Time built with Android in mind. Why Joda-Time? Android has built-in date and time handling - why

Daniel Lew 2.6k Dec 9, 2022
UPnP/DLNA library for Java and Android

Cling EOL: This project is no longer actively maintained, code may be outdated. If you are interested in maintaining and developing this project, comm

4th Line 1.6k Jan 4, 2023
:iphone: [Android Library] Get device information in a super easy way.

EasyDeviceInfo Android library to get device information in a super easy way. The library is built for simplicity and approachability. It not only eli

Nishant Srivastava 1.7k Dec 22, 2022
Android library for viewing, editing and sharing in app databases.

DbInspector DbInspector provides a simple way to view the contents of the in-app database for debugging purposes. There is no need to pull the databas

Infinum 924 Jan 4, 2023
Android Market In-app Billing Library

Update In-app Billing v2 API is deprecated and will be shut down in January 2015. This library was developed for v2 a long time ago. If your app is st

Robot Media 533 Nov 25, 2022
An Android library allowing images to exhibit a parallax effect that reacts to the device's tilt

Motion An Android library allowing images to exhibit a parallax effect. By replacing static pictures and backgrounds with a fluid images that reacts t

Nathan VanBenschoten 781 Nov 11, 2022
Android library to easily serialize and cache your objects to disk using key/value pairs.

Deprecated This project is no longer maintained. No new issues or pull requests will be accepted. You can still use the source or fork the project to

Anup Cowkur 667 Dec 22, 2022
Form Validator Library for Android

Android-Validator Form Validator Library for Android [](https://flattr.com/submit/auto?user_id=throrin19&url=https://github.com/throrin19/Android-Vali

Benjamin Besse 449 Dec 17, 2022
Very easy to use wrapper library for Android SharePreferences

Treasure English document Treasure是一个Android平台上基于SharePreferences的偏好存储库,只需要定义接口,无需编写实现,默认支持Serializable和Parcelable。运行时0反射,不仅使用方便而且性能和原生写法几乎无差别。 使用方法 1

星一 507 Nov 12, 2022
Error handling library for Android and Java

ErrorHandler Error handling library for Android and Java Encapsulate error handling logic into objects that adhere to configurable defaults. Then pass

null 237 Dec 29, 2022
Small Android library to help you incorporate MVP, Passive View and Presentation Model patterns in your app

DroidMVP About DroidMVP is a small Android library to help you incorporate the MVP pattern along with Passive View and Presentation Model (yes, those

Andrzej Chmielewski 225 Nov 29, 2022
A simple Android utils library to write any type of data into cache files and read them later.

CacheUtilsLibrary This is a simple Android utils library to write any type of data into cache files and then read them later, using Gson to serialize

Wesley Lin 134 Nov 25, 2022
Android library that regroup bunch of dateTime utilities

DateTimeUtils This library is a package of functions that let you manipulate objects and or java date string. it combine the most common functions use

Thunder413 98 Nov 16, 2022
Slinger - deep linking library for Android

Slinger - deep linking library for Android Slinger is a small Android library for handling custom Uri which uses regular expression to catch and route

Allegro Tech 27 Dec 8, 2022