Code Monkey home page Code Monkey logo

phrase's Introduction

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(", ")
  .join(1, 2, 3);
// returns "1, 2, 3"

English sentence-style lists:

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

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

listFormatter.join(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.

phrase's People

Contributors

ajit2903 avatar chrisrenke avatar dbriggsdfleet avatar dlew avatar edenman avatar jakewharton avatar loganj avatar pforhan avatar quinnjn avatar rjrjr avatar rwliang avatar zach-klippenstein avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

phrase's Issues

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.

Support for plurals

How do you handle "1 Beer" vs "3 Beers" with this library? Is there any special code for that or is something like that planned?

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?

Phrase.put(String, int)

We should at least call out in the docs that Phrase.put(String, int) uses the value of the int as a string, rather than grabbing a string from resources. Many Android views have interchangeable methods like setText(String) and setText(int) that could mislead a user of this library.

I don't have great suggestions of a more concrete fix. Maybe rename to putInt(String, int) but that seems ugly.

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

Proposal: Interceptor API

Hey! This looks pretty great, and I wanted to propose adding an interceptor API of sorts to allow for dynamic configuration of strings as they come through. This would allow for experimentation with copy pretty easily, as well as anything else people might come up with a need for.

Support for colors

Something like this?

<string name="xx"><color="@color/color_id">{a}</color> is {b}<color="#aabbcc">awesome?</color></string>

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

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 !

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)

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

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.