|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2011 Kevin Sawicki <kevinsawicki@gmail.com> |
| 2 | + * Copyright (c) 2015 Pedro Adame <pedro.a.1smr@gmail.com> |
3 | 3 | *
|
4 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
5 | 5 | * of this software and associated documentation files (the "Software"), to
|
@@ -50,23 +50,32 @@ public class TimeAgo {
|
50 | 50 | private String months;
|
51 | 51 | private String year;
|
52 | 52 | private String years;
|
| 53 | + private String country; |
| 54 | +/** |
| 55 | + * Constructor for the TimeAgo object. |
| 56 | + * @param locale The country code for the desired language to display messages. Supported languages are 'en', 'es', 'it', 'fr' and 'de' at the time. |
| 57 | + */ |
| 58 | + public TimeAgo(String locale) throws IllegalArgumentException{ |
| 59 | + init(locale); |
| 60 | + } |
53 | 61 |
|
54 |
| - public TimeAgo(String locale){ |
55 |
| - |
56 |
| - if(locale.equals("en")){ |
| 62 | + private void init(String locale){ |
| 63 | + if(locale.equals("en")) |
57 | 64 | initLanguage("EN");
|
58 |
| - }else if(locale.equals("es")){ |
| 65 | + else if(locale.equals("es")) |
59 | 66 | initLanguage("ES");
|
60 |
| - }else if(locale.equals("it")){ |
| 67 | + else if(locale.equals("it")) |
61 | 68 | initLanguage("IT");
|
62 |
| - }else if(locale.equals("fr")){ |
| 69 | + else if(locale.equals("fr")) |
63 | 70 | initLanguage("FR");
|
64 |
| - }else{ |
65 |
| - throw new IllegalArgumentException("Locale must be equals to 'es', 'en' or 'it'."); |
66 |
| - } |
| 71 | + else if(locale.equals("de")) |
| 72 | + initLanguage("DE"); |
| 73 | + else |
| 74 | + throw new IllegalArgumentException("Locale not recognised or it isn't supported yet."); |
67 | 75 | }
|
68 | 76 |
|
69 | 77 | private void initLanguage(String language) {
|
| 78 | + country = language.toLowerCase(); |
70 | 79 | prefixAgo = Messages.getString("TimeAgo.PREFIX_AGO" + "_" + language); //$NON-NLS-1$
|
71 | 80 | suffixAgo = Messages.getString("TimeAgo.SUFFIX_AGO" + "_" + language); //$NON-NLS-1$
|
72 | 81 | prefixFromNow = Messages.getString("TimeAgo.PREFIX_FROM_NOW" + "_" + language); //$NON-NLS-1$
|
@@ -450,4 +459,33 @@ public TimeAgo setYears(String years) {
|
450 | 459 | this.years = years;
|
451 | 460 | return this;
|
452 | 461 | }
|
| 462 | + |
| 463 | + /** |
| 464 | + * Returns the country code of the actual language. |
| 465 | + * @return Used country code. |
| 466 | + */ |
| 467 | + |
| 468 | + public String getCountryCode(){ |
| 469 | + return country; |
| 470 | + } |
| 471 | + |
| 472 | + /** |
| 473 | + * Changes the language of the expressions to one of the supported ones. |
| 474 | + * @param locale The new language. Must be equals to 'en', 'es', 'it', 'fr' or 'de'. |
| 475 | + */ |
| 476 | + public void changeLanguage(String locale){ |
| 477 | + if(locale.equals("en")) |
| 478 | + initLanguage("EN"); |
| 479 | + else if(locale.equals("es")) |
| 480 | + initLanguage("ES"); |
| 481 | + else if(locale.equals("it")) |
| 482 | + initLanguage("IT"); |
| 483 | + else if(locale.equals("fr")) |
| 484 | + initLanguage("FR"); |
| 485 | + else if(locale.equals("de")) |
| 486 | + initLanguage("DE"); |
| 487 | + else |
| 488 | + throw new IllegalArgumentException("Locale not recognised or it isn't supported yet."); |
| 489 | + |
| 490 | + } |
453 | 491 | }
|
0 commit comments