1 package ch
.cyberduck
.core
.i18n
;
4 * Copyright (c) 2002-2015 David Kocher. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * Bug fixes, suggestions and comments should be sent to:
18 * feedback@cyberduck.io
21 import ch
.cyberduck
.core
.Local
;
22 import ch
.cyberduck
.core
.preferences
.ApplicationResourcesFinderFactory
;
24 import org
.apache
.commons
.collections
.map
.LRUMap
;
25 import org
.apache
.commons
.io
.IOUtils
;
26 import org
.apache
.log4j
.Logger
;
28 import java
.io
.FileInputStream
;
29 import java
.io
.IOException
;
30 import java
.io
.InputStreamReader
;
31 import java
.io
.LineNumberReader
;
32 import java
.nio
.charset
.Charset
;
33 import java
.util
.Collections
;
35 import java
.util
.regex
.Matcher
;
36 import java
.util
.regex
.Pattern
;
41 public class RegexLocale
implements Locale
{
42 private static final Logger log
= Logger
.getLogger(RegexLocale
.class);
44 private Map
<Key
, String
> cache
45 = Collections
.<Key
, String
>synchronizedMap(new LRUMap(1000));
47 private Local resources
;
50 = java
.util
.Locale
.getDefault().getLanguage();
52 private Pattern pattern
53 = Pattern
.compile("\"(.*)\"\\s*=\\s*\"(.*)\";");
55 public RegexLocale() {
56 this(ApplicationResourcesFinderFactory
.get().find());
59 public RegexLocale(final Local resources
) {
60 this.resources
= resources
;
64 public void setDefault(final String language
) {
70 public String
localize(final String key
, final String table
) {
71 final Key lookup
= new Key(table
, key
);
72 if(!cache
.containsKey(lookup
)) {
76 catch(IOException e
) {
77 log
.warn(String
.format("Failure loading properties from %s.strings. %s", table
, e
.getMessage()));
80 if(cache
.containsKey(lookup
)) {
81 return cache
.get(lookup
);
86 private void load(final String table
) throws IOException
{
87 final LineNumberReader reader
= new LineNumberReader(new InputStreamReader(new FileInputStream(
88 String
.format("%s/%s.lproj/%s.strings", resources
.getAbsolute(), locale
, table
)
89 ), Charset
.forName("UTF-16")));
92 while((line
= reader
.readLine()) != null) {
93 final Matcher matcher
= pattern
.matcher(line
);
94 if(matcher
.matches()) {
95 cache
.put(new Key(table
, matcher
.group(1)), matcher
.group(2));
100 IOUtils
.closeQuietly(reader
);
104 private final class Key
{
105 private String table
;
108 public Key(final String table
, final String key
) {
114 public boolean equals(final Object o
) {
118 if(!(o
instanceof Key
)) {
121 final Key key1
= (Key
) o
;
122 if(key
!= null ?
!key
.equals(key1
.key
) : key1
.key
!= null) {
125 if(table
!= null ?
!table
.equals(key1
.table
) : key1
.table
!= null) {
132 public int hashCode() {
133 int result
= table
!= null ? table
.hashCode() : 0;
134 result
= 31 * result
+ (key
!= null ? key
.hashCode() : 0);